Google Answers Logo
View Question
 
Q: Simple PHP / MySql -- must I call mysql_select_db every time? ( No Answer,   0 Comments )
Question  
Subject: Simple PHP / MySql -- must I call mysql_select_db every time?
Category: Computers > Programming
Asked by: cameront-ga
List Price: $4.00
Posted: 21 May 2004 02:02 PDT
Expires: 20 Jun 2004 02:02 PDT
Question ID: 349851
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 palitoy-ga on 21 May 2004 02:49 PDT
Have you tried doing this for the else statement:

while ($myrow = mysql_fetch_array($result)) {
    // display list if there are records to display
        printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF,
$myrow["id"], $myrow["first"], $myrow["last"]);
    };

Does this produce any results?  It would then be a simple matter of
counting the results and if this was zero stating there were no
errors.

Clarification of Question by cameront-ga on 21 May 2004 03:35 PDT
That suggestion wouldn't solve the problem, because the while loop
would never be entered.  By the time we're at the while loop, $result
is already empty, and so the fetch_array method is receiving nothing.

My question centers on why $result is always empty when executing the
query from inside the else block (unless I place mysql_select_db on
the line directly preceding the query execution).

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!

Request for Question Clarification by jeffyen-ga on 21 May 2004 06:45 PDT
I think you've controlled for the various conditions quite well, and
perhaps another Researcher can point out any obvious syntax errors.
The mysql_select_db command need only be run once with lots of
mysql_query following after it, I've coded that myself before. There's
also no issue with it being inside an 'else' block as can be seen at
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=499
. Could the confounding variable of our problem have to do with the
php or mysql setup that you're using since you mention that it works
for everyone else?
Answer  
There is no answer at this time.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy