Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My SQL, website 'unable to select database'

I'm creating a My SQL database, I've created a database named login and the table name login1. Every time I try to load my page there is an error unable to select database, which I don't understand. In my last line of the config.php it says:

$db = mysql_select_db($SETTINGS["login"], $connection) or die ('request "Unable to select database."'); ?>

I can't see any other mistakes that I've made in the code. Thanks

like image 668
Iona Ryder Avatar asked Mar 14 '26 00:03

Iona Ryder


1 Answers

You must coonect to server like this.

// database connection info
$conn = mysql_connect("localhost","user","pass") or    trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db("databae",$conn) or trigger_error("SQL", E_USER_ERROR);

or better use PDO or MySQLi. Simple example:

$con = mysqli_connect("localhost", "username", "password") or die("Connection Problem" . mysqli_errno($con));
$database = mysqli_select_db($con, "database_name") or die("SQL Problem" . mysqli_error($con));

Or with PDO:

<?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
   $conn = new PDO("mysql:host=$servername;dbname=database", $username, $password);
   $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   echo "Connected successfully";
}
catch(PDOException $e)
{
   echo "Connection failed: " . $e->getMessage();
}
?> 
like image 186
Goro Avatar answered Mar 16 '26 13:03

Goro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!