I have a My SQL database table that looks like this:
|size |drive1 | drive2 | drive3 | drive4 |
|36 |spring | gbx | chain | electric|
|48 |spring | gbx | cable | electric |
and so forth.
I need to display a list of available drives for a given size. Ideally, there should be no listing at all if there is no value in a drive column - but that is not essential.
The size to be selected comes from the result of a form from another page.
So far, my coding looks like this:
<?php
$username = "aname";
$password = "apassword";
$hostname = "localhost";
$formurl = "http://www.mysite.com/Form_error.htm" ;
$errorurl = "http://www.mysite.com/sorry.htm" ;
$drivestable = "mydrives";
$size=$_POST['size'];
$_SESSION['size'] = $_POST['size'];
echo $size;
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo '<p>MySQL connected</p>';
// select database
mysql_select_db("blind_size") or die ("Unable to select database!");
echo '<p>Database selected</p>';
$query = "SELECT drive1, drive2, drive3, drive4
FROM $drivestable
WHERE size = $size
ORDER BY drive1, drive2, drive3, drive4";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. " .
mysql_error());
while ($row = mysql_fetch_row($result)) {
echo(
"<li>" . $row["drive1"] . "</li>"
"<li>". $row["drive2"] . "</li>"
"<li>". $row["drive3"] . "</li>"
"<li>". $row["drive4"] . "</li>"
);
// close database
mysql_close($dbh);
echo '<p>MySQL disconnected</p>';
?>
This results in a blank screen.
If I remove the execute query, and 36 was the size selected in the form, the screen displays:
36
MySQL connected
Database selected
MySQL disconnected
So the execute query is the problem. I'd be grateful for help to put that right.