Friday, 16 June 2017
How to make multiple MySQL connections from PHP
Answer
Answer
The code below is working for one if statement but not giving results for another else if and it is showing the 'flights' table in first query but after another condition not display the other table named 'isb to muree'
$from = isset($_POST['from'])?$_POST['from']:'';
$To = isset($_POST['To'])?$_POST['To']:'';
if( $from =='Islamabad'){
if($To == 'Lahore'){
$db_host = 'localhost';
$db_user = 'root';
$database = 'homedb';
//$table = 'flights';
if (!mysql_connect($db_host, $db_user))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
$result = mysql_query("SELECT * FROM flights");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "Table: 'flights'
";
echo "";
while($row = mysql_fetch_row($result))
{
echo " ";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "$cell ";
echo " \n";
}
}
else if( $from =='Islamabad'){
if($To == 'murree'){
if (!mysql_connect($db_host, $db_user))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
$result = mysql_query("SELECT * FROM 'isb to murree'");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "Table: 'isb to murree'
";
echo "";
while($row = mysql_fetch_row($result))
{
echo " ";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "$cell ";
echo " \n";
}
}
}
}
mysqli_close($con);
?>
Answer
You should move the database connection variables to the top of your code (so they are outside of the if statement)
$db_host = 'localhost';
$db_user = 'root';
$database = 'homedb';
Subscribe to:
Post Comments (Atom)
c++ - Does curly brackets matter for empty constructor?
Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...
-
A fair amount of the second act of The Dark Knight Rises has a class warfare plotline. This is foreshadowed in the trailers with Selina Ky...
-
I'm still trying to wrap my head around how the following expression results in undefined behavior: a = a++; Upon searching SO...
-
i have added this sql in my code , function devenir_client_dataforform() { $type = $_POST['clientType']; //$produit...
No comments:
Post a Comment