Question

I'm trying to connect to a database, but mysql_select_db always returns false. If I just use the first parameter of mysql_select I get the error: Access denied for user ''@'localhost' to database 'newboston', if I enter the connection link as the second parameter mysql_error returns nothing. Anyone knows what's going on?

<?php
$dbServer = 'localhost';
$dbUserName = 'root';
$dbPassword = 'password';

$database = 'newboston';
$db = mysqli_connect($dbServer, $dbUserName, $dbPassword);

$connectFailed = 'Could not connect to ' . $dbServer . '.';

if($db)
{
if(mysql_select_db($database, $db))
{
    echo 'Connected to ' . $database;
}

else
{
    echo 'Could not connect to ' . $database;
    die(mysql_error());
}
}

else
{
    echo $connectFailed;
}

?>
Was it helpful?

Solution

You're mixing mysql and mysqli:

$db = mysqli_connect($dbServer, $dbUserName, $dbPassword);

should be:

$db = mysql_connect($dbServer, $dbUserName, $dbPassword);

(actually you should be using mysqli as mysql is deprecated).

OTHER TIPS

You're mixing mysqli_* functions with mysql_* functions.

May I suggest learning PDO MySQL instead?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top