gpt4 book ai didi

php - PHP/MySQL 脚本中的访问被拒绝错误

转载 作者:行者123 更新时间:2023-11-29 07:02:28 25 4
gpt4 key购买 nike

我有一个自己编写的 PHP 脚本,其中一个页面可以完美连接和执行,但另一个页面会出现以下错误。我在 MAMP 安装上使用 PHP 5.3.6:

Access denied for user ''@'localhost' to database 'premind'

这是抛出错误的页面:

<html>
<head><title>User CP</title></head>
<body>
<form name="logoutbutton" method="post" action="logout.php">
<input type="submit" name="logout" value="Log out" />
</form>
<h1>User Control Panel</h1>
<h2>Below is a list of current assignments:</h2>
<?php
include('/Applications/MAMP/htdocs/premind/includes/vars.php');

session_start();
if (isset($_SESSION['emailaddress'])) {

mysql_select_db($db_name) or die(mysql_error());

$sql4 = 'SELECT `aname`, `date`, `useremail`, `aid` FROM `data`';

$result4 = mysql_query($sql4) or die("<br />" . mysql_error());

$countrows2 = mysql_num_rows($result4);

if (!$result4) {
echo "Cannot show assignments!";
}

while ($row = mysql_fetch_array($result4)) {
if ($row['useremail'] == $_SESSION['emailaddress']) {
echo $row['aid'].". ".$row['aname']." -- ".$row['date']."<br />";
echo "<br />";
}
elseif ($countrows2 == 0) {
echo "<h1>No assignments found!</h1>";
}
}

if ($countrows2 == 0) {
echo "<h1>No assignments found!</h1>";
}
}
else {
header('Location: notloggedin.php');
}
?>
<br />
<br />
<h2>Submit an assignment:</h2>
<form name="submitass" method="post" action="submitassignment.php">
<table>
<tr>
<td><p>Assignment name:</td><td></p><input name="aname" type="text" id="aname" /></td>
</tr>
</table>
<table>
<tr>
<td><p>Due date: (IN YEAR, MONTH, DAY FORMAT)</p></td><td><input name="date" type="text" id="date" /></td>
</tr>
</table>
<br />
<h2>Example of date: 2012-01-05</h2>
<br />
<input type="submit" name="submitass" value="Submit!" />
</form>
<br />
<br />
<h2>Delete and assignment:</h2>
<p>To delete an assignment, enter the number that you see before the assignment name above.</p>
<form name="deleteass" method="get" action="deleteassignment.php">
<p>Assignment ID: </p><input type="text" name="assid" id="assid" />
<input type="submit" name="deleteass" value="Delete!" />
</form>
</body>
</html>

这是 vars.php 文件:

<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="premind"; // Database name
$tbl_name="members"; // Table name

?>

最佳答案

您缺少 mysql_connect() 调用。

你是说你在重定向到这个页面的另一个文件中调用它,但即使它在上一个页面中连接,它在这个页面中也没有连接。因此,您必须在此页面上单独调用 mysql_connect,或包含执行调用的文件。

关于php - PHP/MySQL 脚本中的访问被拒绝错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9400190/

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