1

I have a PHP script that I wrote, and one of the pages connects and executes perfectly, but the other gives me the following error. I am using PHP 5.3.6 on a MAMP installation:

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

Here's the page throwing the error:

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

Here's the vars.php file:

<?php

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

?>
7
  • 2
    Where's your config lines where you set the MySQL userid and password and perform mysql_connect()? Commented Feb 22, 2012 at 17:53
  • In vars.php. One sec, I will edit them in. Note, that another page using the same vars.php connects and works perfectly. Commented Feb 22, 2012 at 17:54
  • Instead of using the system absolute path have you true a relative path? Commented Feb 22, 2012 at 17:54
  • My mysql_connect() call is in another file which, when the user login is verified, it forwards to this page throwing the error. Commented Feb 22, 2012 at 17:56
  • session_start() should be at the very top of your code.. Commented Feb 22, 2012 at 17:58

3 Answers 3

1

You are missing the mysql_connect() call.

You are saying that you called it in another file which redirected to this page, but even though it was connected in the previous page, it is not connected in this page. So you have to call mysql_connect separately on this page, or include the file that does the calling.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Really odd. When I wrote this on my Windows machine, I tried adding mysql_connect() in that file, and it gave me an error about already being connected, but now it seems to work. Thanks!
1

There is no database user set when connecting, something is wrong with your database config and the way you connect to the database.

Make sure your database credentials are ready and included, when calling mysql_connect().

Edit: You are not calling mysql_connect() at all in your code.

3 Comments

My mysql_connect() call is in another file which, when the user login is verified, it forwards to this page throwing the error. It's already connected to MySQL once it gets to this page.
@DustinStalin: You have to call mysql_connect() on every page! It does not suffice to connect once on another page.
Yes, it's included. Would posting that file as well be helpful?
0

I think the problem you are running in to has to do with the path you have to your vars.php assuming (based on comments) that your vars.php file has the connection information. Try using a relative path (such as ../vars.php / which is just an example) instead of a system path. Its possible (probable) that the site does not have access to the system path.

EDIT

I noticed you posted the vars.php file. Now we can safely say that mysql_connect is not being called in the code. In either file try adding:

mysql_connect($host,$username,$password);

5 Comments

Thanks for the help! Do I need to add the three dots for every directory? I have not used relative paths before, so please excuse my ignorance.
I tried that, but the issue is that you do not access the posted script directly. You are forwarded to it by another script, which connects to the database, and then if I add mysql_connect again, it throws an error, presumably because we're already connected.
Thanks for the reply. I'm assuming that everything is in Applications/MAMP/htdocs/premind. If not they need to be. Anyway, say the first file you have listed above (lets call it index.php) is in Applications/MAMP/htdocs/premind/index.php. It would need to call "includes/vars.php" as the path.
Even if you are forwarded by another script you need to open mysql with a connect. Maybe place the connect in vars.php?
Seems to be working now. Thanks for the advice on relative paths though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.