So first off my database table is set up like this:
所以首先我的数据库表设置如下:
id | userid | amount | date | status
1 | 10 | 25.00 | 2017-09-12 | None
and I want to to echo out all the rows that include userid 10 into a html table. I have tried this:
我想将包含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>';
?>
and nothing shows up.
没有任何表现。
3 个解决方案
#1
1
Use below code:
使用以下代码:
1 . use $userinfo instead of $query while($row = mysql_fetch_array($query))
1。使用$ userinfo而不是$ query while($ row = mysql_fetch_array($ query))
2 . Change $mysqli->query
to mysqli_query
.
2。将$ mysqli-> query更改为mysqli_query。
3 . Use mysqli
instead of mysql
.
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>';
?>
#2
0
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>';
?>
#3
0
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. Always list the fields even if you want every single one. You should also be using prepared statements.
另外我只是烦恼但不要在你的select语句中使用*。即使您想要每个字段,也要始终列出字段。您还应该使用预准备语句。
#1
1
Use below code:
使用以下代码:
1 . use $userinfo instead of $query while($row = mysql_fetch_array($query))
1。使用$ userinfo而不是$ query while($ row = mysql_fetch_array($ query))
2 . Change $mysqli->query
to mysqli_query
.
2。将$ mysqli-> query更改为mysqli_query。
3 . Use mysqli
instead of mysql
.
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>';
?>
#2
0
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>';
?>
#3
0
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. Always list the fields even if you want every single one. You should also be using prepared statements.
另外我只是烦恼但不要在你的select语句中使用*。即使您想要每个字段,也要始终列出字段。您还应该使用预准备语句。