Jump to content
Larry Ullman's Book Forums

Php Mysql Select Var From Select Form


Recommended Posts

I am working with PHP Third edition and MySQL second edition - both show php mysql scripts.

 

working with PHP for the web, third edition, page 373, modify the script to accept value from form field 

and then and to receive data from database.

 

I have re worked this many times over and am frustrated to the point of brain freeze.

 

what can i do to receive the business name associated with zip code in database.

any assistance ? thank you.

<?php
if ( isset($_POST['submitted']) ) {

   $baz = $_POST['zip'];

   $dbc = mysql_connect('localhost', 'usr_name', 'password');
   mysql_select_db('data_base_name');

//$query = 'SELECT biz_name FROM retail_client WHERE zip_code = 99201';
//$query = 'SELECT biz_name FROM retail_client WHERE zip_code = \'$baz\' ';


// error message: mysql_query() expects parameter 2 to be resource,


if($r = mysql_query('$dbc' 'SELECT biz_name FROM retail_client WHERE zip_code = 99201')) {

while ($row = mysql_fetch_array($r) {

print "<p> {$row['biz_name']} </p>";

} // while
} // two
} //ONE 

print ' <form action="this_script.php" method="post">

<p>Zip code:
 
<input type="text" name="zip" size="10" /></p>

<input type="submit" name="submit" value="go" />

<input type="hidden" name="submitted" value="true" />
 
</form> ';

?>
 
Link to comment
Share on other sites

Two things I would try:

  1. Using the MySQLi (notice the i) family of functions instead of the MySQL family of functions. The MySQL family of functions are now pretty out of date.
  2. Trying the following query on the DB directly, and seeing if it works:
SELECT biz_name FROM retail_client WHERE zip_code = 99201;

Please let us know what happens after you try those two things.

Thanks.

  • Upvote 1
Link to comment
Share on other sites

  • 2 weeks later...

Yes with a large exclamation mark.

Migrating with the bison. yet i am a little slow. i was really freaking out on that script.

it has been two years since i was fully into coding, been working the graveyard shift, learned my lesson.

this works also, WHERE column_name = REGEXP ' ';

Thanks for the reply.

Link to comment
Share on other sites

  • 2 years later...
  • 1 year later...

Now we all know this sort of query to get some simple from the Database:

 

includeconnect.php;

query = "SELECT FROM table WHERE name = Apache";

result = mysql_queryquery;

 

whilerow = mysql_fetch_assocresult

echo rowname;

 

NOw this is a great way, but imagin you had 20 pieces of data, and dont want to write row every time. Well here the solution

 

includeconnect.php;

query = "SELECT FROM table WHERE name = Apache";

result = mysql_queryquery;

 

whilerow = mysql_fetch_assocresult

extractrow;

echo name;

 

This simple line of code pulls the row out of the variablkes, so now you can call on them using the name of the mysql row, without that nasty row

Link to comment
Share on other sites

 Share

×
×
  • Create New...