为什么我的PHP插入的行数正确,但行中没有数据?
我正在尝试添加一个脚本,该脚本向我的MySQL数据库发出请求,并将数据显示回html表中。在发出请求的那一分钟,表中的行数与mysql数据中的行数相对应,但是没有物理数据 我尝试使用的代码是:为什么我的PHP插入的行数正确,但行中没有数据?,php,mysql,session,html-table,get,Php,Mysql,Session,Html Table,Get,我正在尝试添加一个脚本,该脚本向我的MySQL数据库发出请求,并将数据显示回html表中。在发出请求的那一分钟,表中的行数与mysql数据中的行数相对应,但是没有物理数据 我尝试使用的代码是: <div class="container" style="margin-top:2em;margin-bottom:33em;"> <table border="1" class="table table-striped" style="margin-top: 2em;">
<div class="container" style="margin-top:2em;margin-bottom:33em;">
<table border="1" class="table table-striped" style="margin-top: 2em;">
<thead>
<tr>
<th>id</th>
<th>username</th>
<th>passcode</th>
</tr>
</thead>
<tbody>
<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
$servername="localhost";
$username="wvptszyl_sc";
$password="wvptszyl_sc";
//!@#$%^&*(
$db="wvptszyl_sc";
$conn=mysqli_connect($servername,$username,$password,$db);
//mysql_select_db($db);
if (!$conn) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno($conn) . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error($conn) . PHP_EOL;
exit;
}
@session_start();
$result = $conn->prepare("SELECT id, username, passcode FROM admin");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr>
<td><label><?php echo $row['id']; ?></label></td>
<td><label><?php echo $row['username']; ?></label></td>
<td><label><?php echo $row['passcode']; ?></label></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
身份证件
用户名
密码
您误解了for循环。它用于重复特定的操作,直到指定的条件为false。您的条件是一个赋值,因此存在错误
所以改变这个
对于($i=0;$row=$result->fetch();$i++){
为此:
foreach ($result->fetch_all() as $row) {
$result->fetch_all()返回数据的关联数组。
使用foreach循环,您可以访问该数组中的每个元素,并可以使用$row['any-key-you-want']什么是print\r($row):
??此外,error\u reporting(E\u ALL);ini\u set('display\u errors',1')
不要抑制错误。删除@
并启用显示错误我正在解释为什么应该显示行。您混合了过程式和OOP风格的mysqli。mysqli\u connect
返回一个资源,但返回一个对象。使用$conn=new mysqli(
@Quasimodo's一个不太可能是OP问题原因的问题。虽然这可能是OP问题的合理答案,但您仍然需要提高答案的质量。尽量不要发布仍然需要澄清的答案。如果您已经发现问题,您需要解释问题发生的原因,最好的方法是什么为了修复它。编辑了我的答案。现在好了吗?不是真的,它有点糟糕。关于for
语句无效的那一点是什么?fetch\u all
返回关联数组、数字数组,或者两者都返回。为什么用fetch\u all
替换($row=$result->fetch())
此处,因为从来没有使用过$i
。这应该不是问题所在。@Dharman我也不喜欢fetch_all
,因为它会对大型表造成资源影响。