Wondering if it is possible to include two mysqli connection include php files on the same page.
I have a basic site I am using to learn/develop in php with mysql. I have one database for storing user information and another for website specific information. I can use both independently from each other without any issues.
Config.php:
DEFINE("HOST", "127.0.0.1");
DEFINE("USER","root");
DEFINE("PASS","");
DEFINE("DB","sv");
connect.php:
include_once 'config.php';
$mysqli = new mysqli(HOST, USER, PASS, DB);
sec_config.php:
define("HOST", "127.0.0.1");
define("USER", "sec_user");
define("PASSWORD", "");
define("DATABASE", "svn");
sec_connect.php:
include_once 'sec_config.php'; // As functions.php is not included
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
My php pages can successfully query and display data from these connections. My index.php currently contained included the sec_connect, I am trying to add another php page which (within itself) includes
include_once("connect.php");
When I try and run the page I receive
mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: No such host is known.
mysqli::mysqli(): (HY000/2002): php_network_getaddresses: ...as above
mysqli::prepare(): Couldn't fetch mysqli in
Fatal error: Call to a member function bind_param() on a non-object in
I have simply commented out the above include_once to get everything working again.
I am using windows with xamp for dev, I have tried accessing via localhost and my ip address and I receive the same error. I have tried editing the hosts file and I have also tried amending the values in the connect files to be unique (changed back as it didnt help).
Hopefully this is the righ question to ask: Is it possible to include two mysqli database connections via different includes from my index.php page?
This should work, provided you refer to the two different connections with different variables (they are two distinct instances after all).
For example:
file1.php
$db1 = new mysqli(HOST1, USER1, PASSWORD1, DATABASE1);
file2.php
$db2 = new mysqli(HOST2, USER2, PASSWORD2, DATABASE2);
file3.php
require "file1.php"
require "file2.php"
$result1 = $db1->query("SELECT * FROM table");
$result2 = $db2->query("SELECT * FROM some_other_table");
Related
I have included 1 database connection in my PHP file. The file that connects to the database is called connect.php. This works successfully with no errors. When I try to include 2 database connections in my PHP file, this is when I encounter errors. It seems as though, I can only connect to one database at a time because only 1 of the databases are connected. Is there a way to include 2 database connections in 1 file?
This is what I have right now:
<?php
require "connect.php";
require "informational-connect.php";
?>
This is what's included in connect.php:
<?php
$db= new mysqli("localhost", "XXX", "XXX", "ARTICLES");
if($db->connect_errno)
{
die("Error");
}
?>
This is what's included in informational-connect.php:
<?php
$db = new mysqli("localhost", "XXX", "XXX", "INFORMATION-DATA");
if ($db->connect_errno)
{
echo die("Error");
}
?>
I am using MySQLi.
In order to have different connections you have to give the connection variables different names
Besides, you need only one database to deal with, and therefore only single connection
The database selection is associated with the connection resource. So you can select it once for each connection.
If you don't call mysql_select_db at all,then all your queries will need to specify explicit database prefixes before all the tables, e.g. select * from db1.table ....
mysqli is provide a functionality for connect multiple database at a time.
But How one single variable in a file can hold two different
connections ?
You should use two different variables for each database connection.
$Db1 = new mysqli($hostname,$username,$password,$db_name1); // connection 1
$Db2 = new mysqli($hostname,$username,$password,$db_name2); // connection 2
I have a large procedural style php/mysql website, which is splitted into many different files. Within each file, i include my dbconn.php, to ensure, that a db connection is available.
It looks something likes this:
index.php
<?php
include_once ('header.php');
include_once ('nav.php');
include_once ('content.php');
include_once ('sidebar.php');
include_once ('footer.php');
?>
and within each of these files i call several other files via include_once. So in total i come up with about 30 different files.
Every file starts with:
include_once($_SERVER['DOCUMENT_ROOT'].'/dbconn.php');
Since i'm using include_once, dbconn.php should not be loaded more then once, but recently i get following php warning.
PHP Warning: mysqli_connect(): (HY000/1226): User 'username' has exceeded the 'max_user_connections' resource (current value: 20) in /var/www/html/dbconn.php on line 10
My dbconn.php looks like this:
<?php
$host = "localhost";
$user = "username";
$pass = "password";
$db = "dbname";
if(!mysqli_thread_id($con)){
$con = mysqli_connect($host, $user, $pass, $db);
mysqli_set_charset($con, 'utf8');
}
unset($host,$user,$pass,$db);
?>
So there is a double check.
If there already is an open mysql connection, the file should do basicly nothing.
What am i doing wrong?
Why do i get this php warning and how do i reduce the amount of mysql connections?
Basically, it's not different calls from a single page that is leading to the problem. It's the fact that you have multiple OPEN connections at the moment.
Make sure you close you connections after they have served their purpose.
Also, try to avoid the kind of structure you currently have, have all the DB related stuff included in one file and then include it only once on your page.
I solved the problem by using a singleton pattern.
So i changed the mysqli prodecural code into mysqli objectoriented.
i got the pattern here:
http://www.davecomeau.net/blog/1/Single+Database+Object+in+PHP+5+Using+the+Singleton+Pattern
I am attempting to create a separate login file for database connections as I am not too fond of having all the access details on each page that requires database access.
I have created a separate file on my server that contains the variables required for a successful login and then use the;
include_once('path_to_file/filename.php');
to get the variables and then use;
$dbconnection = mysqli_connect("$hostname","$username","$password","$database") or die ("Could not connect to the server");
but the connection fails every time. I tried including the connection script in the file I am attempting to include but then I get this message:
Can't connect to local MySQL server through socket '/tmp/mysqld.sock' (2)
I'm not really sure how to fix this, but every page in my server more or less access the database and I think it has to be a security risk having login details replicated everywhere!
Anyone have any suggestions or alternatives?
databaseloging format is:
<?php
# parameters for connection to MySQL database
$hostname="hostname";
$database="databasename";
$username="username";
$password="password";
?>
P.S. I have also tried require and got the same result.
Also when using multiple MySQL connections in PHP, you have to supply a fourth argument telling PHP to actually create new connections like this (this is very important, if you are using two connections to the same host):
$db1 = mysql_connect($host1, $user1, $passwd1, true);
$db2 = mysql_connect($host2, $user2, $passwd2, true);
If the fourth argument is not used, and the parameters are the same, then PHP will return the same link and no new connection will be made.
After this you should use "mysql_query" with an extra parameter than defines which connection to use:
$res1 = mysql_query($sql1, $db1) or die(mysql_error($res1));
$res2 = mysql_query($sql2, $db2) or die(mysql_error($res2));
http://www.php.net/manual/en/function.mysql-connect.php
Okay I'm having trouble connecting my database to my host..
Im using myPHPAdmin and I made a database on my host
the problem is in my php code where I define my connection to the host
everything worked fine when I did this on the localhost.
But now that I want to use it on my personal domain it wont work
it doesnt access the data in the database.
Im new at this so dont shoot me ;)
$connect = mysql_connect("host", "user", "pass");
so for my host/domain I enter my domain the-bloodgod.com ?
what do I enter for user and pass? is it just the login I normally use to access the Cpanel? Or do I have to create special permissions in myphpadmin?
also on my hosts myphpadmin it shows that it stores all created tables in the_bloodgod_com database collection
so would this be correct if I put it the code bellow?
mysql_select_db("the_bloodgod_com");
$sql="SELECT * FROM tablename";
MOST hosting companies you have to use "localhost" as the host name for your database connection code. PHP:
$link = mysqli_connect("localhost","username","password","database") or die("Error " . mysqli_error($link));
What I am trying to do is get mysql database to load up my .php file. I am using hostgator to run mysql database server. So far what i have for sql is a table with three columns.
int: id (primary key / A.I.)
varchar: name
text: message
I save the table and name it "test" and the database is called "testdb"
My php file (tutorialTest.php) looks like this:
<?php
$username = "nfoggia_nick";
$password = "imnick";
$database = "nfoggia_testdb";
mysql_connect(localhost, $username, $password);
#mysql_select_db($database) or die("Unable to find database");
$name =$_GET["name"];
$message = $_GET["message"];
$query = "INSERT INTO test VALUES (' ', '$name', '$message')";
mysql_querry($query) or die(mysql_error("error"));
mysql_close();
?>
I added the .php file in my file directory on hostgator and now my problem is this:
I know that this code will do nothing, but when i type in
http://localhost/tutorialTest.php
the web browser says "browser cannot connect to local host" when it should just show a blank screen. Any help? What did i do wrong?
EDIT:
I moved my php file to the document root for my website and now when i run the
http://myWebsiteName/tutorialTest.php this shows up:
Fatal error: Call to undefined function mysql_querry() in /home2/nfoggia/public_html/tutorialTest.php on line 15
Before mentioning all the PHP errors, your URL is wrong.
localhost means your local computer, instead of your hosting environment, which you mentioned is hostgator.
Do you upload your PHP to hostgator server?
Is the MySQL database schema exist in hostgator environment?
Your URL should look like : http://www.hostgator.com/whatever/tutorialTest.php (or under your domain name). Anything but not http://localhost
First of all, remove the # to reveal the error.
For MySQL connection, the first parameter is a string, so you have to enclose it with single quotes, that is:
mysql_connect('localhost', $username, $password);
Last, you have multiple PHP errors :
mysql_querry($query)
You misspelled the function. Also, mysql_error() accepts link identifier as optional parameter instead of a string.
As a side note, stop using deprecated mysql_* functions. use MySQLi or PDO instead. Also, your code is subjected to SQL Injection attack, as you directly allow GET values to be inserted in your query.