Difference between revisions of "MySQL PHP"

From KOP KB
Jump to: navigation, search
(Created page with "<syntaxhighlight lang="php"> <? //connection information $host = 'mysqlv108'; $user = 'chsalezip'; $pass = 'Ch54l351nC'; $db = 'chsalezip'; $conn = mysql...")
 
(Selecting the current week based off Monday)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
<syntaxhighlight lang="php">
+
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.
 +
==Simple Connect and Display troubleshoot==
 +
<syntaxhighlight lang="php" enclose="div">
 
<?
 
<?
 
//connection information
 
//connection information
  
  $host = 'mysqlv108';
+
  $host = 'hostname';
  
     $user = 'chsalezip';
+
     $user = 'username';
  
     $pass = 'Ch54l351nC';
+
     $pass = 'pass';
  
     $db = 'chsalezip';
+
     $db = 'dbname';
  
 
     $conn = mysql_connect($host, $user, $pass);
 
     $conn = mysql_connect($host, $user, $pass);
 
//connection needs db name below
 
//connection needs db name below
mysql_select_db("chsalezip", $conn);
+
mysql_select_db("dbname", $conn);
 +
//This is just if your using information from a search form
 
$search = $_POST["tZip"];
 
$search = $_POST["tZip"];
 
// after the from is whatever table you are attempting to pull data from
 
// after the from is whatever table you are attempting to pull data from
$connstring = "SELECT * FROM distributors WHERE searchZip LIKE '$search' " ;
+
$connstring = "SELECT * FROM tablename WHERE columnname LIKE '$search' " ;
 
$result = mysql_query($connstring);
 
$result = mysql_query($connstring);
 
?>
 
?>
Line 23: Line 26:
 
<?
 
<?
 
echo "<h1>Stores Near You:</h1>";
 
echo "<h1>Stores Near You:</h1>";
// after each bold is a column name so its appears organized
+
 
 +
// 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) {
 
if(mysql_num_rows($result) == 0) {
 
     echo "Sorry there appears to be no stores in your immediate area.";
 
     echo "Sorry there appears to be no stores in your immediate area.";
Line 29: Line 33:
 
else{
 
else{
 
while($nt=mysql_fetch_array($result)){
 
while($nt=mysql_fetch_array($result)){
 +
 +
// this was done due to some kind of formatting with the page after the fact so this may change depending on what your doing
 
echo "<br />";
 
echo "<br />";
 
echo "<br />";
 
echo "<br />";
Line 34: Line 40:
  
 
     // something else to display the content
 
     // something else to display the content
echo "<h4>$nt[sName]</h4>$nt[sAdd]<br />$nt[sCity]<br />$nt[sZip], $nt[sState]<br />$nt[sPhone]";
+
    // 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 />";
Line 43: Line 50:
 
mysql_close();
 
mysql_close();
 
?>
 
?>
 +
</syntaxhighlight>
 +
== Selecting the current week based off Monday ==
 +
It really depends what you are doing but this is where you could modify this to get current week, previous week and so on.
 +
 +
<syntaxhighlight lang="sql" enclose="div">
 +
select * from mmdata where week(FROM_UNIXTIME(`timestamp`), 1) like (week(CURDATE(), 1)-1) ORDER BY timestamp DESC
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 15:12, 6 July 2015

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.

Simple Connect and Display troubleshoot

<?
//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)){

// this was done due to some kind of formatting with the page after the fact so this may change depending on what your doing
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();
?>

Selecting the current week based off Monday

It really depends what you are doing but this is where you could modify this to get current week, previous week and so on.

select * from mmdata where week(FROM_UNIXTIME(`timestamp`), 1) like (week(CURDATE(), 1)-1) ORDER BY timestamp DESC