0

I have a database with 4 tables. (products,purchase,customer,user). When I tried to display all rows in products, no results. But in the user table, it displays. What should be the problem? Is it on my database?tables?php code?

Here's my code:

<?php

$db = mysqli_connect("localhost","root","", "prodpurchase");
if (!$db) {
die('Could not connect: ' . mysqli_error());
}

$sql = mysqli_query($db, "select * from user");
if( $sql === FALSE ) {
  die('Query failed returning error: '. mysqli_error());
} else {
   while($row=mysqli_fetch_array($sql))
   {
    echo $row['username']. "<br>";
   }
}
?>

Hope you could help me.

7
  • what error you getting ? Commented Mar 25, 2013 at 4:16
  • I think you are mixing up some words here. You say you have a database with 4 columns when a database doesn't have columns. A table has columns. Commented Mar 25, 2013 at 4:19
  • @DevangRathod: no error..empty results.. Commented Mar 25, 2013 at 4:22
  • Can you show your products table query ? Commented Mar 25, 2013 at 4:24
  • @JohnPeter: no results..as in white plain interface. Commented Mar 25, 2013 at 4:25

6 Answers 6

1

Did you check the letters uppercase and lowercase in table columns?

Sign up to request clarification or add additional context in comments.

Comments

1

You have inventory in your code, but in your question you named the table products, Is it this simple?

Comments

0
echo $row['item']. "<br>";

What is 'item'? Shouldn't you be selecting them by $row[0] or $row['products'].. or one of your other columns.

Comments

0

check your variables, for your example, try renaming your vairable $sql to something else, because you might have a similar variable somewhere in your code that you did not show.

try this :

<?php

$db = mysqli_connect("localhost","root","", "prodpurchase");
if (!$db) {
die('Could not connect: ' . mysqli_error());
}

$sqlstackoverflow = mysqli_query($db, "select * from user");
if($sqlstackoverflow === FALSE ) {
  die('Query failed returning error: '. mysqli_error());
} else {
   while($row=mysqli_fetch_array($sqlstackoverflow))
   {
    echo $row['username']. "<br>";
   }
}
?>

1 Comment

@scarface23 Thank you for the reply, but I will leave my answer as it is because sometimes this could be the solution.
0

Your code references an inventory table but you said your database has a products table (and no inventory table). Because of this, the query is failing.

1 Comment

What is your query that is wrong? Is there any errors that are being emitted?
0

Just Try With The Following :

<?php
$db = mysqli_connect("localhost","root","","prodpurchase");
if (!$db) {
die('Could not connect: ' . mysqli_error());
}
$sql = mysqli_query($db,"select * from user");
if( $sql === FALSE ) {
  die('Query failed returning error: '. mysqli_error());
} else {
   while($row=mysqli_fetch_array($sql,MYSQLI_ASSOC))
   {    
    echo $row['username']."<br>";    
   }
}
?>

I think this may help you to resolve your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.