PHP mysql_query failure

lostcommander

Member
Messages
52
Reaction score
0
Points
6
Okay, I've been beating my head against this for too long and I don't see anything either wrong with my code or of use in searching the forums. I'm sorry as I'm sure this is either a typo or programmer error (e.g. mixing string concatenation . vs + earlier - wrong language, dur...) on my part.

$query = "SELECT * FROM table";
$dbconn = mysql_connect(localhost,$user,$password);
if (!$dbconn) {
die('DB connection failed: ' . mysql_error());
}
$selection = @mysql_select_db($database);
if (!$selection) {
die('DB selection failed: ' . mysql_error());
}
$result = mysql_query($query);
if (!$result) {
die('Query failed: ' . mysql_error());
}

When I do this, I get "Query failed"; however, there is no mysql error attached. The $database, $user, and $password are all correct, and correctly capitalized. :dunno:
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
250
Points
63
localhost needs to be in quotes: 'localhost'

Is your table named 'table" ? This is probably a problem. You're using a reserved word for a table name. You can probably get around it by quoting 'table' but table is a bad name regardless. It's not descriptive of what's in the table and it's already defined as something else.
 

lostcommander

Member
Messages
52
Reaction score
0
Points
6
Nope, my table is not "table". My user and password aren't named after themselves either. I know those weren't the trouble though.

Now, putting localhost in quotes. I mean, it makes sense in hindsight, but I never considered it, lol... *sigh* unfortunately, that does not seem to have fixed it. I am still getting that third error.
 

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
250
Points
63
I'm sure you thought of this, but just to make sure, does "table" exist in your database? Does "user" have select permissions on "table"? Is there any data in "table"?
 

lostcommander

Member
Messages
52
Reaction score
0
Points
6
This is TOTALLY the time to start asking me "is your computer plugged in?" questions, because I think it's pretty obvious I've forgotten something pretty basic...

Also, the last time I did this kind of stuff was about 3 years ago in PHP4, so if there were any major changes relevant to this particular section of code, that might be an issue. I've been reading up on PHP5 and have tried to reread the PHP5 manual section for each function I'm calling so as to make sure I find any crucial differences, but I might have missed something.

Using PHPMyAdmin from the cPanel page, I have verified that 'table' does indeed exist in my database and that it has 2 rows of data in it. The user, at present, is the original superuser with all privileges. For security I'll change that later to read-only, but for now I wanted to make debugging easier.
 

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
250
Points
63
Just to be sure, try doing this:
Code:
if($result === false) {
....

I don't think resources should ever evaluate to "false" but I know that strings will evaluate to either true or false based on their contents, so maybe this is a similar type of situation?
Edit:
Try this too, put an actual error into your query and see if that at least has output.
 
Last edited:

lostcommander

Member
Messages
52
Reaction score
0
Points
6
Okay, if I use:
if($result === false) {
die('Query failed: ' . mysql_error());
}
then the die statement does NOT get triggered and the page loads (albeit without the data it should have from MySQL).

Also, if I add:
echo($result);
echo(mysql_fetch_row($result));
then it produces: "Resource id #5" "Array"

BTW, thank you VERY much for your help!
 
Last edited:

lostcommander

Member
Messages
52
Reaction score
0
Points
6
And as soon as I took out some extra /* */ blocks, I get the data that IS supposed to be there! Spectacular, thank you very much!
 

xadrieth

New Member
Messages
62
Reaction score
1
Points
0
I know that the problem has been resolved, but i would recomend using the "mysqli_" functions, you'll fine that there easier to use and better.
 

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
250
Points
63
I know that the problem has been resolved, but i would recomend using the "mysqli_" functions, you'll fine that there easier to use and better.

True, but I think mysqli is currently disabled while the servers are being serviced :)
 
Top