Reputation: 37
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:
<?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.
Upvotes: 3
Views: 4013
Reputation: 56
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.
Upvotes: 0
Reputation: 4166
Use below code:
1 . use $userinfo instead of $query while($row = mysql_fetch_array($query))
2 . Change $mysqli->query
to mysqli_query
.
3 . Use mysqli
instead of 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>';
?>
Upvotes: 1
Reputation: 6311
you mixing mysqli with mysql and object oriented style with Procedural
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>';
?>
Upvotes: 0