Connecting PHP TO MYSQL ERROR message - php

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();

Related

HY000/1049 Unknown database when using mysqli_connect

<?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.

Unable to connect to MySQL database but able to connect to database server

I am trying to build a dynamic website for connecting pages with database I used the code as follows. connection with server is Ok but unable to select database. data base name, user id, password, ip of host all gave but not working. please help....
define('DB_NAME', '');
define('DB_USER', '');
define('DB_PASSWORD', '');
define('DB_HOST', '');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
else {
echo 'connected to server..................'; }
$db_selected = mysql_select_db($DB_NAME, $link);
if ($db_selected) {
print "Database Found";
}
else {
print "Database NOT Found";
}
Mysql has been deprecated and will eventually be removed. Consider using PDO or MySqli.
Here is a link to the PHP documentation page for making connections to a database using MySqli.
http://php.net/manual/en/mysqli.quickstart.connections.php
If you follow the instructions carefully you 'should' be able to connect. If not, perhaps you can post the error message you receive.
Try to give proper credential to connect to mysql server. mysqli_connect("localhost","root","password","db_name");

Internal server error while connecting to my database online

I'm using the following code to connect to a database that I have setup on godaddy.com. But when I try to run the script I get "Internal Server Error".
<?php
$db_conx = mysqli_connnect('MY_WEBSITE.secureserver.net', 'MY_USERNAME',
'MY_PASSWORD', 'DATABASE_NAME');
// Evaluate the connection
if (mysqli_connect_errno()) {
echo "cannot connect to database";
echo mysqli_connect_error();
exit();
}
else {
echo "Successful database connection, happy coding!!!";
}
?>
I'm not sure what i'm doing wrong

How to check if mysqli_connect was successful or not?

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());
}

mysql not connect with php scripts?

<?php
$cons=mysql_connect("localhost","root","");
mysql_select_db("infogallery") or die('Not Connected to the data base:'.mysql_error());
?>
I write above code for connection with mysql but when i run this scripts..nothing display on the brouser...what can i do for the connection with mysql....
If nothing is displayed, then it means it succeeded. Add more code which queries the database and displays some results.
Don't connect as the root account. Create an account specifically for playing around with.
Once you've done that, modify your code as follows:
$cons = mysql_connect('localhost', 'username', 'password');
if ($cons === FALSE) {
die("Failed to connect to MySQL: " . mysql_error());
}
mysql_select_db(etc.....);
You don't check if the connection failed, then try to do a database operation on that potentially failed connection. The or die(...) you have will only show the error caused by the select attempt, and the error message from the failed connection will be lost.
I like to just do
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("infogallery") or die(mysql_error());
echo "So far, so good.";
How about something like the following:
<?php
try {
$cons = mysql_connect("localhost","username","password");
} catch ($e) {
die('Failed to connect to the database: ' . mysql_error());
}
try {
mysql_select_db("infogallery", $cons);
} catch ($e) {
die('Failed to select the database: ' . mysql_error());
}
?>

Categories