MySQL PHP

From KOP KB
Revision as of 22:30, 4 August 2014 by ReMaster (talk | contribs)
Jump to: navigation, search

This is really just one example but its one way you can display data. This one just happens to be one where it took a post from a form for like a zip code and then checked related zip codes in a a database.

<?
//connection information

 $host = 'hostname';

    $user = 'username';

    $pass = 'pass';

    $db = 'dbname';

    $conn = mysql_connect($host, $user, $pass);
	//connection needs db name below
mysql_select_db("dbname", $conn);
//This is just if your using information from a search form
$search = $_POST["tZip"];
// after the from is whatever table you are attempting to pull data from
$connstring = "SELECT * FROM tablename WHERE columnname LIKE '$search' " ;
$result = mysql_query($connstring);
?>

 	 	
<?
echo "<h1>Stores Near You:</h1>";

// Checks to make sure it actually brings a result, so to whether show them or to say out of luck or similar issue
if(mysql_num_rows($result) == 0) {
     echo "Sorry there appears to be no stores in your immediate area.";
   }
else{
while($nt=mysql_fetch_array($result)){
echo "<br />";
echo "<br />";
// for each $nt has a correlated column name to display

     // something else to display the content
     // this displays the content how you want it depending on the theme of what their doing will depend on what you do here for html
	 echo "<h4>$nt[cnam1]</h4>$nt[cnam2]<br />$nt[cnam3]<br />$nt[cnam4], $nt[cnam5]<br />$nt[cnam6]";
   }
echo "<br />";
echo "<br />";
}

echo mysql_error();
mysql_close();
?>