php - 未连接到数据库

标签 php mysql database

我正在尝试在 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);

即使我确信我正确拼写了数据库名称(我复制粘贴),我仍然收到错误“未选择数据库”。有谁知道为什么我的代码可能会抛出此错误?

最佳答案

可能是权限问题(登录的用户可能没有数据库的读取权限):

mysql_select_db() fails unless the connected user can be authenticated as having permission to use the database.

关于php - 未连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8765131/

相关文章:

php - 为什么 Apache 在没有扩展名的调用文件上执行 php 文件

mysql - 选择经纬度值在 10 公里半径内的行?

mysql - 如何使用内连接更新选择中的字段值?

MySQL查询,用变量创建表

database - Liferay:社区页面保存在数据库的什么位置?

php - 将无限播放列表(数组)保存到数据库的最佳方法? (php mysql)

php - Facebook 的 HipHop - 它有什么用?

php - 使搜索脚本循环遍历数组而不是文本输入值

javascript - 具有回调函数参数的PHP数组到JSON

python - Aurora MySQL 列值的批量垂直更新