2

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

?>
2
  • 3
    You are using an obsolete database API and should use a modern replacement. Commented Apr 22, 2013 at 10:34
  • Check for record count like this: if (mysql_num_rows($records) ==0) Commented Apr 22, 2013 at 10:35

4 Answers 4

11

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.

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

4 Comments

@user2306964 - Have edited with enough information to get it working using the right libraries.
@user2306964 What is NOT working? Perhaps you should try echoing an error. For example, include or die(mysql_error()); to the end of the connection and query statements like this: mysql_query(...) or die( mysql_error()); At least then you know that the statement run.
@user2306964 Also it's best you change to mysqli like Oshawott has shown.
@user2306964 - Is this still not working for you? I just basically rewrote your program from scratch.
1

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

1 Comment

WHAT is the RIGHT way then?
0

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

Comments

0

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

?>  

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.