vish
vish

Reputation: 165

mysql_query not returning data

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);

?>

Upvotes: 2

Views: 2371

Answers (4)

Ravindra Shekhawat
Ravindra Shekhawat

Reputation: 4353

Lets resolve this issue first.The error it was actually showing is no database selected you have to select the database that needs the code

mysql_select_db("Assign1",$conn);

Hope this code will perfectly sole your issue .Try it once .........

 <?php
$conn = mysql_connect("localhost", "root", "pass");
mysql_select_db("Assign1",$conn);
$result = mysql_query("select * from users", $conn);
if(!$result)
{
    echo "No Records Found";
    exit();
}
while($row = mysql_fetch_array($result))
{
  echo $row[0]['name'];
  echo "<br />";
}
mysql_close($conn);
?>

Upvotes: 0

echo_Me
echo_Me

Reputation: 37233

here you go

 <?php

 $conn = mysql_connect("localhost", "root", "pass", "Assign1");
 mysql_select_db(' ----your-database-here---', $conn ) ;
 $records = mysql_query($conn, "select * from Users");

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


 }else
{
echo "No Records Found";
exit();
}



mysql_close($conn);

?>  

Upvotes: 0

Glitch Desire
Glitch Desire

Reputation: 15023

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.

Upvotes: 11

Alex
Alex

Reputation: 2146

You have a wrong usage of mysql_query function

use it like this:

 <?php
$conn = mysql_connect("localhost", "root", "pass","Assign1");
$result = mysql_query("select * from Users", $conn);
if(!$records)
{
echo "No Records Found";
exit();
}
while($row = mysql_fetch_array($result))
{
  echo $row['name'] . " " . $row['pwd'];
  echo "<br />";
}
mysql_close($conn);
?>

Upvotes: 1

Related Questions