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);
Related
I am making a website that will use a MySQL database on a friend's web hosting service. It supports PHP and MySQL but I'm not sure what file I am meant to create the database and table in. I understand the syntax so I just need to know where and which files to do it in. I guess you don't put it in the website's html files because it only needs to be created once.
I have the following code:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$conn = mysqli_connect($servername, $username, $password);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "CREATE DATABASE LeagueData";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
You don't have to create a file. You have to create the DB and structure from within MySQL (or you can load a .sql file into MySQL).
If he has phpMyAdmin use that. It's much easier.
Creating a table from within MySQL: http://dev.mysql.com/doc/refman/5.7/en/creating-tables.html
Importing an .sql file from the commandline: mysql -u username -p database_name < file.sql
Adding on to Patrick's answer:
You need to create your database first, and not through PHP. mysqli allows you to connect to a particular database (which you need to specify in your code), and then execute queries on that database.
First, create a database. You should consult your web-hosting provider on how to do this. Most web-hosts allow you to create a database through one of the CPanel modules. (You'll also have to create a "user", and assign that user to the newly created database.)
Once you have created the database, you will be able to connect to it via PHP. Your code to connect to the database should look like:
$conn = mysqli_connect($servername, $username, $password, $database_name);
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
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
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 have this query:
mysql_select_db('scanner');
$query = "SELECT * FROM scanner.proxy ORDER BY RAND() LIMIT 20";
$result = mysql_query($query) or die(mysql_error());
it tells me:'scanner.proxy ' doesnt exist.. even though I do have it the table with the database. It is weird, cause my other queries work..but this one doesnt.
UPADTE:
Event when I put this:
$user='root';
$user='root'
$password='<removed_password>';
$dbname = 'scanner';
$host = 'localhost';
$link = mysql_connect($host, $user, $password) or die (mysql_error());
mysql_select_db($dbname, $link) or die (mysql_error());
it gives me this...
Unknown database 'scanner'
But I can see the scanner database in the phpmyadmin
Even when I type in phpmyadmin the sql statement
SHOW TABLES FROM 'scanner'
it says it cant find scanner
We solved this problem which occurred when connecting to multiple remote MySQL databases within the same script by adding a re-connect method before every mysql_query statement. The re-connect method invoked both mysql_connect and mysql_select_db functions. We had previously tried setting the new link parameter to true in the connect statement(s) but We still got database errors and all kinds of funny behavior.
make sure the user you use to connect has enough rights to query that table. Perhaps that table was created by another user.
EDIT: Perhaps you need to grant access to scanner from a remote location. Running this sholud help on that.
GRANT ALL ON scanner.* TO your_user#'IP_ofServer_where_PHP_runs' IDENTIFIED BY 'PASSWORD';
You have set the $dbname = 'aws_backlinks', but you are trying to connect to a different database within your query. Change your connection string to include this db instead or just connect to the server and set your database at the time of the query.
$db_host = 'localhost:Port';
Specify port for localhost.
I was facing the same problem and specifying the port solved the problem.
You're missing the argument that specifies the connection (result of mysql_connect()) in your mysql_select_db() call:
$db_host = 'localhost';
$db_username = 'root';
$db_password = 'whatever';
$db_name = 'scanner';
$link = mysql_connect($db_host, $db_username, $db_password);
mysql_select_db($db_name, $link);