2

I'm using SQLite3 and PHP to create a little class, this means I don't want any plugins or other classes, just plain SQLite3 and PHP.

Currently I'm trying to retrieve values from a database, but I want to check if there is a result or not with that conditions:

$select = $db->query("SELECT * FROM views WHERE address = '$ip' AND page = ''");

After some search I couln't find nothing specific, but I think I'm missing it.

First I tried:

   if($select){
        // result returned
   }else{
        // no results
   }

And then this solution worked(the previous always failed):

while($row = $select->fetchArray(SQLITE3_ASSOC) ){
    $exists = TRUE;
    break;
}

if($exists == TRUE){
    // result returned
}else{
    // no results
}

But this seems to be, tricky... is there a way to check if a select query returned something or nothing?

3 Answers 3

5

You can chek it simple in "runtime":

$emptyArr  = array();
$filledArr = array(1,2,3,4,5);

if ($emptyArr) {
    echo 'Empty array is not empty!';
}
if (!$filledArr) {
    echo 'Filled array is empty!';
}

Because this: http://php.net/manual/ru/sqlite3result.fetcharray.php Will return filled array or false. Then this is equal:

$arr = array();
var_dump((false == $arr)); // true

And special for all dizzzlikers! Incredible, sweet, honey solution:

if ($res = $select->fetchArray(SQLITE3_ASSOC)) {
    foreach ($res as $item) {
        // output
    }
} else {
    // nothing
}
Sign up to request clarification or add additional context in comments.

6 Comments

Wouldn't using the !$filledArray give a PHP Warning if error reporting is on? You can only do that with booleans.
@Jake And you try it for arrays? )))
Arrays are handled like variables, so it'd be handled the same way. Both ways you showed are wrong as they're checking if they're true or false which is not something you should do with a variable unless it's set as a boolean.
I tried that already, that would work if $select was a normal array. But in this case is a SQLite3 Object and not array, basically, doesn't work that way. But thanks for your reply
@rafaelmsantos When you use fetch style - you known return type
|
0

Couldn't you take the variables and could use isset or empty

if(!empty($var)){
   // Variable is not empty and is set
}

if(isset($var)){
    // Variable is set, may be empty though.
}

if(empty($var)){
    // Variable is not set or is empty.
}

if(!isset($var)){
    // Variable is not set.
}

This would also work for arrays and array keys.

7 Comments

Fail: $var = 0; var_dump(empty($var)); // true
@Deep Empty checks if it is empty and set. 0 would correlate with false, which is why it says it is empty.
$select returns and SQLite3 Object( ), and this doesn't work as arrays.
More fail: $var = null; var_dump(isset($var)); // false
@Deep It isn't set? Null != set... are you kidding me?
|
0

Theres a function called *->numRows();

http://php.net/manual/en/function.sqlite-num-rows.php

that returns the number of rows fetched from a database. I don't use sqlite but I'm pretty sure a dbquery returns an array of values from the database (well, in this case an object) along with a true/false if it was successful or not.

Try:

$select = $db->query("SELECT * FROM views WHERE address = '$ip' AND page = ''");



$rows = $select->numRows();

echo $rows;  //returns # of rows

or    

if ($rows){
//return anything but 0 would eval true..
}

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.