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 "";
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 "$cell
";
while($row = mysql_fetch_row($result))
{
echo "";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "";
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 want to create an options array from a string. How can i create an array as { width : 100, height : 200 } from a string ...
-
I'm new to Programming. I would like to implement a program with a yield keyword . So That, I have created a new List and ask the user ...
$cell
Blog Archive
-
▼
2017
(2404)
-
▼
June 2017
(276)
- c++ - Does curly brackets matter for empty constru...
- Why is Gwen Stacy (Emma Stone) in the Amazing Spid...
- debugging - Why do I get: double free or corruptio...
- c# - How to add double quotes to a string that is ...
- Open and define two excel files in VBA
- What are the Android SDK build-tools, platform-too...
- javascript - What does "this" mean in jQuery?
- java - Is there any performance difference between...
- c++ - Sequence Points vs Operator Precedence
- php - Parse error: syntax error, unexpected '{', e...
- javascript - What is the difference between Bower ...
- character - Was Bane part of the League of Shadows...
- Why is it bad style to `rescue Exception => e` in ...
- reactjs - Should flux stores, or actions (or both)...
- mysql - How to prevent XSS attacks in PHP?
- javascript - Changing URL without reloading (angul...
- plot explanation - Fox's analysis of the Bat - Mov...
- javascript - How to measure time taken by a functi...
- optimization - What Every Programmer Should Know A...
- java - How to get the right input for the String v...
- regex - Greedy vs. Reluctant vs. Possessive Quanti...
- Is there a CSS parent selector?
- php - how to save data dynamically to database
- http - Why use output buffering in PHP?
- enumeration - Iterate Between Enum Values in C#
- php - I can't find what *exactly* the syntax error...
- android - How to HTTP request by POST method with ...
- jquery - Event handler not working on dynamic content
- ruby - Match all occurrences of a regex
- angular - How to call function after dom renders i...
- javascript - Get a list of Available Functions on ...
- How to extract multi-part zip files with PHP?
- R Shiny App Error - Couldn't Find Any UI
- css3 - CSS select first element with a certain class
- security - Is there an exhaustive list of function...
- PHP header() will not redirect issue
- How can i validate integer form in php?
- plot explanation - What did Griffin show the Colon...
- php - Help with this error message > unexpected T_...
- How to make multiple MySQL connections from PHP
- c++ - What is the efficient way to count set bits ...
- How can I add a key/value pair to a JavaScript obj...
- arrays - VBA - looping through cells using "i"
- Why doesn't println! work in Rust unit tests?
- comparison - Is there a difference between !== and...
- javascript - In JS, is it allowed to declared the ...
- security - Secure hash and salt for PHP passwords
- security - What common web exploits should I know ...
- PHP/MySQL Fatal memory allocation error
- How do I convert a String to an int in Java?
- javascript - Deferred/promise confusion and implem...
- c - Necessity of use (float*) before malloc
- bash - echo that outputs to stderr
- javascript - Adding a different onclick for each d...
- android - How to call c++ methods from JNI
- c++ - What is The Rule of Three?
- javascript pushing element at the beginning of an ...
- c++ - Why does ofstream give me an echo ie. writes...
- php - $mysqli->prepare returns false, but $mysqli-...
- c++ - Undefined reference when using lambda
- unicode - What characters can be used for up/down ...
- android - NullPointerException FirebaseUser.getUid...
- sql - Retrieving the last record in each group - M...
- c++ - Is < faster than
- python - Horizon Line Detection via Canny and Hough
- plot explanation - Why doesn't Carl use the pill t...
- php - Get data from asp.net page using curl
- How to manually include external aar package using...
- How to find out the starting point for each match ...
- java - Understanding UI thread
- android - Can underline words in TextView text
- Regex Python - Modify random generated string
- javascript - What is JSONP, and why was it created?
- the dark knight rises - Was Bane's dialogue spoken...
- javascript - AngularJS : Initialize service with a...
- javascript - Why does Google prepend while(1); to ...
- c# - InvalidOperationException is thrown when a ri...
- How to delete a character from a string using Python
- scope - C# - Location of using statements
- java - What is the difference between JDK and JRE?
- oop - What is polymorphism, what is it for, and ho...
- javascript - What is the difference between angula...
- c++ - "undefined reference" to Virtual Base class ...
- inception - Why isn't Arthur woken up by the kick?...
- android - How do I save the state of my activity?
- c++ - Two General CS Questions
- Where are the most common OOP concepts implemented...
- character - Why does Banner finally allow the Hulk...
- c# - How to mock Controller.User using moq
- Iterate JavaScript Object Array Elements
- javascript - Data available only after alert
- image processing - How do I find Waldo with Mathem...
- c++ - Difference between static and shared libraries?
- Asynchronous Process inside a javascript for loop
- c++ - How do I solve "unresolved external symbol" ...
- swift - Core Location plist not working (Xcode 9.4...
- javascript - Is using == instead of === ok for nul...
- What does [:-1] mean/do in python?
- What does @ mean in PHP?
- html - How to style a checkbox using CSS
-
►
April 2017
(433)
-
►
March 2017
(450)
-
►
February 2017
(379)
-
►
January 2017
(432)
-
►
2016
(3825)
-
►
December 2016
(446)
-
►
November 2016
(421)
-
►
October 2016
(458)
-
►
September 2016
(374)
-
►
August 2016
(369)
-
►
April 2016
(311)
-
►
March 2016
(269)
-
►
February 2016
(145)
-
►
January 2016
(66)
-
►
2015
(11)
-
►
December 2015
(11)
No comments:
Post a Comment