My code is like this...
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$link) {
die('Could not connect: ' . mysqli_error($link));
}
$db_selected = mysqli_select_db($link, DB_NAME);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysqli_error($link));
}
//use $link works fine
mysqli_close($link);
$link2 = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$link2) {
die('Could not connect: ' . mysqli_error($link2));
}
$db_selected = mysqli_select_db($link2, DB_NAME);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysqli_error($link2));
}
if (!mysqli_query($link2, $sql)) {
die('Error ' . mysqli_error($link2));
}
and I get an error like
$link.TableNameFromLink2 does not exist
so basically even though I am closing the first link and opening a new one it is still defaulting to the first link when trying to connect to the table I want that exists in the second link... I don't get it.. It's killing me :(
In $link2 you are using same CONSTANT DB info as you used in $link
You have to use another CONSTANT to define another DB details.
like
define('DB_USER','user1');
define('DB_PASSWORD','password1');
define('DB_NAME','dbname1');
define('DB_HOST','localhost');
// for 2nd db ( if username and password is same then no need to define DB_USER1 and DB_PASSWORD1)
define('DB_USER1','user2');
define('DB_PASSWORD1','password2');
define('DB_NAME1','dbname2');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$link2 = mysqli_connect(DB_HOST, DB_USER1, DB_PASSWORD1, DB_NAME1);
Related
function db_connect($db_host, $db_user, $db_pass, $db_name) {
$db_connect = mysqli_connect($db_host, $db_user, $db_pass) or die('Could not connect: ' . mysqli_error($db_connect));
$db_select = mysqli_select_db($db_name, $db_connect) or die ('Could not select ' . $db_name . ': ' . mysqli_error($db_select));
}
Every variable is defined. All that appears is
Could not select CMS:
There isn't even an error message. What am I doing wrong? As far as I am aware, it is connecting to MySQL, but it isn't connecting to the specified database 'CMS'.
According to php.net, mysqli_select_db exepts to be first parameter MYSQLI link, seccond DB Name
bool mysqli_select_db ( mysqli $link , string $dbname )
http://php.net/manual/en/mysqli.select-db.php
You should turn on error reporting for next time, to catch that error.
error_reporting(1);
Hey i am using Mamp on imac and my problem is that when i hit the submit button (on a post form) to enter the data then nothing shows up and the database remains empty.
Here is my code :
<?php
define('DB_NAME', 'demob');
define('DB_USER','brom');
define('DB_PASSWORD','****');
Define('DB_HOST','localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect : ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('cant use' . DB.NAME . ' : ' .mysql_error());
}
$value = $_POST['input1'];
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_close();
?>
You are not executing a query.
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_query($sql);
You should know that, the method you are using to connect to mysql is deprecated now. please read up about PDO or mysqli
I am very new to PHP so this is my first attempt to connect to a mysql database through a php file and I am getting this message. I dont know how much anyone can help me with this or if at least someone can guide me to a right direction
Can not use : soum_email1:
And my php looks like this
<?php
define('DB_NAME', 'soum_email1');
define('DB_USER', 'soum_email');
define('DB_PASSWORD', 'Qe232f9');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!link){
die('could not connect:' . mysql_error());
}
$db_selct = mysql_select_db(DB_NAME, $link);
if(!$db_selected){
die('Can not use : ' . DB_NAME . ':' .mysql_error());
}
echo 'connection sucessful';
?>
You are assigning the mysql_select_db() function $db_selct, but then checking $db_selected (which with the code you've posted is always falsey.
Also, link should be $link (on line 9).
Your code should be:
define('DB_NAME', 'soum_email1');
define('DB_USER', 'soum_email');
define('DB_PASSWORD', 'Qe232f9');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link){
die('could not connect:' . mysql_error());
}
$db_selct = mysql_select_db(DB_NAME, $link);
if(!$db_selct){
die('Can not use : ' . DB_NAME . ':' .mysql_error());
}
echo 'connection sucessful';
You should note though that the mysql_* family of functions are now deprecated, and you should consider using MySQLi or PDO.
First of all:
Please use the pdo or mysqli database like Quentin wrote in the first comment.
Further you should name your variables right,
$db_selct = mysql_select_db(DB_NAME, $link);
and
if(!$db_selected){
die('Can not use : ' . DB_NAME . ':' .mysql_error());
}
have different variable names.
I'm having trouble connection to the database
Here is the code I am using
$con = mysql_connect('host', 'user', 'pass');
mysql_select_db('database_name', $con);
And here is the results I get
Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in /home/heartbeat_db/heartbeatsmart.com/php/config/dbconnect.php on line 2
Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/heartbeat_db/heartbeatsmart.com/php/config/dbconnect.php on line 3
Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in /home/heartbeat_db/heartbeatsmart.com/php/config/dbconnect.php on line 2
Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/heartbeat_db/heartbeatsmart.com/php/config/dbconnect.php on line 3
try using this code
$con=mysql_connect("host","user","pass");
mysql_selectdb("database_name",$con);
It's better to use PDO or Mysqli. I prefer PDO because it also supports other databases than mysql so you can easier migrate if necessary.
You can easily make a connection through
$db = new PDO('mysql:host=localhost;dbname=<SOMEDB>', '<USERNAME>', 'PASSWORD');
For more information: http://php.net/manual/en/book.pdo.php
If you want to use mysqli use:
$mysqli = new mysqli("localhost", "user", "password", "database");
Try using this code
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($link) . "\n";
mysqli_close($link);
Source: http://www.php.net/manual/en/mysqli.construct.php
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
$mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
?>
$con = mysql_connect('host', 'user', 'password');
if (!$con) {
die('Not connected : ' . mysql_error());
}
$db = mysql_select_db('database_name', $con);
if (!$db) {
die ('Can\'t use database_name : ' . mysql_error());
}
If I use the following code, it works:
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
But when I do this, it doesn't:
$db_host='localhost';
$db_id='root';
$db_pass='';
$con = mysql_connect($db_host, $db_id, $db_pass);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
Trying to swap (") and (').
mysql_ functions are discouraged for new applicationa you are advised to use mysqli or PDO. The following code uses PDO to connect to database.
//dependant on your setup
$host= "localhost";
$username="XXX";
$password="YYY";
$database="ZZZ";
// connect to the database
try {
$dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);
//Remainder of code
}
catch(PDOException $e) {
echo "I'm sorry I'm afraid you can't do that.". $e->getMessage() ;// Remove or modify after testing
file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]'). $e->getMessage()."\r\n", FILE_APPEND);//Change file name to suit
}
// close the connection
$dbh = null;
try using a script like this
$db_host = 'localhost';
$db_id = 'root';
$db_pass ='';
$con = mysql_connect ($ db_host, $ db_id, $db_pass) or die ('Could not connect:'. mysql_error ());
That code is fine. Review the error log- the problem has to be external to that code.