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());
}
Related
I am new to Mysqli_* and I am getting these errors:
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string
given in D:\Hosting\9864230\html\includes\connection.php on line 11
Warning: mysqli_error() expects exactly 1 parameter, 0 given in
D:\Hosting\9864230\html\includes\connection.php on line 13
Database selection failed:
<?php
require("constants.php");
// 1. Create a database connection
$connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$connection) {
die("Database connection failed: " . mysqli_error());
}
// 2. Select a database to use
$db_select = mysqli_select_db(DB_NAME,$connection);
if (!$db_select) {
die("Database selection failed: " . mysqli_error());
}
?>
Your arguments are in the wrong order. The connection comes first according to the docs
<?php
require("constants.php");
// 1. Create a database connection
$connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$connection) {
error_log("Failed to connect to MySQL: " . mysqli_error($connection));
die('Internal server error');
}
// 2. Select a database to use
$db_select = mysqli_select_db($connection, DB_NAME);
if (!$db_select) {
error_log("Database selection failed: " . mysqli_error($connection));
die('Internal server error');
}
?>
// 2. Select a database to use
$db_select = mysqli_select_db($connection, DB_NAME);
if (!$db_select) {
die("Database selection failed: " . mysqli_error($connection));
}
You got the order of the arguments to mysqli_select_db() backwards. And mysqli_error() requires you to provide a connection argument. mysqli_XXX is not like mysql_XXX, these arguments are no longer optional.
Note also that with mysqli you can specify the DB in mysqli_connect():
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if (!$connection) {
die("Database connection failed: " . mysqli_connect_error();
}
You must use mysqli_connect_error(), not mysqli_error(), to get the error from mysqli_connect(), since the latter requires you to supply a valid connection.
<?php``
if(mysql_connect("localhost","root",""))
echo"connect";
else
echo "not connect";
?>
this is my code but it's not connected . give the error as warning
Warning: mysql_connect(): MySQL server has gone away in C:..\pro1.php on line 2
Warning: mysql_connect(): Error while reading greeting packet. PID=2296 in C:..\pro1.php on line 2
Warning: mysql_connect(): MySQL server has gone away in C:..\pro1.php on line 2
not connect
You can try using either MySQLi or PDO and their prepared statement to be more secure.
If you intend using just MySQL then use the below code to initialize your Database connection.
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
For reference see http://php.net/manual/en/function.mysql-connect.php
Alternatively kindly use MySQLi
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
For reference see http://php.net/manual/en/mysqli.construct.php
If you consider using PDO then try
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
foreach($dbh->query('SELECT * from FOO') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
For reference see http://php.net/manual/en/pdo.connections.php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
http://php.net/manual/en/mysqli.construct.php
remove '' after php open tag and
try like this
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('dbname'); //your database name
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>
I am trying to access localhost db from remote server .
$link = mysql_connect('192.168.65.44', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
Warning: mysql_connect(): Can't connect to MySQL server on '192.168.65.44' (4) in /home/xx/xx/xx/xx/xx
Could not connect: Can't connect to MySQL server on '192.168.65.44' (4)
The error tells that there's a problem in your host. If you are trying locally in your system, use
$link = mysqli_connect('localhost', 'root', '');
Note : mysql_ is deprecated. You need to use either Improved Mysqli (mysqli_) or PDO.
put "localhost" in place of "192.168.65.44" in
mysql_connect
Try this it will work :
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
// Set the active mySQL database
$db_selected = mysql_select_db($dbname, $db);
if (!$db_selected) {
die ("Can\'t use db : " . mysql_error());
}
Error: Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/content/41/10663841/html/storelocator/phpsqlsearch_genxml.php on line 21
Can\'t use db :
I guess, you've to connect to MySQL first:
// connect to mysql
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('cannot connect, error: ' . mysql_error());
}
// select db "xyz"
$db_selected = mysql_select_db('xyz', $link);
if (!$db_selected) {
die ('cannot select DB, error: ' . mysql_error());
}
Notice: For your future codes I recommend using PDO: http://php.net/manual/en/pdo.connections.php
I was running an example from a mysqli tutorial.
$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";
}
I was getting the error:
Warning: mysqli::mysqli(): (42000/1049): Unknown database 'world' in /home/...
What should I do to create the database?
You have to use this:
$mysqli=new mysqli("localhost","user","password") or die(".........");
if($mysqli->query("Create database if not exists MyDB")){
"Failed creating new database: ".$mysqli->connect_errno();
die();
}
$mysqli->select_db("MyDB");
$mysqli->close();
try something like this
$mysqli = new mysqli("localhost", "user", "password");
in this way you are connected to database
and put this query using mysqli Create Database database1