Skip to content
Advertisement

Check empty select in sqlite?

How can i check the result of my query? I try this, but when the result is empty – error doesn’t shows.

$result = sqlite_query($db, "SELECT * FROM catalog WHERE cat=".$_GET["cat"]." AND size".$size.""); 
        if($result !== 0) {
            while ($row = sqlite_fetch_array($result)) { 
                echo '<img src="'.$row['img1'].'" alt="'.$row['id'].'" />'."rn"; 
            } 
        } else {
            err2();
        }

Advertisement

Answer

Note that sqlite_fetch_array() returns a result handle, not the number of rows, so you should not compare it against zero:

This function will return a result handle or FALSE on failure.

Use sqlite_num_rows() to determine the number of rows returned, e.g.

if(sqlite_num_rows($result) !== 0) {
  ...
} else {
  ...
}

Note that you should not use user-provided data by directly incorporating it in the SQL query as that allows for SQL injection attacks. Also, make sure that the contents of your database, particularly values in columns img1 and id are properly escaped or you risk being vulnerable to XSS attacks.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement