我想連接到一個PHP腳本內的MySQL數據庫。到目前爲止,我的代碼看起來像不連接數據庫
//to my knowledge, this works. I was able to echo out the correct name
$name = $_POST["name"];
$server_name = "localhost";
$user_name = //my user name
$password = //my password
$db_name = //the db name
//it passes this error check, so I am connecting properly I am assuming
$dbconn = mysql_connect($server_name, $user_name, $password)
or die ('Could not connect to database: ' . mysql_error());
mysql_select_db($db_name, $dbconn);
$query = "SELECT *
FROM brothers
WHERE name = '$name'";
//it DOES NOT make it past this one
$result = mysql_query($query)
or die('Bad Query: ' . mysql_error());
//filter through the query as a row
$row = mysql_fetch_array($result, MYSQL_ASSOC);
//echo the result back to the user
echo $row["name"];
echo $row["major"];
//close the connection
mysql_close($dbconn);
我不斷收到錯誤「沒有數據庫中選擇」,儘管我確信,我拼寫正確的數據庫名稱(我複製粘貼)。有誰知道爲什麼我的代碼可能會拋出這個錯誤?
你在上面的代碼中提到數據庫名稱? – Shyju 2012-01-06 22:11:54
你確定你的數據庫登錄信息是正確的嗎 – MrFoh 2012-01-06 22:12:49
在'mysql_select_db()'調用之後執行同樣的命令'或者死掉'('無法使用DB:'。mysql_error())'以查明那裏發生了什麼。 – CanSpice 2012-01-06 22:12:51