Can 1 PHP file connects to 2 databases? - php

I am trying to connect to the first database to pull some information, then update a table in the second database with that information.
However, the second query always tries to pull from the first database if I don't close the connection. If I do close the connection, then it gives me a socket error.
I am using Drupal (so my first database is the Drupal database). I want to get a value from one of those tables and put it in my Member Info database whenever 1 of 3 different PHP pages load.
Is this possible?

Yes, you can create multiple connections. You need to specify the link inside mysql_query as second arguements.
Example :
$link1 = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link1) {
die('Not connected : ' . mysql_error());
}
// second foo1
$db_selected_1 = mysql_select_db('foo1', $link1);
if (!$db_selected_1) {
die ('Can\'t use foo : ' . mysql_error());
}
$link2 = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link2) {
die('Not connected : ' . mysql_error());
}
// select foo2
$db_selected_2 = mysql_select_db('foo2', $link2);
if (!$db_selected_2) {
die ('Can\'t use foo : ' . mysql_error());
}
// query on first db
mysql_query('SELECT * FROM 1 WHERE 1', $link1);
// query on second db
mysql_query('SELECT * FROM 1 WHERE 1', $link2);
If you have the same crendetials on the same server for both database, you could just do this :
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// select foo1
$db_selected = mysql_select_db('foo1', $link);
// query on first db
mysql_query('SELECT * FROM 1 WHERE 1', $link);
// select foo2
$db_selected = mysql_select_db('foo2', $link);
// query on second db
mysql_query('SELECT * FROM 1 WHERE 1', $link);
I however suggest you to use mysqli_ or even PDO.

You can store multiple connections concurrently.
There is no limit in PHP that will stop you from doing that.
If you're having a problem with specific code, please update the question.

Related

error: No database selected when displaying data on website

My name is Anna and I'm building a website with a festival calendar! People can add festivals when they're logged in. But now is my question, how can I display what people put in my database. I found the following code on this website, but when I put it in my script, I get the error that there's no database selected. But I do have a database selected (Festivals) ?
$sql = "SELECT festival_name, festival_organisator, festival_begin FROM Festivals WHERE festival_id=3";
$result = mysql_query($sql) or die(mysql_error());
$festival_name = $festival_organisator = $festival_begin = array();
while($row = mysql_fetch_assoc($result)) {
$festival_name[] = $row['festival_name'];
$festival_organisator[] = $row['festival_organisator'];
$festival_begin[] = $row['festival_begin'];
}
I hope anybody can help me! Thanks in advance
You have not yet connected to the database itself
Query selecting from the table Festivals does not mean that you've selected the Festivals database, those are two very different things.
The database Festivals contains all the tables (table Festivals, table x, etc).
The table Festivals contains data which you can query (festival_name, festival_organisator, etc).
Now you must connect
Taking a look at the documentation is always a good choice, let's see how you can connect to and select a database:
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>
You should be connected to the database, having access to the selected database through $link.
mysql_query($sql, $link)
But don't do it!
Unless you must, but cute puppies will die :(
You shouldn't be using mysql_* in the first place though since it's no longer supported. Instead you should be using PDO or mysqli to achieve your task. It may sound or appear trickier at first but it's actually rather simple:
Related answer on how to query with PDO.
The PDO documentation provide nice examples on how to query.
The mysqli_* documentation provide nice examples on how to query.

Get a row from a database based on querystring

BIt of a php/mysql noob here, hope someone can help.
Ok so i have a URL which has an id in the querystring like so: wwww.mysite.com/page1.php?id=1
What i want to do is connect to a table in the database and get the data from the columns on one row where the first column named ID equals the id number held in the querystring.
I then want to print the data from each column in different div's elsewhere on the page.
There's also the additional issue of what to do if there's no row in the table with the same id as the querystring, i'd want it to change the id in the querystring to 1 and load that rows data.
I had a little go, i know it connects ok but i have no idea if the rest is what i want:
<?php
$link = mysql_connect('Address', 'Database', 'Password');
if (!$link) {
die('Could not connect to MYSQL database: ' . mysql_error());
}
$per = $_GET['id'];
$query = "select A,B,C,D,E,F,G,H,I,J,K,L from table_name where per=".$_GET['ID']."";
echo $result['A'];
mysql_close($link);
?>
And then put this in the div's to print the data.
<?php echo $result['A']; ?>
Am i along the right lines or completely wrong?
$dbConnection = mysql_connect('Address', 'Database', 'Password');
if (!$dbConnection) {
die('Could not connect to MYSQL database: ' . mysql_error());
}
$per = $_GET['id'];
$query = $dbConnection->prepare("select A,B,C,D,E,F,G,H,I,J,K,L from table_name where per = ?");
$query->bind_param('s', $per);
$query->execute();
$result = $query->get_result();
<?php echo $result; ?>
use this code first to avoid SQL Injection second that's the way it should work in PHP first prepare the query second execute and only then show it.
Use mysql_query function in your code.
mysql_* functions is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used.
<?php
$link = mysql_connect('Address', 'Database', 'Password');
if (!$link) {
die('Could not connect to MYSQL database: ' . mysql_error());
}
$per = $_GET['id'];
$query = "select A,B,C,D,E,F,G,H,I,J,K,L from table_name where per=$per";
$result = mysql_query($query, $link) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo $row['A'];
mysql_close($link);
?>

MYSQL, PHP two connections inside while loop

I have two databases on the same MYSQL server.
I want to be able to query one table on one database, and use the results for an insert into the other table (on the other data base). I've tried moving the mysql_select_db lines around to no avail. Please note this is a one off internal script so security is not a concern (Don't want to us mysqli)
<?php
// Connecting, selecting database
$link1 = mysql_connect('127.0.0.1', 'username', 'password', true)
or die('Could not connect: ' . mysql_error());
$link2 = mysql_connect('127.0.0.1', 'username', 'password', true)
or die('Could not connect: ' . mysql_error());
mysql_select_db('db1', $link1) or die('Could not select database');
mysql_select_db('db2', $link2) or die('Could not select database');
// Performing SQL query
$query = "select fields from table";
$result = mysql_query($query,$link1) or die('Query failed: ' . mysql_error());
while ($row = mysql_fetch_array($result)){
$querynew = "insert into table (blah,blah) values ('$row['name']',$row['name2']')";
mysql_query($querynew, $link2);
}
You can use plain SQL for this to minimize traffic across the wire:
INSERT INTO `db2.tbl1` (`field1`,`field2`)
SELECT `field1`, `field2` FROM `db1.tbl2` WHERE `someCondition`='IsMet'

sql query not inserting into table?

<?php
$link = mysql_connect('localhost', 'sc2broad_testing', '1BananA2');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_query("INSERT INTO Persons (re) VALUES ('Peter')");
mysql_close($link);
?>
This code isnt taking the value 'peter' and inserting it into persons row 're'?? should i attempt to tell it which database somewhere? thanks . it is saying it connects successfully even if i am not telling it which database to connect to? only the server and user? i am confused.
I think you may need to specify the database that you are querying to?
mysql_select_db('db_name', $link)
If not try changing the mysql_query to:
print("INSERT INTO Persons (re) VALUES ('Peter')");
You can then check the query is correct and test it works outside of the php.

mysql query not inserting string into table?

I have been able to manually insert values in my table using phpmyadmin, and even if i end up using the same php code i get from php my admin to call the query it STILL won't add the value to the table. here is the code:
<?php
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('sc2broating1', $link);
$sql = "INSERT INTO `sc2broad_tesing1`.`Persons` (`re`) VALUES (\'hello11\')";
mysql_query($sql);
mysql_close($link);
?>
Don't escape value.
$sql = "INSERT INTO `sc2broad_tesing1`.`Persons` (`re`) VALUES ('hello11')";
I would also consider using bound parameters, as seen in mysqli::prepare, if Mysqli is an option.

Categories