连接扩展
1. Mysqli扩展
//mysqli扩展
if(function_exists('mysqli_connect')){
echo 'Mysqli扩展已经安装'."\n";
}
$link = mysqli_connect('localhost','root','123456','test','3306');
2. pdo扩展
$dbh = new PDO('mysql:host=localhost;dbname=test','root','123456');
inset
<?php
$link = mysqli_connect('localhost','root','123456','test','3306');
$name = '笨猪';
$sql = "insert into user(name) values ('$name')";
echo $sql."\n";
mysqli_query($link,$sql);
$id = mysqli_insert_id($link);
echo $id;
mysqli_close($link);
select
可以通过设定参数MYSQL_NUM只获取数字索引数组,等同于mysql_fetch_row函数,如果设定参数为MYSQL_ASSOC则只获取关联索引数组,等同于mysql_fetch_assoc函数。
$row = mysql_fetch_row($result);
$row = mysql_fetch_array($result, MYSQL_NUM);
$row = mysql_fetch_assoc($result);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
如果需要获得所有的结果 需要循环遍历
while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
print_r($row)."\n";
}
update、delete
获取更新过的数据行数:
if(mysqli_query($link,$sql)){
//输出更新的行数
echo mysqli_affected_rows($link);
}
关闭数据库
mysqli_close($link);
评论