LJ Archive

Listing 4. Connecting to MySQL

mysql>
CREATE TABLE parts (
  partno int(11) DEFAULT '0' NOT NULL,
  parentpartno int(11),
  description char(255),
  price decimal(8,2),
  shipweight decimal(10,4),
  PRIMARY KEY (partno)
);
-----------php code------------
$connection = mysql_connect("127.0.0.1","root","tiger");
$db="stores";
$query=mysql_db_query($db,"SELECT
  partno,parentpartno,description,price FROM
  parts ",$connection);
$rowno=mysql_num_rows($query);
if ($rowno) {
echo  "<FORM ACTION = \"page2.php3\" METHOD=GET>";
echo "<TABLE border><CAPTION><SPAN><H3> Parts List </CAPTION>";
echo "<COLGROUP><COL align = left></COL>
<COL align= left></COL><COL align=left>
</COL><COL align=right><COL></COL></COLGROUP>";
echo "<TR>
     <TD> Part # </TD>
     <TD>  Parent Part # </TD>
     <TD> Description </TD>
     <TD> Price </TD> <TD> Include in Cart
      </TD></TR>";
            while($rowno-){
       $row=mysql_fetch_array($query);
        echo "<TR>\n";
        for($i=0;$i<4;$i++)
         {
           $value=$row[$i];
           echo "  <TD>$value</TD>";
            }
        echo "<TD><INPUT NAME=$row[0] VALUE = 1
    Type = \"checkbox\"></TD></TR>\n";
// want to use partnumber as param to checkout
// script,that is field 0 of query
       }
echo "</TABLE>";
}
echo "<INPUT  TYPE=\"submit\" VALUE="\go
 on to next step\">";
LJ Archive