Ok, this shouldn't be but a 2 minute response from somebody in the
know. I'm going through a php/mysql tutorial on webmonkey, and I ran
into a problem that I feel might have to do with my php.ini, but I'm
really not sure, as this code apparently works fine for everyone else.
At "* Location 2 *" in my script is the line:
$result = mysql_query("SELECT * FROM employees", $db);
This returns nothing into result. If I add an 'or die' with the mysql
error it tells me that it lost the connection to the mysql server.
I can fix this error using one of two methods:
1) Insert the following line at *Location 2*, before i execute the query.
mysql_select_db("employeedb",$db);
2) Move the line below *Location 2* (the query execution) to *Location 1*
I'm thouroughly confused by this. Why doesn't the database selection
I make at the top of the script persist for my assignment to '$result'
at *Location 2*?
What confuses me even more is that if I copy the query at *Location 2*
to a place below (and outside) the if-else statement (say, into a
variable called '$result2'), everything is fine, which means that it's
not likely some timeout issue. It seems that in my assignemnt into
'$result' (at *Location 2*), my script simply 'forgets' that I've
already made a database selection... very frustrating. Does the fact
that it's within an else-block have anything to do with it?
If my php.ini would help out, I'd be happy to post it.
********************** BEGIN SCRIPT ***************************
<html>
<body>
<?php
$db = mysql_connect("vergil.u.washington.edu:3232", "root", "some password");
mysql_select_db("employeedb",$db);
// *LOCATION 1*
// If this is the first time this script has been called,
// $id will be undefined.
// Otherwise, display the individual record referred to by $id
if ($id) {
$result = mysql_query("SELECT * FROM employees WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
printf("First name: %s\n<br>", $myrow["first"]);
printf("Last name: %s\n<br>", $myrow["last"]);
printf("Address: %s\n<br>", $myrow["address"]);
printf("Position: %s\n<br>", $myrow["position"]);
} else {
// Show the entire employee list
$result = mysql_query("SELECT * FROM employees", $db);
// *LOCATION 2*
if ($myrow = mysql_fetch_array($result)) {
// display list if there are records to display
do {
printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF,
$myrow["id"], $myrow["first"], $myrow["last"]);
} while ($myrow = mysql_fetch_array($result));
} else {
// no records to display
echo "Sorry, no records were found!";
}
}
?>
</body>
</html>
************************* END SCRIPT ***************************
p.s. This script is constantly generating "sorry, no records were
found" since I don't have the 'or die' in there, but hopefully that's
obvious. |
Request for Question Clarification by
jeffyen-ga
on
21 May 2004 04:59 PDT
I see what you're trying to say cameront, and this problem is most
intriguing (unless we really miss out some obvious typo with the code
or something!)
If I were you, I'd like to be absolutely sure that I'm able to
replicate fix(1), i.e., there really is a difference between putting
mysql_select_db below '//show the entire employee list' and not puttin
that line in. The routine doesn't actually enter the 'if ($id)'
section, right?
Also, try taking the 'if ($'id)' section completely away and just
leave the 'else' section intact.
|
Clarification of Question by
cameront-ga
on
21 May 2004 05:44 PDT
in trying to diagnose the problem, I actually did both.
I made sure that the (top level) 'if' block wasn't being executed.
this should have been obvious enough to me, being that "sorry, no
records were found" was displayed, but I put echoes within the "if
($id)" block just to make sure. the result: I'm positive that it's
not executing that top block.
as for removing the if portion of the if-else statment, that inspires
the code to work correctly and list the contents of my employee table.
that's what I don't understand, it seems that the mysql_query is
evaluated in a different context when inside the else block.
what's driving me crazy, though, is that I can remove the 'if' block,
and simply replace the else statement with something trivial, like 'if
(true) {' and it executes correctly, so it's certainly not a problem
that it's inside of a block. is there some special property of the
else block that could be causing this behavior?
|
Clarification of Question by
cameront-ga
on
21 May 2004 05:58 PDT
sorry - i made a slight mistake in asking the question, which nobody
seems to be messed up by, but let me clarify.
in my question, I claim that the query execution is below the line
'//*Location 2*'. if you actually look at the script, the query
execution:
$result = mysql_query("SELECT * FROM employees", $db);
happens just above that 'Location 2.'
sorry again for the mistake, just imagine that '*Location 2*' is
directly above the assignment statement to $result. yikes!
|