[英]How do I echo rows that have a specific ID in it from Database
So first off my database table is set up like this: 所以首先我的数据库表设置如下:
id | id | userid | userid | amount | 金额| date | 日期| status 状态
1 | 1 | 10 | 10 | 25.00 | 25.00 | 2017-09-12 | 2017-09-12 | None 没有
and I want to to echo out all the rows that include userid 10 into a html table. 我想将包含userid 10的所有行回显到html表中。 I have tried this: 我试过这个:
<?php
$id = $_GET['id'];
$userinfo= $mysqli->query("SELECT * FROM invoices WHERE userid = $id");
$row = mysql_fetch_array($userinfo) or die();
echo '<table>';
while($row = mysql_fetch_array($query)){
echo '<tr>
<td><font size="2" face="Lucida Sans Unicode" color=#EBEBEB>' .$row['id'].'</td>
<td><font size="2" face="Lucida Sans Unicode" color=#EBEBEB>' .$row['amount'].'</td>
<td><font size="2" face="Lucida Sans Unicode" color=#EBEBEB>' .$row['date'].'</td>
<td><font size="2" face="Lucida Sans Unicode" color=#EBEBEB>' .$row['status'].'</td>
</tr>';
}
echo '</table>';
?>
and nothing shows up. 没有任何表现。
Use below code: 使用以下代码:
1 . 1。 use $userinfo instead of $query while($row = mysql_fetch_array($query))
使用$ userinfo而不是$ query while($row = mysql_fetch_array($query))
2 . 2。 Change $mysqli->query
to mysqli_query
. 将$mysqli->query
更改$mysqli->query
mysqli_query
。
3 . 3。 Use mysqli
instead of mysql
. 使用mysqli
而不是mysql
。
<?php
$id = $_GET['id'];
//mysqli_connect("host","username","password","dbname")
$conn=mysqli_connect("localhost","root","","dbname");
$sql="SELECT * FROM invoices WHERE userid = '.$id.'";
$result=mysqli_query($conn,$sql);
echo '<table>';
while($row = mysqli_fetch_array($result)){
echo '<tr>
<td>' .$row['id'].'</td>
<td>' .$row['amount'].'</td>
<td>' .$row['date'].'</td>
<td>' .$row['status'].'</td>
</tr>';
}
echo '</table>';
?>
you mixing mysqli with mysql and object oriented style with Procedural 你使用Procedural将mysqli与mysql和面向对象的样式混合在一起
use object oriented as below 使用面向对象如下
<?php
$id = $_GET['id'];
$userinfo= $mysqli->query("SELECT * FROM invoices WHERE userid = $id");
echo '<table>';
while($row = $userinfo->fetch_assoc()){
echo '<tr>
<td><font size="2" face="Lucida Sans Unicode" color=#EBEBEB>' .$row['id'].'</td>
<td><font size="2" face="Lucida Sans Unicode" color=#EBEBEB>' .$row['amount'].'</td>
<td><font size="2" face="Lucida Sans Unicode" color=#EBEBEB>' .$row['date'].'</td>
<td><font size="2" face="Lucida Sans Unicode" color=#EBEBEB>' .$row['status'].'</td>
</tr>';
}
echo '</table>';
?>
Try this instead: 试试这个:
<?php
$id = $_GET['id'];
$mysqli = new \mysqli('localhost', 'username', 'password', 'database');
$userinfo= $mysqli->query("SELECT id, amount, date, status FROM invoices WHERE userid = $id");
echo '<table>';
if ($userinfo->num_rows > 0) {
while($row = $userinfo->fetch_assoc()) {
echo '<tr>
<td><font size="2" face="Lucida Sans Unicode" color=#EBEBEB>' .$row['id'].'</td>
<td><font size="2" face="Lucida Sans Unicode" color=#EBEBEB>' .$row['amount'].'</td>
<td><font size="2" face="Lucida Sans Unicode" color=#EBEBEB>' .$row['date'].'</td>
<td><font size="2" face="Lucida Sans Unicode" color=#EBEBEB>' .$row['status'].'</td>
</tr>';
}
}
else {
echo "0 rows returned";
}
echo '</table>';
?>
Also just me being annoying but don't use * in your select statement. 另外,我只是烦人但不要在你的select语句中使用*。 Always list the fields even if you want every single one. 即使您想要每个字段,也要始终列出字段。 You should also be using prepared statements. 您还应该使用预准备语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.