Jump to content

New to PHP-MYSQL - Nothing showing?


sherri

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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"];

}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.