Reputation: 282
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.
Upvotes: 0
Views: 2242
Reputation: 2928
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.
Upvotes: 0
Reputation: 388
Did you check the letters uppercase and lowercase in table columns?
Upvotes: 1
Reputation: 3972
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.
Upvotes: 0
Reputation: 2344
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>";
}
}
?>
Upvotes: 0
Reputation: 595
You have inventory in your code, but in your question you named the table products, Is it this simple?
Upvotes: 1
Reputation: 612
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.
Upvotes: 0