0

I am trying to write a very simple query in php.

$conn = createConnection();
$sql = "select count(username) from passenger where username = '$passengerId' and password ='$password'";
$result = mysql_query($sql);
$count = mysql_result($result, 0);

echo $count;
if ($count == 1)
    echo "true == 1";
else 
    echo "false != 1";

echo print_r(mysql_result($result, 0));
echo $count;

at the end, echo print_r(mysql_result($result, 0)) gives me 1 as expected, but echo $count does not print anything. Also the if statement give me false != 1. I am not sure what kind of problem occurs here.

4
  • To be sure what's really there use echo "<pre>[$count]</pre>"; or var_dump($count); instead of just echo $count;, you might get spaces, or other things. Commented Jan 30, 2015 at 12:25
  • var_dump($count); give me NULL. Do you know the reason? Commented Jan 30, 2015 at 12:34
  • use mysqli and enable error reporting. Commented Jan 30, 2015 at 12:46
  • @user3764319: Probably an error in your SQL command. Commented Jan 30, 2015 at 19:48

3 Answers 3

1

Try this:

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="select count(username) as totalUser from passenger where username = '$passengerId' and password ='$password'";
$result=mysqli_query($con,$sql);

// Associative array
$row=mysqli_fetch_assoc($result);
printf ("%s",$row["totalUser"]);

// Free result set
mysqli_free_result($result);

mysqli_close($con);
?>

See, if that solves your problem.

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

6 Comments

Is your sql query working fine? try that, see if you're getting any count value, even check values which you are passing in your where condition are available.
i think there might a database connection problem. is mysqli ok to create a connection?
Yep! Even you should use mysqli, with that check if your phpmyadmin is working
no it's not connection problem otherwise it would give Warning: mysql_connect(): Access denied or Warning: mysql_result() expects parameter 1 to be resource
Updated my answer, check now!
|
1

print_r will always return 1 when TRUE.

For example

print_r(mysql_result($result, 0)); 
// Equals this 
print_r(TRUE);  

In above case both will return 1 and 0 for FALSE

But In your Case you use echo and print_r together which is also wrong. Basically your query not able to find any record from DB.

echo print_r(0); //will always return 01 use either echo or print_r

Comments

0

The reason is count will return either 0 or any integer value but the result set will always be there because either the row of result set will have any value or zero but the row would exist

Iterate over the result set and see if it is 0 or any integer value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.