문제

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

?>
도움이 되었습니까?

해결책

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

다른 팁

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

May I suggest learning PDO MySQL instead?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top