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());
}
Related
I want to create a single connection page that i could configure this page once each time and all of the pages that needs connection to mysql uses this page? like wordpress that it has just one configuration page.
if yes how can i do that ?
<php
$connection = new mysqli ('SERVER','DB_USERNAME','DB_PASS','DB');
$connection -> set_charset('UTF-8');
if ($connection->connect_errno)
{
printf ('Connect failed: %s\n', $connection->connect_error);
exit();
}
That's all you need in your connection.php. You can include it with
include('connection.php');
You can find many tutorials for mysqli!
You can simply make a connetion.php and in that page put your connection functions:
$connection = mysql_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD);
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db(DB_NAME,$connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
//and any other config
then you can easily do this in anypage you want:
require_once("connection.php")
or
include("connection.php");
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.
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();
<?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());
}
?>
I'm trying to connect to a mysql database with php and myadmin. I've tried a lot of codes I could find online, but I just can't put this thing to work...
Can anyone please tell me what I might be doing wrong?
this is the php script I am using:
<?php
$useremail = $_POST["useremail"];
$password = $_POST["password"];
if($useremail && $password){
// open database
$connect = mysql_connect("localhost", "carlos", "nenem");
if(!$connect){
die ("Not able to connected to the database: " .mysql_error());
}
// select database
$select_db = mysql_select_db("vergilioDB", $connect);
if(!$connect_db){
die("Not able to connect to the database: " .mysql_error());
}
mysql_close($connect);
} else {
die("Please enter useremail and password, or REGISTER if you are a new user!");
}
?>
Carefull, $select_db != $connect_db
The variable names are different, rewrite to:
$select_db = mysql_select_db("vergilioDB", $connect);
if(!$select_db){
die("Not able to connect to the database: " .mysql_error());
}
note that you are closing your MySQL connection regardless of anything else before you ever use it for anything..
mysql_close($connect);
You likely want to separate your connection logic from your login logic. You likely need the DB connection to validate the password no matter what and your DB connection should be the first thing you do to make sure you can even DO anything with a given page...