<?php
$con = mysqli_connect("localhost","root","password","shoutit", "80");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
When I try to run this I get the error message of
Unknown database.
How come something off the official documentation doesn't work? I couldn't find a solution for this. Adding an "80" port makes it load forever.
Related
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?
I'm trying to connect php with mysql so I write the code like this:
<?php
// Create connection
echo "1";
$con=mysqli_connect("localhost","root","pwd");
echo "2";
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
but i dont get any response, there is only a "1" on the page. I'm sure that the uname and pwd is correct. Any advices?
Use mysqli_connect function instead of mysql_connect,
<?php
// Create connection
$con=mysqli_connect("example.com","root","pwd","my_db");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Seems like the mysqli extension not enabled in your php.ini configuration.
After enabling the extension restart the Apache server and try again the running the code.
I'm using cPanel server and I'm getting this error.
Lost connection to MySQL server at 'reading initial communication packet', system error: 0
Although i read his question Lost connection to MySQL server at 'reading initial communication packet', system error: 0 , I still didn't get the solution. when i run my php code, it returns
"Connection successfulLost connection to MySQL server at 'reading initial communication packet', system error: 0"
here's the code:
$con = mysql_connect('mysite.com:2082', 'me#mysite.com', 'passwordhere', 'dbname');
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "Connection successful";
}
if ($selected = mysql_select_db("dbname",$con) or die(mysql_error())){
echo "Your database name is dbname";
}
mysqli_close($con);
thanks for answering.
I was able to sort it out. here's the code:
$con = mysql_connect('localhost', 'serverusername', 'serverpassword');
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$selected = mysql_select_db("db_name",$con) or die(mysql_error());
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'm writing an installer script and I need to check if the user entered correct database information and therefore need to check if mysqli_connect was successful or not. A simple if-else will suffice.
What about this:
<?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();
}
?>
Like so:
if( ! $db = mysqli_connect(...) ) {
die('No connection: ' . mysqli_connect_error());
}