This is my code:
<?php
$connection = mysqli_connect('localhost','root','','cms');
if($connection){
echo "Error : unable to connect to mysqli.".PHP_EOL;
echo "Debugging errno".mysqli_connect_errno().PHP_EOL;
echo "Debugging error".mysqli_connect_error().PHP_EOL;
exit;
}
?>
And I'm getting the following error:
Error : unable to connect to mysqli. Debugging errno0 Debugging error
Brother your connection is successful , you just need to use -
if(!$connection) instead of if($connection)
Related
I have Apache (with PHP) and MySQL installed and running on my Raspberry Pi. I've done some simple tests with PHP, and it seems to be working. And MySQL is working perfectly from the terminal, and even another computer.
I made this PHP file:
<?php
echo("Connecting");
$connection = new mysqli("127.0.0.1", "admin", "password", "test");
if ($connection->connect_error) {
die("Connection error");
}
echo("Connection successful");
?>
Yet when I go to this page in my web browser, all I see is "Connecting". If I comment out the connection command, I see "Connecting Connection successful" in the browser.
It seems as if the PHP code stops running or hangs at the connection command.
Any ideas why I'm having this strange behavior?
Try adding these lines at the top: error_reporting(E_ALL);
ini_set('display_errors',1);
I hope You should do following
if ($connection->connect_error) {
echo "Connection error";
}else{
echo "Connection successful";
}
Try this instead
<?php
$link = mysqli_connect("127.0.0.1", "admin", "password", "test");
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);
?>
This is my code to connect to Azure:
<?php
$servername='myproject.database.windows.net';
$username='somename';
$password='somepassword';
$database='some_db';
$conn=New mysqli($servername,$username,$password,$database);
//Connection error handling:
// echo #mysqli_ping($conn) ? 'true' : 'false';
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
} else {
// echo "Connection success";
}
?>
I get the error:
Connection failed: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
I have tried launching this php from a WAMP server and from azurewebsites but both do not seem to work. It works with PDO but I have some pages already setup with mysqli_fetch_assoc queries and would like to preserve them.
Any suggestions?
When connecting to localhost with this code, I get an error message. I'm unable to connect to MYSQL and I'm not sure why:
<?php
//connect
$link = mysqli_connect("$localhost", "$root", "$password", "$database");
if (!$link)
{
$output = 'Unable to connect to the database server.';
include 'output.html.php';
exit();
}
if (!mysqli_set_charset($link, 'utf8'))
$output = 'Unable to set database connection encoding.';
include 'output.html.php';
exit();
}
if (!mysqli_select_db($link, '$database'))
{
$output = 'Unable to locate the Teamster database.';
include 'output.html.php';
exit();
}
$output = 'Database connection established. ';
include 'output.html.php';
?>
I get this error message. This is what populates when I refresh the page, above a form that I've created. Is this a problem of connectivity or coding?
//check connection //if (mysqli_connect_errno($con)) // { echo "Failed to connect to MySQL: " . mysql_connect_error(); // } //
if you are getting:
//check connection //if (mysqli_connect_errno($con)) // { echo "Failed to connect to MySQL: " . mysql_connect_error(); // } //
then this is your problem:
$link = mysqli_connect("$localhost", "$root", "$password", "$database");
where loclhost, username, password, database combination is not correct.
also make sure you are on the right port and that you are allowed to connect with those credential.
A "cannot connect to db" is always due to some combination of that line and not because of code since you failed on if (!$link)
I (don't think I can) post comments, but it would help if you posted the exact error message.
eg the output of
mysql_connect_error()
From what I can tell though, you should make sure that the $localhost is set to something (probably "localhost", and that you're using the correct username and password.
never output a fixed error message, when the system could TELL you what's wrong:
$output = 'Unable to connect to the database server:' . mysqli_connect_error();
I am new to PHP and am trying to connect to a oracle database on some server.
However the php script is not executing properly.
<?php
echo "started \n";
// Create connection to Oracle
$conn = oci_connect("username", "password", "abc.def.ghi.com");
if (!$conn) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else {
print "Connected to Oracle!";
}
// Close the Oracle connection
oci_close($conn);
?>
It prints started, but after that it does not print any error or "Connected to Oracle".
php -l filename.php gives "no syntax errors".
Was PHP compiled with oracle support? Check your error log, if not you'll error out on oci_connect() and not reach anything else.
(display errors would also be of help)
I tried running the following code: (http://localhost/read.php)
<html>
<body>
<?php
$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
if (mysql_query("CREATE DATABASE testphp",$link))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
?>
</body>
</html>
and got the following error:
Fatal error: Call to undefined function mysql_connect() in
C:\Program Files (x86)\ApacheSoftware Foundation\Apache2.2\htdocs\read.php
on line 5
Look at you phpinfo(). Most likely mysql extensions is not there.
And while you are at it, you could just drop the ancient mysql_* way ow doing thing and learn how to use PDO and prepared statements. It's an abstraction API for database connection and interaction.
your mysql-extension for php is not loaded! check that in your php.ini.