Hi everyone I am trying to make a query with Variables. Which means someone chooses an option from a drop down list and with that value I get all the data from Mysql. I have tried so many things and had no success. Please help 😟 I am copying the php code I am using to execute this query. The rare thing is that when I hard code the variable after WHERE I get the expected result. HMMM!!
<?php
$name = "$_POST['airlines']";
$username = "myusername";
$password = "mypassword";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
//select a database to work with
$selected = mysql_select_db("mydb",$dbhandle)
or die("Could not select database:" .mysql_error());
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM airlines WHERE airline = '$name'");
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
$row=mysql_fetch_row($result);
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
echo $row[1];
echo $row[2];
echo $row[3];
?>
<?php
$name = "$_POST['airlines']";
$username = "myusername";
$password = "mypassword";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
//select a database to work with
$selected = mysql_select_db("mydb",$dbhandle)
or die("Could not select database:" .mysql_error());
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM airlines WHERE airline = '$name'");
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
$row=mysql_fetch_row($result);
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
echo $row[1];
echo $row[2];
echo $row[3];
?>