<?php
/* die/exit operation*/
mysqli_connect('localhost','root','') or die ('The connection is lost');
echo 'connected';
?>
mysqli_connect or die functions working together fine in case of the both correct and incorrect host names.But no matter what username I am using,it is always showing 'connected'.Can anyone please tell me why it is happening?
I don't think your die statement will ever be reached.
mysql_connect is the alias for mysqli::connect and the object will be made.
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
To get a connection failure:
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
Related
Here's my code.
<?php
$server = "localhost";
$uname = "replace it with anything";
$pswd = "";
$conn = mysqli_connect($server, $uname, $pswd);
if(!$conn){
die('Caught');
}
else{
die('Connected');
}
?>
No matter what I passed in the mysqli_connect() as username. It always returns true. In the case of the wrong password, it shows an error that accesses denied, but I don't know why, no matter what I enter in the username, it always returning true.
It does not return a boolean but an object which represents the connection. You can then check the object for connectivity. From the manual:
<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
/*
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
*/
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
Try
<?php
$mysqli = new mysqli("host", "username", "password", "database") or die($mysqli->error());
if ($mysqli->connect_errno) {
echo "error";
exit();
}
?>
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();
}
<?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'm connecting to Mysql Database With following code. I'm using mysqli. But It's not connecting to mysql database.
define("HOST", "hostname");
define("USER", "username");
define("PASS", "password");
define("DB", "dbname");
$link = mysqli_connect(HOST, USER, PASS) or die("Couldn't make database connection.");
$db = mysqli_select_db($link, DB) or die("Couldn't select database");
Strange things is that when I delete or die("Couldn't make database connection.") and or die("Couldn't select database"); then it's connect to db. Why ? Is there anything I'm doing wrong ?
This is the proposed way to handle the situation:
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
This is the code to connect to the mysql database:
$con = mysql_connect("", "", "");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("", $con);
I want to echo 'connected'/'disconnected' depending on state.
How can it be done?
Do it Like this
if ($con) {
echo 'connected';
} else {
echo 'not connected';
}
Or try this
echo $con ? 'connected' : 'not connected';
Firstly, use the mysqli_xxx() functions instead of the old obsolete mysql_xx() functions.
This is strongly recommended anyway because the old library is in the process of being deprecated, but will also make your question easier to answer. (you could also use the PDO library, for which the answer will be similar, but for this answer I'll stick with mysqli for simplicity)
With the mysqli library, you get a variable that contains your DB connection, which you can examine at any point.
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
Now, you can query the $mysqli variable to find out what is happening.
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
Later on, you can query the variable using the ping command: http://www.php.net/manual/en/mysqli.ping.php
$mysqli->ping();
or maybe the stat command if you want more info: http://www.php.net/manual/en/mysqli.stat.php
Try using mysql_ping
mysql_ping — Ping a server connection or reconnect if there is no
connection
something like this should help you
if(mysql_ping($con))
echo 'connected';
else
echo 'disconnected';