<?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());
}
?>
Related
How can I catch and ignore errors when connecting to a database and display a simple text message instead?
My connection looks like this:
$connection = mysqli_connect($dbhost, $dbuser, $dbpw, $dbname);
If the connection fails I want it simply to echo "Error connecting to the database" instead of the big error box.
EDIT:
Although error_reporting(0) did work, what worked better was just putting '#' in front of mysqli_connect:
$connection = #mysqli_connect($dbhost, $dbuser, $dbpw, $dbname);
if (!$connection) {
$connection_status = 'Connection to database failed';
} else {
$connection_status = null;
}
?>
<?php echo $connection_status; ?>
Taken from the documentation of mysqli_connect:
Examples
Example #1 mysqli_connect() example
<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
mysqli_connect:
$connection = mysqli_connect($dbhost, $dbuser, $dbpw, $dbname);
if (!$connection)
{
error_reporting(0);
die("Error: Unable to connect to MySQL." . PHP_EOL);
}
To disable "big error boxes" (I assume standart messages?), write error_reporting(0); above this.
You may set your custom handler for errors
register_shutdown_function("shutdownHandler");
function shutdownHandler()
{
if (!is_null($e = error_get_last())).
{
echo $e['message'];
}
}
Newish to php. I have been trying to query a database, and I keep getting the exception thrown that the query could not be completed. I checked to make sure I was connecting to the database, and everything looked fine, until I dug deeper. It appears that the my code tells me that I am connecting to the database regardless of what I put in for a password, username, or even if I do not have this data defined. I don't get it. Originally I had the following code in a function, but I put it no its own page to debug:
<?php
echo'this is working so far <br>';
/*$db = 'fake';
$host = 'localhost';
$password = 'wrong';
$user = 'root';
*/
$result = new mysqli($host, $user, $password, $db);
if(!$result){
echo 'did not connect to database';
throw new Exception('Could not connect to database');
}
else{
echo'connected to database';
return $result;
}
It always tells me I am connected to the database..
Because you are mixing Object oriented style with Procedural style To check database connection
Procedural style
<?php
$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);
?>
Object oriented style
<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
?>
Read http://php.net/manual/en/mysqli.construct.php
Note:
OO syntax only: If a connection fails an object is still returned. To check if the connection failed then use either the mysqli_connect_error() function or the mysqli->connect_error property as in the preceding examples.
Source
That means if($result) check is always true no matter what. So no, you don't have that database connection but you are verifying it incorrectly leading you to believe you do.
Your check should be
if($result->connect_error)
// no luck
else
// game on
You should check connect_errno property which stores the error code from last connect call.
$mysqli = new mysqli($host, $user, $password, $db);
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
I am having trouble connecting to my localhost database with php. It feels like I have followed every tutorial there is.
current php code:
<?php
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="webutvshop";
$username="dbconnect";
$password="password";
//DO NOT EDIT BELOW THIS LINE
$link = mysql_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysql_error());
}
else{
echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}
$db_selected = mysql_select_db($database, $link);
if (!$db_selected) {
die ('Can\'t select database: ' . mysql_error());
}
else {
echo 'Database ' . $database . ' successfully selected!';
}
mysql_close($link);
?>
The issue stands, when I acess the file locally on my computer I do not get any answer at all from it. I've tried with many other, yet I do not get any answers from them!
I need help in order to keep working on my schoolproject, thanks.
Stop using mysql_*, because they are deprecated officially. Use mysqli_* or PDO for this purpose. An example:-
<?php
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="stackquestion";
$username="root";
$password=""; // check once with empty password and once with some password that you tried already
//DO NOT EDIT BELOW THIS LINE
$link = mysqli_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysqli_connect_error());
}
else{
echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}
$db_selected = mysqli_select_db($link,$database);
if (!$db_selected) {
die ('Can\'t select database: ' . mysqli_error($link));
}
else {
echo 'Database ' . $database . ' successfully selected!';
}
mysqli_close($link);
?>
Output:- http://prntscr.com/7cbr5j
Can you also verify you can connect to the database from a command line:
mysql -u dbconnect -ppassword -h localhost webutvshop
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);
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());
}