0

My array would only get a 0 or a 1. I'm using PDO statement for mysql, and this is what I have

$result = $fetching -> fetch();
$first_result = $result[0];

if($first_result === 0){
//do something...
}else{
echo "testing";
}

I'm always getting the testing no matter what, if its 0 or not. I've echoed out the $first_result[0] for testing purposes and it will tell me what the value is. So I know that the value will shoot out either 0 or not 0. But when it is 0, it still echos out testing.

Help please?

Thank you!

1
  • 1
    Have you done a var_dump() to ensure the types match? Remember === compares type and value. Commented Apr 17, 2012 at 5:26

3 Answers 3

3

First of all try to put == not type equivalence. And Second I always use this to check if there any record:

$q = $pdo->prepare('Your SQL');
$result = $q->execute($params);
$rows = $q->fetchAll(PDO::FETCH_ASSOC);
if (!count($rows) || !$result)
{
    echo 'No rows or error';
    // To output error $pdo->errorInfo();
}
else
{
    $el = $row[0];
}

Hope that helps.

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

2 Comments

would fetchAll be slow if the database gets extremely large?
Depends on what do you mean saying extremely large? If you expect really large number of rows, it's a good practice to limit and making partition for your queries. I any case if you want to select all at once, there always would be a hard-ware limitations. So, the partition is the way to go.
0

The === is testing for type, pdo will return a string in that case so you are testing an int against a string.

Try:

if($first_result === '0'){

Comments

0

Seems your types are not matching. === matches value and type

PHP’s === Operator enables you to compare or test variables for both equality and type. Refer below for example.

Read More...

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.