sherri Posted July 9, 2008 Share Posted July 9, 2008 I am new to php/mysql and am trying to do a search on a database by zip code. I am not getting anything on my search.php. I know I ma missing something but not sure what it is. Here is my site http://www.fixmyplane.net/ And here is my code I am using. The search is <h2>Search by Zip Code</h2> <form method="post" action="search.php"> <input type="text" name="search" size=25 maxlength=25> <input type="Submit" name="Submit" value="Submit"> </form> The php code is <? //connect to mysql //change user and password to your mySQL name and password mysql_connect("removed"); //select which database you want to edit mysql_select_db("removed"); $search=$_POST["search"]; //get the mysql and store them in $result //change whatevertable to the mysql table you're using //change whatevercolumn to the column in the table you want to search $result = mysql_query("SELECT * FROM `air09` ORDER BY `air09`.`zip` ASC LIMIT 0 , 30 ") or die ('Error: '.mysql_error ()); //grab all the content while($r=mysql_fetch_array($result)) { //the format is $variable = $r["nameofmysqlcolumn"]; //modify these to match your mysql table columns $cert_no=$r["Cert No"]; $first_name=$r["First Name"]; $last_name=$r["Last Name"]; $address=$r["Address"]; $address_2=$r["Address 2"]; $city=$r["City"]; $state=$r["State"]; $zip=$r["Zip"]; //display the row echo "$cert_no <br> $first_name $last_name <br> $address <br> $adress2 <br> $city $state $zip <br >"; } ?> What am I doing wrong. This is the first of other searches I need to build. Database structure cert_no text latin1_swedish_ci No first_name text latin1_swedish_ci No last_name text latin1_swedish_ci No address text latin1_swedish_ci No address_2 text latin1_swedish_ci Yes NULL city text latin1_swedish_ci No state text latin1_swedish_ci No zip text latin1_swedish_ci Any help would be great. Sherri Quote Link to comment https://forums.phpfreaks.com/topic/113965-new-to-php-mysql-nothing-showing/ Share on other sites More sharing options...
matthewhaworth Posted July 9, 2008 Share Posted July 9, 2008 mysql_error() not mysql_error () I'm still looking though, just in case.. btw.. you need to put more <br />'s in your echo, pressing enter doesn't actually put them on the lower line.. <?php //connect to mysql //change user and password to your mySQL name and password mysql_connect("localhost", "username", "password"); //select which database you want to edit mysql_select_db("database"); $search=$_POST["search"]; //get the mysql and store them in $result //change whatevertable to the mysql table you're using //change whatevercolumn to the column in the table you want to search $result = mysql_query("SELECT * FROM `air09` ORDER BY `air09`.`zip` ASC LIMIT 0 , 30 ") or die('Error: '.mysql_error()); //grab all the content while($r = mysql_fetch_array($result)) { echo $r["Cert No"] . '<br />'; echo $r["First Name"]. ' '; echo $r["Last Name"] . '<br />'; echo $r["Address"] . '<br />'; echo $r["Address 2"] . '<br />'; echo $r["City"] . '<br />'; echo $r["State"] . '<br />'; echo $r["Zip"]; } ?> Try that. I've just realised that your DB doesn't match up with your array. Cert No = Cert_No Quote Link to comment https://forums.phpfreaks.com/topic/113965-new-to-php-mysql-nothing-showing/#findComment-585722 Share on other sites More sharing options...
revraz Posted July 9, 2008 Share Posted July 9, 2008 If you show errors, then it will tell you which line has the errors instead of just a blank screen. Quote Link to comment https://forums.phpfreaks.com/topic/113965-new-to-php-mysql-nothing-showing/#findComment-585738 Share on other sites More sharing options...
matthewhaworth Posted July 9, 2008 Share Posted July 9, 2008 <?php if (!(mysql_connect("localhost", "username", "password"))) { exit('Connection unsuccessful' . mysql_error()); } if (!(mysql_select_db("database"))) { exit('Could not connect to database' . mysql_error()); } $search = $_POST["search"]; // I don't know where you use this? //get the mysql and store them in $result //change whatevertable to the mysql table you're using //change whatevercolumn to the column in the table you want to search $result = mysql_query("SELECT * FROM `air09` ORDER BY `air09`.`zip` ASC LIMIT 0, 30") or exit('Error: '.mysql_error()); //grab all the content while($r = mysql_fetch_array($result)) { echo $r["cert_no"]; echo '<br />'; echo $r["first_name"]; echo ' '; echo $r["last_name"]; echo '<br />'; echo $r["address"]; echo '<br />'; echo $r["address_2"]; echo '<br />'; echo $r["city"]; echo '<br />'; echo $r["state"]; echo '<br />'; echo $r["zip"]; } ?> Try that.. Edit: I've updated this a few times.. I've taken into account craygo's post below. Quote Link to comment https://forums.phpfreaks.com/topic/113965-new-to-php-mysql-nothing-showing/#findComment-585744 Share on other sites More sharing options...
craygo Posted July 9, 2008 Share Posted July 9, 2008 Also your database may be case sensitive. I always keep my fields in lower case and make sure I match up all field names. echo $r["cert_no"] . '<br />'; echo $r["first_name"]. ' '; echo $r["last_name"] . '<br />'; echo $r["address"] . '<br />'; echo $r["address_2"] . '<br />'; echo $r["city"] . '<br />'; echo $r["state"] . '<br />'; echo $r["zip"]; Ray Quote Link to comment https://forums.phpfreaks.com/topic/113965-new-to-php-mysql-nothing-showing/#findComment-585747 Share on other sites More sharing options...
sherri Posted July 9, 2008 Author Share Posted July 9, 2008 Parse error: syntax error, unexpected '{' in /misc/20/106/627/392/3/user/web/fixmyplane.net/search.php on line 74 Did I leave something out. <? if (!(mysql_connect("sql2.bravehost.com", "mecfix", "password")) { exit('Connection unsuccessful' . mysql_error()); } if (!(mysql_select_db("airmechan-820133"))) { exit('Could not connect to database' . mysql_error()); } $search = $_POST["search"]; // I don't know where you use this? //get the mysql and store them in $result //change whatevertable to the mysql table you're using //change whatevercolumn to the column in the table you want to search $result = mysql_query("SELECT * FROM `air09` ORDER BY `air09`.`zip` ASC LIMIT 0 , 30 ") or exit('Error: '.mysql_error()); //grab all the content while($r = mysql_fetch_array($result)) { echo $r["Cert_No"]; echo '<br />'; echo $r["First_Name"]; echo ' '; echo $r["Last_Name"]; echo '<br />'; echo $r["Address"]; echo '<br />'; echo $r["Address_2"]; echo '<br />'; echo $r["City"]; echo '<br />'; echo $r["State"]; echo '<br />'; echo $r["Zip"]; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/113965-new-to-php-mysql-nothing-showing/#findComment-585756 Share on other sites More sharing options...
matthewhaworth Posted July 9, 2008 Share Posted July 9, 2008 There is no line 74.. have you posted the whole code? Plus, please use [ code ] tags when posting php, click the '#' in the Post Reply toolbox. Also <?php echo ' '; ?> Will not move the text onto a new line lol. Use br as in the code I showed you. Edit: Well, actually, apparently it will.. lol I learn something new every day. Quote Link to comment https://forums.phpfreaks.com/topic/113965-new-to-php-mysql-nothing-showing/#findComment-585758 Share on other sites More sharing options...
revraz Posted July 9, 2008 Share Posted July 9, 2008 I don't even see 74 lines of code. Quote Link to comment https://forums.phpfreaks.com/topic/113965-new-to-php-mysql-nothing-showing/#findComment-585761 Share on other sites More sharing options...
craygo Posted July 9, 2008 Share Posted July 9, 2008 first thing I see is the ! in front of the parenthesis. remove the parenthesis. if (!mysql_connect("sql2.bravehost.com", "mecfix", "password")) Ray Quote Link to comment https://forums.phpfreaks.com/topic/113965-new-to-php-mysql-nothing-showing/#findComment-585763 Share on other sites More sharing options...
matthewhaworth Posted July 9, 2008 Share Posted July 9, 2008 <?php // Connect to MySQL. if (!mysql_connect("localhost", "username", "password")) { exit('Connection unsuccessful' . mysql_error()); } // Connect to the database. if (!mysql_select_db("database")) { exit('Could not connect to database' . mysql_error()); } // Query the database for records with zip = to what the user posted. $result = mysql_query("SELECT * FROM `air09` WHERE zip = '" . $_POST["search"] . "' ORDER BY `air09`.`zip` ASC LIMIT 0, 30") or exit('Error: '.mysql_error()); // Output the results of the query. while($r = mysql_fetch_array($result)) { echo $r["cert_no"]; echo '<br />'; echo $r["first_name"]; echo ' '; echo $r["last_name"]; echo '<br />'; echo $r["address"]; echo '<br />'; echo $r["address_2"]; echo '<br />'; echo $r["city"]; echo '<br />'; echo $r["state"]; echo '<br />'; echo $r["zip"]; echo '<br />'; } ?> Here, I saw what you were trying to do.. with the query.. (I think).. so I added it. Also, I improved what others said was wrong. Quote Link to comment https://forums.phpfreaks.com/topic/113965-new-to-php-mysql-nothing-showing/#findComment-585773 Share on other sites More sharing options...
sherri Posted July 9, 2008 Author Share Posted July 9, 2008 still nothing? http://www.fixmyplane.net/search.php Quote Link to comment https://forums.phpfreaks.com/topic/113965-new-to-php-mysql-nothing-showing/#findComment-585776 Share on other sites More sharing options...
matthewhaworth Posted July 9, 2008 Share Posted July 9, 2008 still nothing? http://www.fixmyplane.net/search.php It works for me, just replace the \n with <br />.. lol, i keep making stupid mistakes like that.. i have PHP flu lol. Quote Link to comment https://forums.phpfreaks.com/topic/113965-new-to-php-mysql-nothing-showing/#findComment-585779 Share on other sites More sharing options...
sherri Posted July 9, 2008 Author Share Posted July 9, 2008 replace the \n with what? I dont see it? What do you see? What zip code did you use? sherri Quote Link to comment https://forums.phpfreaks.com/topic/113965-new-to-php-mysql-nothing-showing/#findComment-585786 Share on other sites More sharing options...
matthewhaworth Posted July 9, 2008 Share Posted July 9, 2008 replace the \n with what? I dont see it? What do you see? What zip code did you use? sherri With <br />. I just clicked your link and it loaded a couple.. Quote Link to comment https://forums.phpfreaks.com/topic/113965-new-to-php-mysql-nothing-showing/#findComment-585803 Share on other sites More sharing options...
sherri Posted July 10, 2008 Author Share Posted July 10, 2008 Thank you sherri Quote Link to comment https://forums.phpfreaks.com/topic/113965-new-to-php-mysql-nothing-showing/#findComment-586849 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.