Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql_query not returning data

Tags:

php

mysql

The table Users contains data but still it shows Records Not Found

<?php

$conn = mysql_connect("localhost", "root", "pass", "Assign1");
$records = mysql_query($conn, "select * from Users");

if(!$records)
{
    echo "No Records Found";
    exit();
}

while($row = mysql_fetch_array($records))
{
    echo $row['name'] . " " . $row['pwd'];
    echo "<br />";
}

mysql_close($conn);

?>
like image 237
vish Avatar asked Jun 13 '26 01:06

vish


1 Answers

You have the parameters to mysql_query reversed. It should be:

$records = mysql_query("select * from Users", $conn);

Your other issue is with the if statement. You're checking if on a query, not on a result set.

Also, I'm sure you probably know but mysql libraries are deprecated and are being removed. You should really learn to use mysqli functions as they will be far more useful to you in the future.

Link to MySQLi documentation - It's really no harder than mysql libraries.

To re-implement in correct libraries:

<?php

$mysqli = new mysqli("localhost", "user", "pass", "database");
$query = $mysqli->query("SELECT * FROM users");
$results = $query->fetch_assoc();

if($results) {
    foreach($results as $row) {
        echo $row['name'] . " " . $row['pwd'] . "<br/>";
    }
} else {
    echo "No results found.";
}

?>

Hopefully I didn't just do your whole assignment for you, but it'd probably be worth it to get one more person using mysqli properly.

like image 184
Glitch Desire Avatar answered Jun 14 '26 16:06

Glitch Desire



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!