Hello Everyone, I was trying to figure out why this particular mysql_query() doesn't return false and how to deal with it. First of all, I have a table (call it table1) with only one row in it. The one row has a 2 in the user_id column. So when I do

$theQuery="SELECT * FROM table1 WHERE 'user_id'='$numVar'";
if(!mysql_query($theQuery)){echo "SOMETHING!";echo "SOMETHING ELSE";}

the echo's dont work when $numVar does not match the only row in there with a 2 for user_id. I thought that when a query does not match anything it returns FALSE but instead it returns "resource" as per gettype() and "mysql result" from get_resource_type. When I print_r(mysql_fetch_array(mysql_query($theQuery),MYSQL_NUM)) it prints a 1. I thought that would print a 0 if anything for the first row but I don't understand what is going on here. Any insight as to what is happening or how to deal with it. I need to check the table to make sure that there is no row with user_id=$numVar in a boolean. I thought the code would work thinking that if there were no matching value in the specified column the mysql_query() would return FALSE. Any help is very much appreciated. I don't know what is even going on anymore.

Recommended Answers

All 4 Replies

try this:

$sql = "SELECT * FROM table1 WHERE table1.user_id = '" . $numVar . "'";
$query = mysql_query($sql);
if (!$query) {
echo 'Query NOT ok';
}
else {
echo 'Query ok';
}

i think you might of had some syntax errors in your code and thats why it didn't work.

Hey, thanks. I gave it a shot and it worked. Yeah, there probably was some syntax error that wasn't working the way I had planned, but this way is a lot better anyhow. Now I can just call the var instead of having a function evaluated. Thanx a million.

sorry i guess i am not following. what variable are you wanting to call?

Well, this is what I am trying to do. From the table, it contains shipping information like street city state zipcode and other contact info. When the current user does not exist, meaning the query doesnt find any rows matching the user_id value with the variable that I am looking for e.g. $numVar, I want to use that boolean for if statements. This is the basic layout that I am working with:

$isInReg="SELECT * FROM table1 WHERE table1.user_id = '".$numVar."'";
$inRegQuery=mysql_query($isInReg);
if($inRegQuery){
$regData="mysql_fetch_array(inRegQuery)";
/* DISPLAY DATA FROM TABLE AND DISPLAY LINK TO $_SERVER[PHP_SELF]."?update=true" */
}

if(!$inRegQuery){
/* echo a form to use to enter required fields method=post submit to $_SERVER[PHP_SELF]."?confirmData=true" */
}
if(!$inRegQuery && isset($_GET[confirmData])){
/* USER NOT IN TABLE, CREATE QUERY FROM $_POST VARIABLES AFTER CHECKING TO INSERT NEW ROW INTO TABLE */
}
if($inRegQuery && isset($_GET[update])){
/* echo a form to use to change data with current values from the table method post and submit to $_SERVER[PHP_SELF]."?confirmUpdate=true" */
}
if($inRegQuery && isset($_GET[confirmUpdate])){
/* CHECK FIELDS FROM $_POST AND UPDATE THE ROW */
}

WEll, that is in fact what I am trying to accomplish, but the query doesn't seem to work in all the cases. It does not work when evaluating !$inRegQuery or at least none of the instructions are being executed. At this point I have just been using echo to see what is happening and so it seems that when no row has table1.user_id = $numVar it still returns something other than false but print_r prints nothing when I call mysql_fetch_array($inRegQuery,MYSQL_ASSOC). I don't know enough about how they deal with stuff but I based all of this on the fact that $inRegQuery returns false if it does not find any rows matching the value. It does not, otherwise the !$inRegQuery echo statements should be executing. Any ideas?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.