As an echo of lines having a specific identifier in it from the database

So first, my database table is set up like this:

id | userid | quantity | date | status

1 | 10 | 25.00 | 2017-09-12 | Is absent

and I want to highlight all rows that include userid 10 in the html table. I've 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 is displayed.

+3


source to share


3 answers


Use the code below:

1.use $ userinfo instead of $ query while($row = mysql_fetch_array($query))

2. Change $mysqli->query

to mysqli_query

.



3. Use mysqli

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

      

+1


source


you are mixing mysqli with mysql and object oriented style with procedural

use object oriented like 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>';
?>

      

0


source


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 I am just annoying but am not using * in your select statement. Always include fields, even if you need each one. You must also use prepared statements.

0


source







All Articles