Page 1 of 1
Trouble with database query failing
Posted: Fri Sep 04, 2009 1:13 pm
by mosk
Hi. I've successfully selected and connected to my database with mysql_connect and mysql_select_db commands (the die on failure messages and errors weren't triggered).
When I try to perform my database query with
Code: Select all
<?php
$result = mysql_query("SELECT * FROM myTable", $connection);
if(!$result){
die("Database query really failed: " . mysql_error());
}
?>
I get my 'Database query really failed' message along with: Warning: mysql_query(): 5 is not a valid MySQL-Link resource in /homepages/37/d280082766/htdocs/Penczak/page3.php on line 24 (the $result = line)
I've tried this with several tables in the database and run into the same error (actually had mysql_query(): 6 error as well).
Can someone point me in the right direction to fix this?
Is there a php command I can add to to show the contents of my database so I can see what the page is seeing?
Note - I checked my query structure inside of PHPMyAdmin and that seemed to be right.
Thanks for any help.
Re: Trouble with database query failing
Posted: Fri Sep 04, 2009 2:06 pm
by jackpf
Where are you assigning $connection? Can you post the whole script?
Re: Trouble with database query failing
Posted: Fri Sep 04, 2009 3:54 pm
by mosk
Hey jackpf -
I had my database connection and a constants file in an includes folder, and I just worked out the problem though I don't quite understand the problem. Starting from scratch, however, I rebuilt my database connection and selection, and then my query - all of which worked fine. I then went back to using my includes connection and constants for the database and it still worked fine.
The problem only occurs if I put my header and footer includes BELOW my constants and connection includes. So it works fine like this:
Code: Select all
<?php require_once("includes/header.php");?>
<?php require_once("includes/footer.php");?>
<?php require_once("includes/functions.php");?>
<?php require_once("includes/form_functions.php");?>
<?php require_once("includes/constants.php");?>
<?php require_once("includes/connection.php");?>
but no good like this:
Code: Select all
<?php require_once("includes/functions.php");?>
<?php require_once("includes/form_functions.php");?>
<?php require_once("includes/constants.php");?>
<?php require_once("includes/connection.php");?>
<?php require_once("includes/header.php");?>
<?php require_once("includes/footer.php");?>
Rest of the code below:
Code: Select all
<?php //step 3 Perform database query
$result = mysql_query("SELECT * FROM myTable", $connection);
if(!$result){
die("Database query really failed: " . mysql_error());
}
if($result){
echo "Query worked <br />";
}
?>
<?php
// Step 4 Use returned data
echo "Here goes!! <br />";
while($row = mysql_fetch_array($result)){
echo $row[1]." ".$row[2]."<br />";
//echo $row["menu_name"]." ".$row["position"]."<br />";
}
?>
Re: Trouble with database query failing
Posted: Fri Sep 04, 2009 4:30 pm
by jackpf
You're probably using $connection in one of the scripts you're including, which is overwriting it...so it's no longer a valid mysql resource.
Have a look and see if you're using $connection in any of them scripts,
Re: Trouble with database query failing
Posted: Fri Sep 04, 2009 9:10 pm
by mosk
jackpf -
Thanks for the excellent troubleshooting.
I'd dabbled in php a few months ago, then took a break to learn some other programs, and I'd forgotten exactly how those included files work. As you suggested, I had a closeConnection function in one of my includes (my footer), and I'd placed all of my includes at the top of my document (as opposed to placing my footer at the bottom where it belonged). So I was basically closing my connection before running my query. Works much better with the footer down at the bottom.
Thanks again.
Re: Trouble with database query failing
Posted: Fri Sep 04, 2009 9:48 pm
by jackpf
Awesome. Nice spot.
