PHP无法打印mysql表的两列数据,结果仅显示证明正确的第一行数据,其余各行显示 “未定义的偏移量”。
$query1='select MNO from mima01 order by MNO;';
$query2='select MTYPE from mima01 order by MNO;';
$result1=mysqli_query($conn,$query1);
$result2=mysqli_query($conn,$query2);
$username=mysqli_fetch_array($result1);
$usertype=mysqli_fetch_array($result2);
$count='select count(*) from mima01;';
$usercount=mysqli_query($conn,$count);
$usernum=mysqli_fetch_array($usercount);
for($i=0;$i<$usernum[0];$i++) {
echo '<tr><td style="text-align:center">' ;
echo $username[$i];
echo '</td>';
echo '<td style="text-align:center">' ;
echo $usertype[$i];
echo '</td></tr>';
}
答案 0 :(得分:0)
我已将您的代码重写为遍历查询结果的常规方式:
$query = 'select MNO, MTYPE from mima01 order by MNO;';
$result = mysqli_query($conn, $query);
while ($data = mysqli_fetch_array($result)) {
echo '<tr><td style="text-align:center">' .
$data['MNO'] .
'</td>' .
'<td style="text-align:center">' .
$data['MTYPE'] .
'</td></tr>';
}
请注意,我无法运行此代码,因此我不能百分百确定它会起作用。
答案 1 :(得分:0)
据我了解,我认为您的解决方案是:
$query='SELECT MNO,MTYPE FROM mima01 ORDER BY MNO DECS;'; //use DECS|ASC
$result=mysqli_query($conn,$query);
$username=mysqli_fetch_array($result);
// to count the rows
$count=mysqli_num_rows($result);;
echo $count;
// to display data
while($row=mysqli_fetch_array($result) {
echo '<tr><td style="text-align:center">';
echo $row['MNO'];
echo '</td>';
echo '<td style="text-align:center">';
echo $row['MTYPE'];
echo '</td></tr>';
}
希望这很有帮助