Web Hosting Talk







View Full Version : MySQL 4 and PHP


JustinH
06-12-2003, 02:54 AM
Okay, so I have MySQL 4.0.12 and I've been doing some SELECT queries. Well, one of my queries, is SELECT table FROM database WHERE value='something';. The problem is, something isn't one of the available values and MySQL 4 is sending back "Empty set (0.00 sec)", and PHP isn't considering this an error. Anyone else have this problem?

The Prohacker
06-12-2003, 03:04 AM
Just to make sure we are on the same page and your query isn't messed up:

You said:
SELECT table FROM database WHERE value='something';

Do you mean:
SELECT info FROM table WHERE value='something';

??

JustinH
06-12-2003, 03:09 AM
Yeah that's what I meant (hehe late here). Here's the exact output:

mysql> SELECT password,salt FROM admin WHERE username='testasdfasdfsaf';
Empty set (0.00 sec)

The Prohacker
06-12-2003, 03:14 AM
When doing it in PHP are you checking it with an OR and then using mysql_error?

$var = mysql_query("SELECT password,salt FROM admin WHERE username=\"".$username."\"") or die(mysql_error());

JustinH
06-12-2003, 03:25 AM
Originally posted by The Prohacker
When doing it in PHP are you checking it with an OR and then using mysql_error?

$var = mysql_query("SELECT password,salt FROM admin WHERE username=\"".$username."\"") or die(mysql_error());

Yep. It is dieing, but doesn't produce any error (mysql_error() produces nothing). And the $var is empty as well. So technically it is considering it an error, but just doesn't give any information. Just seemed odd.

calvin
06-12-2003, 03:26 AM
I believe it is working correctly. If there isn't a username that matches "testasdfasdfsaf", it will return an empty set, which isn't an error. There are just no results that matched the query. Depending on what you are doing, you can use something like mysql_num_rows() to check how many results matched the query and if it is 0, print an error message or whatever.

JustinH
06-12-2003, 03:29 AM
Usually, you get a result of "0" I was thinking. The problem is, it does "die" when using or die();.

JustinH
06-12-2003, 03:56 AM
I'm guessing everything is just broken right now. I tried running a script that does:

print_r($_RESULT);

and set the URL to result.php?email=test&password=test and it doesn't print ANYTHING. Stupid crap.

seltice
06-14-2003, 02:18 AM
This is the way I always do my SQL statements. If there are errors in the SQL syntax or I reference a non-existant field, I do get an error, but there is almost never a situation where this kind of error would occur on a live site doing standard database operations.

$result = mysql_query("SELECT * FROM database WHERE field='value'");
if (mysql_num_rows($reuslt) > 0) {

$row = mysql_fetch_object($result);
print $row->field;

mysql_free_result($result);

} else {

print "What data, I see no data?";

}

JustinH
06-14-2003, 03:03 AM
Originally posted by seltice
This is the way I always do my SQL statements. If there are errors in the SQL syntax or I reference a non-existant field, I do get an error, but there is almost never a situation where this kind of error would occur on a live site doing standard database operations.

$result = mysql_query("SELECT * FROM database WHERE field='value'");
if (mysql_num_rows($reuslt) > 0) {

$row = mysql_fetch_object($result);
print $row->field;

mysql_free_result($result);

} else {

print "What data, I see no data?";

}

Yeah, I figured out where my problems started. Somehow MySQL 4 and the newest PHP aren't playing nice together :). Thanks for the suggestion.

SynHost
06-15-2003, 09:22 PM
MySQL returning empty set is not an error, its just returning zero rows. You need to use mysql_num_rows($result).

JustinH
06-15-2003, 09:32 PM
Originally posted by SynHost
MySQL returning empty set is not an error, its just returning zero rows. You need to use mysql_num_rows($result).

Yep. It is dieing, but doesn't produce any error (mysql_error() produces nothing).