<?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);?>
<?php$connection = mysqli_connect("localhost","root","root","sunyang");if ( $connection ) {echo "数据库连接成功"; }else {echo "数据库连接失败"; }?>
<?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();//关闭连接?>
面向对象方法: <?php $conn = mysqli('localhost', ''my_user', 'my_password', 'world'); $sql = "select * from db_table"; $query = $conn->query($sql); while($row = $query->fetch_array()){ echo $row['title']; } ?> 面向过程方法: <?php $conn = mysqli_connect('localhost', ''my_user', 'my_password', 'world') or ('error'); $sql = "select * from db_table"; $query = mysqli_query($conn,$sql); while($row = mysqli_fetch_array($query)){ echo $row['title']; } ?>