I have 2 connection string in 2 file connect.php connect to 2 mysql database on 2 server.
File dbconnect1:
<?php
$conn = mysql_connect('sv1','root','123456') or dir("No connect");
mysql_select_db('db1')or dir("not connect database");
mysql_query("SET charactor_set_results=utf8",$conn);
mysql_query("SET NAMES 'utf8'");
?>
File dbconnect2:
<?php
$conn = mysql_connect('sv1','root','123456') or dir("No connect");
mysql_select_db('db2')or dir("not connect database");
mysql_query("SET charactor_set_results=utf8",$conn);
mysql_query("SET NAMES 'utf8'");
?>
When I include in file php and exercute query it not show result.
One of 2 connection string not work. Why?
I think it is because you use the same variable $conn for both DB connections. Replace it with $conn2 for example in the dbconnect2.php file. At the moment your first connection get overwritten by the second connection. This is because you do them one after another.
Plus you have to put TRUE for the fourth paramether in mysql_connect() function in order to start a new connection to the same DB server.
http://php.net/manual/en/function.mysql-connect.php
File dbconnect2:
<?php
$conn2 = mysql_connect('sv1','root','123456', TRUE) or die("No connect");
mysql_select_db('db2', $conn2)or die("not connect database");
mysql_query("SET charactor_set_results=utf8", $conn2);
mysql_query("SET NAMES 'utf8'", $conn2);
?>
Plus you have to point which MySQL connection you want to use in mysql_select_db() function(s) and mysql_query() function(s).
You have to change the second db connection variable say $conn2 and also use the connection statement as follows:
$conn2 = mysql_connect('sv1','root','123456', TRUE) or die("No connect");
Where the 4th parameter has to pass as TRUE.
Reference: http://php.net/manual/en/function.mysql-connect.php
Related
< ?php
$connection = mysql_connect(localhost","user","password") or die ("Couldn't connect to the server!");
mysql_select_db("users", $connection) or die ("Couldn't connect to the database!");
I used the above code above to try and connect to my database but keep getting an error in line 2 where mysql_connect(localhost)---- I've typed in my localhost name correctly why won't it work?
please help this is my first day ever dealing with php and databases.
thanks in advance.
Missing a quote in localhost:
$connection = mysql_connect("localhost","user","password") or die ("Couldn't connect to the server!");
The correct way to do it is:
<?php
$connexion = mysql_connect("localhost", "user", "password")
or die("Couldn't connect to the database! : " . mysql_error());
mysql_close($connexion);
?>
Nota Bene:
Rather use mysqli_ or PDO. What you are using here is deprecacted for security reasons.
I have read a lot about this but it still doesn't work.
I'm just trying to select a database to create a new table in, I try:
$db = mysqli_select_db("test");
if(!$db) {
echo "error: " . mysqli_error($db);
}
But I still get an error (and mysqli_error($db) doesn't seem to work).
Of course I have already connected to it:
$con=mysqli_connect("localhost", "administrator", "****");
On phpMyAdmin I have these databases:
Why can't I select "test" ?
And creating a database doesn't work because I don't have the rights, as you can see.
The procedular signature of this function is:
bool mysqli_select_db ( mysqli $link , string $dbname )
So you will have to provide the resource you got back from the mysqli_connect() to make it work. Something like this:
$con = mysqli_connect("localhost", "administrator", "****");
$success = mysqli_select_db($con, "test");
Alternatively you could specify the database on the connect call with a 4th argument:
$con = mysqli_connect("localhost", "administrator", "***", "test");
See the examples on mysqli_connect().
mysqli_select_db function requires two parameters link and dbname. Please refer to the documentation:
http://php.net/manual/en/mysqli.select-db.php
You are only passing link and no database name in your call:
$db = mysqli_select_db("test");
I have the following PHP script setup to run as a cron job every minute:
<?php
$con = mysql_connect("localhost", "user", "pass");
if (!$con) {
die("Database error");
}
mysql_query("TRUNCATE TABLE contact_ips");
mysql_close($con);
die();
?>
This script is to prevent a contact form being submitted from the same IP address more than once within 1 minute. Therefore, it should clear the contents of the table that submitted the form and it is not doing that.
Could it be my cron command that is working correctly?
Thanks
First of all, mysql_query is deprecated. Use mysqli instead.
Second, it seems like you might want to set a variable to the query, eg $result = mysqli_query() so you can do post-processing.
Have you tried adding a die() to the mysql query to check that that query is running?
Also, it seems like you haven't selected a DB. Add this after the connection code. mysql_select_db("databaseName", $con);
It should be
<?php
$con = mysql_connect("localhost", "user", "pass");
if (!$con) {
die("Database error");
}
mysql_select_db("database", $con);
mysql_query("TRUNCATE TABLE contact_ips");
mysql_close($con);
die();
?>
I've seen a few errors like this, but I found no answer.
Unable to connect to database: Access denied for user ''#'localhost' to database 'socialdb'
socialdb is my database. The "Unable to connect to database:" part is located here.
$db = mysql_select_db("socialdb",$con);
if(!$db) {
die ('Unable to connect to database: ' . mysql_error());
}
I don't know what's causing this. Here are my mysql_connect details
<?php
$con = mysql_connect("localhost");
if(!$con) {
die ('Error: ' . mysql_error());
}
I need to find the root. Thanks.
I DON'T HAVE A USERNAME OR PASSWORD FOR MySQL
Should it be this?
mysql_connect("localhost","","");
The error is pretty self-explanatory, you're not allowed to connect without specifying some credentials.
Change your call to mysql_connect() to something like this:
mysql_connect("localhost", "user", "password");
Your default credentials are most likely:
$con = mysql_connect("localhost","root","");
You're missing username and password parameter, should be:
$con = mysql_connect("localhost","username","password");
You did not specify a user in neither mysql_connect or your PHP configuration.
Either set mysql.default_user and mysql.default_password in your PHP configuration or use the appropriate arguments for mysql_connect.
mysql_connect takes 3 parameters
$con = mysql_connect("localhost", "dbuser", "password");
even if you are not having a user. there exist 'root' user
so use it
$con = mysql_connect('localhost','root','');
Not wanting this question to be too long, I will skip to an example:
If I have 2 files: paper.php and rock.php, and they contain the following:
paper.php:
include('rock.php');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");
and rock.php:
define ("DB_HOST", "localhost");
define ("DB_USER", "foo");
define ("DB_PASS","bar");
define ("DB_NAME","fooDBar");
Eventually, will the user viewing my paper.php file be connected to the DB or not?
Not wanting the answer to be too long:
Yes.
Yes, you define all the appropriate variables in rock.php and are including rock.php, then they will be defined for the whole program execution, including where you do a mysql_connect().