博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP数据库的基本操作
阅读量:5249 次
发布时间:2019-06-14

本文共 3210 字,大约阅读时间需要 10 分钟。

<?php

//连接数据库

/*$con=mysql_connect("localhost","root","19960621");// or die('Error:'.mysql_error());
if(!$con){
die("连接失败!".mysql_errno());
}else{
echo "连接数据库成功";
mysql_query("CREATE DATABASE dbmysql",$con);
mysql_select_db("dbmysql",$con);
$sql="CREATE TABLE person (personID int NOT NULL AUTO_INCREMENT,FirstName varchar(15),LastName varchar(15),Age int,PRIMARY KEY(personID))";
$str=mysql_query($sql,$con);
echo "<br>".$str."<br>";
mysql_close($con);
}
//往表里插数据
$con=mysql_connect("localhost","root","19960621");
mysql_select_db("dbmysql",$con);
mysql_query("insert into person(`FirstName`, `LastName`, `Age`)values('tom','tony','20')");
mysql_query("insert into person(`FirstName`, `LastName`, `Age`)values('lh','h','16')");
mysql_query("insert into person(`FirstName`, `LastName`, `Age`)values('jy','j','24')");
//查询数据
$con=mysql_connect("localhost","root","19960621");
mysql_select_db("dbmysql",$con);
$result=mysql_query("select * from person");
while($row=mysql_fetch_array($result)){
echo $row["FirstName"]."".$row["LastName"];
echo "<br>";
}
//更新数据
$con=mysql_connect("localhost","root","19960621");
mysql_select_db("dbmysql",$con);
mysql_query("update person set FirstName='dx',LastName='x',`Age`=18 where `personID`=3");
//删除数据
$con=mysql_connect("localhost","root","19960621");
mysql_select_db("dbmysql",$con);
mysql_query("delete from person where `personID`=2");
mysql_close($con);

//mysql_list_dbs()函数==>列出 MySQL 服务器中所有的数据库

$con=mysql_connect("localhost","root","19960621");
$dl=mysql_list_dbs($con);
while($db=mysql_fetch_object($dl)){
echo $db->Database."<br>";
}
mysql_close($con);
//mysql_num_fields()函数==>返回结果集中字段的数
$con = mysql_connect("localhost", "root", "19960621");
if (!$con){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("dbmysql",$con);
$sql = "SELECT * FROM person";
$result = mysql_query($sql,$con);
echo mysql_num_fields($result);//输出列的个数
mysql_close($con);
//mysql_num_rows()函数==>返回结果集中行的数目
$con = mysql_connect("localhost", "root", "19960621");
if (!$con){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("dbmysql",$con);
$sql = "SELECT * FROM person";
$result = mysql_query($sql,$con);
echo mysql_num_rows($result);//查询出的总行数
mysql_close($con);
//mysql_affected_rows()函数==>返回前一次MySQL操作所影响的记录行数
$con = mysql_connect("localhost", "root", "19960621");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbmysql");
mysql_query("DELETE FROM person WHERE personid =3");
$rc = mysql_affected_rows();
echo "Records deleted: " . $rc;
mysql_close($con);
//mysql_real_escape_string()函数==>转义SQL语句中使用的字符串中的特殊字符
//获得用户名和密码的代码
$user="a\r\nbc";
$pwd="a\r\nbc";
//转义用户名,以便在 SQL 中使用
$user = mysql_real_escape_string($user);
$pwd = ($pwd);
$sql = "SELECT * FROM users WHERE user='" . $user . "' AND password='" . $pwd . "'";
echo $sql; //用户名和密码对比
//mysql_result() 函数返回结果集中一个字段的值
$con = mysql_connect("localhost", "root", "19960621");
if (!$con){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("dbmysql", $con);
$sql = "SELECT * from Person";
$result = mysql_query($sql,$con);
echo mysql_result($result,1,2);
mysql_close($con);*/

转载于:https://www.cnblogs.com/phpzhang/p/6062098.html

你可能感兴趣的文章
Js时间处理
查看>>
Java项目xml相关配置
查看>>
三维变换概述
查看>>
第三次作业
查看>>
vue route 跳转
查看>>
【雷电】源代码分析(二)-- 进入游戏攻击
查看>>
Entityframework:“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。...
查看>>
Linux中防火墙centos
查看>>
mysql新建用户,用户授权,删除用户,修改密码
查看>>
FancyCoverFlow
查看>>
JS博客
查看>>
如何设置映射网络驱动器的具体步骤和方法
查看>>
ASP.NET WebApi 基于OAuth2.0实现Token签名认证
查看>>
283. Move Zeroes把零放在最后面
查看>>
Visual Studio Code 打开.py代码报Linter pylint is not installed解决办法
查看>>
Python 数据类型
查看>>
S5PV210根文件系统的制作(一)
查看>>
centos下同时启动多个tomcat
查看>>
slab分配器
查看>>
数据清洗
查看>>