PHP5 mysqli 教程
<?php$db_host="localhost"; //连接的服务器地址$db_user="root"; //连接数据库的用户名$db_psw="root"; //连接数据库的密码$db_name="sunyang"; //连接的数据库名称$mysqli=new mysqli($db_host,$db_user,$db_psw,$db_name);?>
<?php $db_host="localhost"; //连接的服务器地址 $db_user="root"; //连接数据库的用户名 $db_psw="root"; //连接数据库的密码 $db_name="sunyang"; //连接的数据库名称 $mysqli=new mysqli(); $mysqli->connect($db_host,$db_user,$db_psw,$db_name); ?>
$mysqli->close();
<?php $connection = mysqli_connect("localhost","root","root","sunyang"); if ( $connection ) { echo "数据库连接成功"; }else { echo "数据库连接失败"; } ?>
mysqli_close();
<?php $mysqli=new mysqli("localhost","root","root","sunyang"); //实例化mysqli $query="select * from employee"; $result=$mysqli->query($query); if ($result) { if($result->num_rows>0){ //判断结果集中行的数目是否大于0 while($row =$result->fetch_array() ){ //循环输出结果集中的记录 echo ($row[0])."<br>"; echo ($row[1])."<br>"; echo ($row[2])."<br>"; echo ($row[3])."<br>"; echo "<hr>"; } } }else { echo "查询失败"; } $result->free(); $mysqli->close(); ?>
<?php $mysqli=new mysqli("localhost","root","root","sunyang"); //实例化mysqli $query="delete from employee where emp_id=2"; $result=$mysqli->query($query); if ($result){ echo "删除操作执行成功"; }else { echo "删除操作执行失败"; } $mysqli->close(); ?>
<?php $mysqli=new mysqli("localhost","root","root","sunyang"); //实例化mysqli $query="select * from employee"; $result=$mysqli->prepare($query); //进行预准备语句查询 $result->execute(); //执行预准备语句 $result->bind_result($id,$number,$name,$age); //绑定结果 while ($result->fetch()) { echo $id; echo $number; echo $name; echo $age; } $result->close(); //关闭预准备语句 $mysqli->close(); //关闭连接 ?>
字符种类 | 代表的数据类型 |
I | integer |
D | double |
S | string |
B | blob |
<?php $mysqli=new mysqli("localhost","root","root","sunyang"); //实例化mysqli $query="insert into employee (emp_number,emp_name,emp_age) values (?,?,?)"; $result=$mysqli->prepare($query); $result->bind_param("ssi",$number,$name,$age); //绑定参数 $number=‘sy0807‘; $name=‘employee7‘; $age=20; $result->execute(); //执行预准备语句 $result->close(); $mysqli->close(); ?>
<?php $mysqli=new mysqli("localhost","root","root","sunyang"); //实例化mysqli $query="select * from employee where emp_id < ?"; $result=$mysqli->prepare($query); $result->bind_param("i",$emp_id); //绑定参数 $emp_id=4; $result->execute(); $result->bind_result($id,$number,$name,$age); //绑定结果 while ($result->fetch()) { echo $id."<br>"; echo $number."<br>"; echo $name."<br>"; echo $age."<br>"; } $result->close(); $mysqli->close(); ?>
$mysqli=new mysqli("localhost","root","root","sunyang"); //实例化mysqli $query = "select emp_name from employee ;"; $query .= "select dep_name from depment "; if ($mysqli->multi_query($query)) { //执行多个查询 do { if ($result = $mysqli->store_result()) { while ($row = $result->fetch_row()) { echo $row[0]; echo "<br>"; } $result->close(); } if ($mysqli->more_results()) { echo ("-----------------<br>"); //连个查询之间的分割线 } } while ($mysqli->next_result()); } $mysqli->close();//关闭连接 ?>
$link = get_connect(); $ret = array(); if($link->real_query($sql)){ if ($r = $link->store_result()) { while( $row = $r->fetch_row()){ $ret[] = $row; } $r->close(); } } return $ret;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。