Hi all I have a question about PHP connection and Android.
I have got some PHP files, in everyone of this file I make a call for connect to the database
new mysqli($servername, $username, $password, $dbname);
and after, I will do a query to extract data from DB and return a JSON to Android into an AsyncTask. I have seen that make everytime a call to
new mysqli($servername, $username, $password, $dbname);
was very slow.
My question is, is there a way for speed up connection to DB or save the connection for the first time, into Android and after pass as parameter to PHP files?
Thanks in advance.
For the servername you can use "p:servername" for example:
new mysqli("p:db.test.com", "user", "pass", "db");
Apache will then create a persistent connection that gets reused.
If you want to use it in the same request you can always save it in a variable like this:
$db = new mysqli("db.test.com", "user", "pass", "db");
and call your functions to the $db variable
Read throught this: http://php.net/manual/en/mysqli.persistconns.php for further information
Related
I have a block of code to submit my HTML form values to my Database using PHP via XAMPP. After construction and testing the record will saved in database successfully but I see a notice that says below:
Undefined variable: localhost in
C:\xampp\htdocs\30-11-2021\processhtml.php on line 19
Kindly see my line 19 below also:
$conn = new mysql ($localhost, $dbusername, $dbpassword, $dbname);
You want
$conn = new mysql ('localhost', $dbusername, $dbpassword, $dbname);
The first parameter is the hostname of the server running MySQL. In your case it's 'localhost'.
php warns you when you use an undefined variable, and gives the value null for that variable. In this context, a null hostname defaults to localhost, so that's why your code actually works.
I already create a database in MySQL using
CREATE DATABASE register
And I set
$db= "CREATE DATABASE register";
$host="localhost";
$dbusername="root";
$dbpassword="";
$dbname= "register";
//create database connection
$conn = new mysqli($host,$dbusername,$dbpassword,$dbname);
but some error that becomes error
Warning: mysqli::__construct(): (HY000/1049): Unknown database 'register' in C:\xampp\htdocs\web\html\reg.php on line 19
How can I fix that
You don't have to specify a database:
$conn = new mysqli($host, $dbusername, $dbpassword);
You can then issue your query with $conn->real_query($db) (which is OK because you're just issuing a statement without any user data) and use $conn->select_db to make the database the active db after creating it.
Your assignment in the beginning ($db = ...) doesn't do anything else than assign the value to a variable - the query isn't ran before you actually send it to MySQL.
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'am using MySQLi to connect and fetch data from my MySQL server with
$link = new mysqli("localhost", "root", "","database_1");
I have a file that used for connection and data collection (dboperations.php) from above database
Now , i need to connect another database (e.g. database_2) and fetch data in the same php file.
Conditions:
Are databases on the same server? YES
Am i authorized to connect with same username and pass? YES
Is there any way to do that? Thanks.
Use mysqli::select_db to switch databases on the same server:
$link = new mysqli('localhost', 'root', '', 'database1');
then
$link->select_db('database2');
SELECT * FROM database_2.table ...
I am working on a word press plugin where i have some data to be saved in another mysql server database.Simply the data have to be saved in another MySql server's database table.Hope i am Clear.How can i approach to this progrmmatically.Any suggestions are highly appreciated.Thanks in advance
You will have to read up on PHP's methods for creating a database connection. Once you establish an additional database connection (using IP, port, and login credentials for the second database), you'll be able to interface with it through SQL. You'll be able to assign the new database connection to a handle so you can easily control which database you're sending the SQL queries to and avoid communicating with the wrong one.
Please make a new database connection with new instance and save data with other database.
for eg.
$dbh1 = mysql_connect($hostname, $username, $password);
$dbh2 = mysql_connect($hostname, $username, $password, true);
mysql_select_db('database1', $dbh1);
mysql_select_db('database2', $dbh2);
mysql_query('select * from tablename', $dbh1);
mysql_query('select * from tablename', $dbh2);