how to avoi php warnings when identifying a empty SQL table?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bottleweb
Forum Newbie
Posts: 7
Joined: Tue Apr 22, 2008 11:23 am

how to avoi php warnings when identifying a empty SQL table?

Post by bottleweb »

Hello, I'm at wit's end with a php related problem and am hoping that someone here can help me. I'm sure the solution isn't very complicated, but I just can't think of it. Basically, I want to know if a query returns an empty result or not, without returning a php warning.

Code: Select all

SELECT item_id FROM content WHERE MATCH(item_title) AGAINST('$query_string' IN BOOLEAN MODE)
So basically I want to check the content table to see if $query_string exists in any of the item title's. If it does not, I want to run some code. Problem is, how do I do this query in php without getting a warning? Here's what I've been doing:

Code: Select all

$q = "SELECT item_id FROM content WHERE MATCH(item_title) AGAINST('$query_string' IN BOOLEAN MODE)";
 
$r = @mysqli_query ($dbc, $q);
 
//The following line causes the error
$query_result = mysqli_fetch_array($r, MYSQLI_ASSOC)
 
//this conditional checks if the result is or isnt null, thus identifying whether the query result was empty
if (sizeof($query_result) != 0) {
 
// result is not empty
} else {
// result is empty
}
 
 
(where $dbc is my database connection). When I run this, it works fine, but I get php warnings. Surely there's a better way of doing it, without getting warnings? Thanks in advance for any help.
Last edited by bottleweb on Sun Jun 01, 2008 12:35 pm, edited 1 time in total.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: how to avoi php warnings when identifying a empty SQL table?

Post by JAB Creations »

LoL I encounter that problem often and the same response I always get is to compare empty quotes...

Code: Select all

if ($my_data = '') {echo 'nothing in the quotes, there is no data';}
bottleweb
Forum Newbie
Posts: 7
Joined: Tue Apr 22, 2008 11:23 am

Re: how to avoi php warnings when identifying a empty SQL table?

Post by bottleweb »

Hmm, this doesn't seem to work. I think the problem is that $r isn't false because the query ran successfully (it just didn't find anything). And I can't run mysqli_fetch_array because it returns an error. Is there any way to convince mysqli_fetch_array not to give a warning when making it parse an empty result?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: how to avoi php warnings when identifying a empty SQL table?

Post by JAB Creations »

I'm not very experienced with MySQL though you could probably assign the query to a PHP variable and then have PHP execute the code so long as the variable is not blank. Give it a try or wait until the big guns show up to help you with their magic. :mrgreen:
bottleweb
Forum Newbie
Posts: 7
Joined: Tue Apr 22, 2008 11:23 am

Re: how to avoi php warnings when identifying a empty SQL table?

Post by bottleweb »

Ok i fixed the problem using the following conditional to test it:
if ( $r && ( sizeof(mysqli_fetch_array($r, MYSQLI_ASSOC)) != 0 ) )
I'm not 100% sure why this works, I guess it checks to see if $r is true before, well, checking if its true. just checking for $r doesn't work, I don't know why. Maybe I did something wrong but yeah this seems to work. Thanks for your help!
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: how to avoi php warnings when identifying a empty SQL table?

Post by JAB Creations »

Cool I helped someone with something I don't understand. :mrgreen: No problem though you should still consider other people's input because if there is one thing I've learned in PHP is that you can get something to work, or you can get something to work efficiently. So you should now see if there is a more efficient way to get this to work. Have fun! :)
Post Reply