<?php
$db=new mysqli("localhost","root","root","db");
if(mysqli_connect_errno()){
echo "数据库链接失败!<br>\n";
echo mysqli_connect_error();
exit;
}
$sql="select * from t_user";
$result=$db->query($sql);
if(!$result){
$db->close();
echo '查询失败!';
exit;
}
echo "<table border='1' bgcolor='lightblue'>";
echo "<tr><th>用户名</th><th>密码</th><th>姓名</th><th>Email</th></tr>";
while($row=$result->fetch_assoc()){
echo "<tr><td>{$row['f_username']}</td>";
echo "<td>{$row['f_password']}</td>";
echo "<td>{$row['f_sex']}</td>";
echo "<td>{$row['f_email']}</td></tr>\n";
}
echo "</table>";
$result->free();
$db->close();
?>
请问我为什么我无法查询该数据库中记录?
$db=new mysqli("localhost","root","root","db");
if(mysqli_connect_errno()){
echo "数据库链接失败!<br>\n";
echo mysqli_connect_error();
exit;
}
$sql="select * from t_user";
$result=$db->query($sql);
if(!$result){
$db->close();
echo '查询失败!';
exit;
}
echo "<table border='1' bgcolor='lightblue'>";
echo "<tr><th>用户名</th><th>密码</th><th>姓名</th><th>Email</th></tr>";
while($row=$result->fetch_assoc()){
echo "<tr><td>{$row['f_username']}</td>";
echo "<td>{$row['f_password']}</td>";
echo "<td>{$row['f_sex']}</td>";
echo "<td>{$row['f_email']}</td></tr>\n";
}
echo "</table>";
$result->free();
$db->close();
?>
请问我为什么我无法查询该数据库中记录?
里面没有任何数据显示
echo " <td>" . $row["f_password"] . "</td>";
我也是写成
echo " <td>" . $row["f_password"] . " </td>"; 不是比第一种写起来更加简单吗?
用“{}”括起来后,PHP系统会认为这是一个变量,比如如果$row['f_sex']的值为“男”,则系统会查找变量“$男”是否存在,它当然不会存在了,所以不会有任何数据出现。也就是说
如果$row['f_sex']=男,则{$row['f_sex']} = $男
你明白了吗?,只要改成echo " <td>" . $row["f_sex"] . " </td>"; 就可以了。
$sql="insert into t_user(f_user,f_password,f_sex,f_email) values";
$sql .="('$username','$pwd','$sex','$email')";在另一个register.php我已经有插入这么一句了 怎么无法链接到数据库?
echo " <td>{$row['f_password']} </td>";
echo " <td>" . $row["f_password"] . " </td>";
这两种写法,意思是一样的,结果也是一样的。不懂不要在这乱回答。误导别人...看楼主帖的程序,好像没什么问题,如果程序执行没有报错的话,那应该就是t_user表里没有数据
楼主去数据库看下有没有数据比较好