php - 如何从数据库中回显其中具有特定 ID 的行

标签 php html mysql

所以首先我的数据库表是这样设置的:

id | userid | amount | date | status

1 | 10 | 25.00 | 2017-09-12 | None

我想将包含 userid 10 的所有行回显到 html 表中。我试过这个:

<?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>';
?>

什么也没有显示。

最佳答案

使用下面的代码:

1 。使用 $userinfo 而不是 $query while($row = mysql_fetch_array($query))

2。将 $mysqli->query 更改为 mysqli_query

3。使用 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>';
?>

关于php - 如何从数据库中回显其中具有特定 ID 的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44793513/

相关文章:

php - mssql_connect() : Unable to connect to server

php - 二叉树映射数据库查询

javascript - 如何正确停止ajax请求

html - 使用 css 调整 img 内容的大小

html - CSS 悬停嵌套列表

mysql - 本地存储 : MySQL vs. JSON?

php - mysql 连接两个表,count 和 sum 别名

php - Laravel 5.4 尚未设置门面根

javascript - 修复由 tumblr 的模板系统发出的损坏的 html

php - 这是一种有效的更新方法吗