用法

1
mysqli_query(connection,query,resultmode);
参数 用法
connection 必需。规定要使用的 MySQL 连接。
query 必需。规定查询字符串。
resultmode 可选。一个常量。

返回值

mysql_query() 仅对SELECT | SHOW | EXPLAIN | DESCRIBE 语句返回一个资源标识符,如果查询执行不正确则返回 FALSE。

对于其它类型的 SQL 语句如insert|update,mysql_query() 在执行成功时只返回 TRUE,出错时返回 FALSE。

非 FALSE 的返回值意味着查询是合法的并能够被服务器执行。这并不说明任何有关影响到的或返回的行数。很有可能一条查询执行成功了但并未影响到或并未返回任何行。

结果集

fetch_array() : 返回结果集的一行作为数组,两种数组索引都行

fetch_object(): 返回结果集的一行作为对象

fetch_row() : 返回结果集的一行作为枚举数组,只能使用数字索引

fetch_assoc() : 返回结果集的一行作为关联数组,只能是关联数组的索引形式

Mysqli_connect连接

1
2
3
4
5
6
7
8
9
10
$server = "localhost";
$username = "da1sy";
$password = "*****";
$db_name = "da1sy";
$coon=mysqli_connect($server,$username,$password,$db_name);
if(!$coon)
{//连接失败会输出error+错误代码
die("error:".mysqli_connect_error());
}
$coon-> close();//结束释放

实现登录认证

1
2
3
4
5
$sql = mysqli_query($coon,"SELECT * from student where id = id and name='name';");
$rows=$sql->fetch_object();//将结果以对象方式传给$rows
if($rows){
}else{
}

其他查询

1
2
3
4
mysqli_query($coon,"INSERT INTO num VALUES ($max,0, $news);");//插入一条记录
$max = mysqli_query($coon,"SELECT * from num where seq =".$seq.";");//提取num字段内容
$num = $num->fetch_array();
$num = $num['1'];//以数组的形式展现某字段的值