<?php
header("Content-type: text/html; charset=utf-8");
$conn = mysqli_connect("localhost", "root", "123456", "test002");
if (!$conn) {
die("连接失败" . mysqli_connect_error());
} else {
echo "Success!";
}
//mysqli_select_db($conn,"test001");
$sql = "select * from user1";
//解决中文乱码;
mysqli_query($conn, "set names utf8");
$res = mysqli_query($conn, $sql);
//var_dump($res);
while ($row = mysqli_fetch_row($res)) {
//echo "<br>$row[0]--$row[1]--$row[2]";
//var_dump($row);
foreach($row as $key=>$val){
echo "--$val";
}
echo "<br>";
}
$res1 = mysqli_query($conn, $sql);
$row001=mysqli_fetch_row($res1);
print_r($row001);
$row002=mysqli_fetch_array($res1,MYSQL_ASSOC);
echo "<br>";
print_r($row002);
//指针复位;
mysqli_data_seek($res1,0);
while ($row = mysqli_fetch_row($res1)) {
echo "<br>$row[0]--$row[1]--$row[2]";
}
//清理内存,关闭连接;
mysqli_free_result($res1);
mysqli_close($conn);
echo "<br>hello";
******************************************************
if (!$conn) {
die("link failed" . mysqli_connect_error());
}
$sql = "insert into user1 (name,password,email,age) values ('小王',1234565,
'wang@sohu.com',44),('小芳',44546,'fang@sohu.com',47)";
$sql="delete from user1 where id=4 or id=6";
$sql="update user1 set age=33 where id=2";
******************************************************
<?php
class SqlTool {
private $conn;
private $host="localhost";
private $user="root";
private $password="123456";
function SqlTool($database){
$this->conn=mysqli_connect($this->host,$this->user,$this->password,$database);
if(!$this->conn){ die("LINK FAILED".mysqli_connect_error());
}
mysqli_query($this->conn,"set names utf8");
}
public function execute_dql($sql){
$bool=mysqli_query($this->conn,$sql);
return $bool; }
public function execute_dml($sql){
$bool=mysqli_query($this->conn,$sql);
if(!$bool){
return 0;
}else if(mysqli_affected_rows($this->conn)>0){ return 1;
}else{
return 2;
}
}
}
************************************************
$sql="select * from user1";
$sqlTool=new SqlTool($database);
$res=$sqlTool->execute_dql($sql);
while($row=mysqli_fetch_row($res)){
foreach($row as $key=>$val){
echo "--$val";
}
echo "<br>";
}
mysqli_free_result($res);
*****************************************************
用表格输出数据库
<?php
header("Content-Type:text/html;charset=utf8");
$conn = mysqli_connect('localhost','root','123456','test003');
$sql = "select * from user1";
//解决中文乱码;
mysqli_query($conn,"set names utf8");
$res = mysqli_query($conn, $sql);
$rows=mysqli_num_rows($res);
$columns=mysqli_num_fields($res);
echo "<table border=1><tr>";
//取出字段名;
while($info=mysqli_fetch_field($res)) {
echo "<th>$info->name</th>";
}
//取出数据;
while($row=mysqli_fetch_row($res)){
echo "<tr>";
for($i=0;$i<$columns;$i++){
echo "<td>$row[$i]</td>";
}
echo "</tr>";
}
echo "</tr>";
echo "</table>";