Is $suburb text or numeric? If it is text it needs the single quotes around it.
If it is numeric then you don't need the quotes.
Either way, as Thirusha says, you do need the curly brackets (plus the double quote on the end).
slyme
Junior Poster in Training
63 posts since Aug 2009
Reputation Points: 11
Solved Threads: 4
Try this:
("SELECT * FROM '" . $table . "' WHERE Suburb = '" . $suburb . "'")
slyme
Junior Poster in Training
63 posts since Aug 2009
Reputation Points: 11
Solved Threads: 4
1. Do you have mysql_connect ?
2. Are you selecting the database ?
3. Does that table exist ?
4. Are you sure the query is correct ? Ie., echo the query and test it in phpmyadmin/mysql console.
If your answer is *yes to all*, please post your complete code, so that others can assist.
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
You can use this script to test your db/php stuff - fill in the blanks at the top and run it, if it fails you should glean some more meaningful info:
<?php
$db_host = '';
$db_name = '';
$db_user = '';
$db_password = '';
$db_table = '';
$db_query = '';
$db_field = '';
$db_display = '';
$con = mysql_connect($db_host, $db_user, $db_password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_name, $con);
$result = mysql_query("SELECT * FROM $db_table WHERE $db_field = '$db_query'");
while($row = mysql_fetch_array($result))
{
echo $row[$db_display];
}
mysql_close($con);
?>
slyme
Junior Poster in Training
63 posts since Aug 2009
Reputation Points: 11
Solved Threads: 4
Ok - surround the code with an if statement to check whether the form has been posted or not.
I'm guessing that the code is being executed without any input when you first load the page.
slyme
Junior Poster in Training
63 posts since Aug 2009
Reputation Points: 11
Solved Threads: 4
Are you using Linux server ? If yes, then make sure to check the case (lowercase/uppercase) of table and column names. Linux is case-sensitive. :)
Did you try printing out the query and executing it in mysql ?
Well, here is a quote from php.net
mysql_fetch_array
Return Values:
Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows.
As you can see, you are using the empty result resource in the while loop. Try this.
<form action="" method="post" name="Form">
<p>Business Type:
<select name="table" id="table">
<option value="Plumber">Plumber</option>
<option value="Electrician">Electrician</option>
<option value="Painter">Painter</option>
</select>
</p>
<p>Suburb:
<select name="suburb" id="suburb">
<option value="Bucketty">Bucketty</option>
<option value="Kulnura">Kulnura</option>
<option value="Gosford">Gosford</option>
</select>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
<?php
$table = $_POST['table'];
$suburb = $_POST['suburb'];
$sql = "SELECT * FROM $table WHERE Suburb='$suburb'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
echo '<br/> Name: '.$row['Name'];
echo '<br/> Suburb: '.$row['Suburb'];
echo '<br/> Listing: '.$row['Listing'];
echo '<br/><br/>';
}
} else {
echo "No records found :)";
}
?>
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
Can you post your code ? (and print the variable $sql and post it here ?)
I guess the variable $table is empty or something.
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
The variables $table and $suburb will be empty when the page is first loaded. They will only populated once the form has been submitted - whatever other problems there may be, I think this should be avoided by using an if statement to check that the form has been submitted before executing any other code.
slyme
Junior Poster in Training
63 posts since Aug 2009
Reputation Points: 11
Solved Threads: 4
The variables $table and $suburb will be empty when the page is first loaded. They will only populated once the form has been submitted - whatever other problems there may be, I think this should be avoided by using an if statement to check that the form has been submitted before executing any other code.
:) Thanks for waking me up. That is absolutely right.
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356