Jump to content



Photo

Php Mysql Select Var From Select Form

php mysql select var

  • Please log in to reply
4 replies to this topic

#1 fishswim

fishswim

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 24 March 2014 - 4:26 PM

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> ';

?>
 

  • 0

#2 HartleySan

HartleySan

    Advanced Member

  • Members
  • PipPipPip
  • 3042 posts
  • LocationColumbus, OH USA

Posted 24 March 2014 - 10:02 PM

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.


  • 1

#3 fishswim

fishswim

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 6 April 2014 - 9:58 AM

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.


  • 0

#4 HartleySan

HartleySan

    Advanced Member

  • Members
  • PipPipPip
  • 3042 posts
  • LocationColumbus, OH USA

Posted 6 April 2014 - 10:37 AM

Glad it worked.

Just FYI though: Regexes are pretty slow, so try to avoid them when you can.


  • 0

#5 Thorcoft

Thorcoft

    Newbie

  • Members
  • Pip
  • 5 posts
  • LocationMoscow

Posted 25 April 2016 - 4:14 AM

You can use the PHP fputcsv function to generate CSV files from array. The array can be created via mysql_query and mysql_fetch_assoc functions. If you need more help, let me know.
  • 0