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

?>
franzlorenzon
  • 5,845
  • 6
  • 36
  • 58
vish
  • 165
  • 2
  • 13
  • 3
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Apr 22 '13 at 10:34
  • Check for record count like this: if (mysql_num_rows($records) ==0) – itsols Apr 22 '13 at 10:35

4 Answers4

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.

Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
  • @user2306964 - Have edited with enough information to get it working using the right libraries. – Glitch Desire Apr 22 '13 at 10:38
  • @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. – itsols Apr 22 '13 at 10:41
  • 1
    @user2306964 Also it's best you change to mysqli like **Oshawott** has shown. – itsols Apr 22 '13 at 10:42
  • @user2306964 - Is this still not working for you? I just basically rewrote your program from scratch. – Glitch Desire Apr 22 '13 at 10:56
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);
?>
Alex
  • 2,126
  • 3
  • 25
  • 47
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);

?>  
echo_Me
  • 37,078
  • 5
  • 58
  • 78
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);
?>
Ravindra Shekhawat
  • 4,275
  • 1
  • 19
  • 26