gpt4 book ai didi

php - 未连接到数据库

转载 作者:行者123 更新时间:2023-11-29 14:37:22 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com