Pergunta

I have a pagination that have over 30 records and works great under table name "product". I also created another table name "Note" under same database, that note is allow me to read note of each product.

And now I'm trying to add new column features in pagination to check to see if any records already exist in "note" table using product_id number.

<?php 
$sql = new mysqli($mysql_hostname, $mysql_user, $mysql_password, 
$mysql_database);
$query = "SELECT product_id FROM notes WHERE product_id = $product_id";
$result = mysqli_query($sql, $query);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
    if(isset($row['$product_id'])) {
    echo "<p>No note </p>";
    } else {
    echo "<p>have note </p>";
    } 
?>

The problem I have is that in "note" table has 3 records, and when I run pagination, is showing only first row that are not exit any note... and is only one records instead of All records (30 records).. I did NOT put "limit 1 ) in WHERE statements ...

My question is that how can I write php code to allow me ALL records whether has note or not but show in each records column "no note" or "have note" if "product_id" number exit or not!

many thanks for help.

Foi útil?

Solução 2

mysqli_fetch_array returns an array per row, and requires you to loop the call to work with each result. Also, to call an element from the array you need $row['product_id'] not $row['$product_id']

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

     if(isset($row['product_id'])) {
         echo "<p>No note </p>";
     } else {
         echo "<p>have note </p>";
     } 
}

If you want to display all records from the database, regardless of product_id, you can change your query to:

$query = "SELECT product_id FROM notes";

Outras dicas

You need to loop through the result.

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
    // do something
}

Checking isset($row['product_id']) is pointless - if it doesn't equal $product_id then that row won't be returned by the query anyway.

You need to iterate through mysqli_fetch_array. Try this instead

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
          if(isset($row['$product_id'])) {
                  echo "<p>No note </p>";
          } else {
                  echo "<p>have note </p>";
          } 
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top