treehouse : what would you like to learn today?
Web Design Web Development iOS Development

PHP Error Warning

  • Hi, I'm having some trouble with a script I'm making I see this error:

    Warning: mysql_query() [function.mysql-query]: Access denied for user 'rettro'@'localhost' (using password: NO) in /home/rettro/public_html/admin/post.php on line 52

    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/rettro/public_html/admin/post.php on line 52

    I'll show you the page If that helps?
    <?php require('config.php'); ?>
    <?php include('header.php'); ?>
    <?php

    if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    die(\"Invalid ID specified.\");
    }

    $id = (int)$_GET['id'];
    $sql = \"SELECT * FROM php_blog WHERE id='$id' LIMIT 1\";

    $result = mysql_query($sql) or print (\"Can't select entry from table php_blog.<br />\" . $sql . \"<br />\" . mysql_error());

    while($row = mysql_fetch_array($result)) {

    $date = date(\"l F d Y\", $row['timestamp']);

    $title = stripslashes($row['title']);
    $entry = stripslashes($row['entry']);
    $password = $row['password'];
    $get_categories = mysql_query(\"SELECT * FROM php_blog_categories WHERE `category_id` = $row[category]\");
    $category = mysql_fetch_array($get_categories);

    if ($password == 1) {
    if (isset($_POST['username']) && $_POST['username'] == $my_username) {
    if (isset($_POST['pass']) && $_POST['pass'] == $my_password) {
    ?>
    <div class=\"post\">
    <h1><a href=\"post.php?id=<?php echo $id; ?>\"><?php echo $title; ?></a></h1>


    <div class=\"postmeta\">Posted on <?php echo $date; ?>. <?php

    $result2 = mysql_query (\"SELECT id FROM php_blog_comments WHERE entry='$id'\");
    $num_rows = mysql_num_rows($result2);

    if ($num_rows > 0) {
    echo \"<a href=\\"post.php?id=\" . $id . \"\\">\" . $num_rows . \" Comments</a>\";
    }
    else {
    echo \"<a href=\\"post.php?id=\" . $id . \"\\">Leave a Comment</a>\";
    } ?></div>
    <?php echo $entry; ?><br />

    </div>
    <?php

    }
    else { ?>

    <p>Sorry, wrong password.</p>

    <?php
    }
    }
    else {
    echo '<div class=\"post\"><h1>' . $title . '</h1>';

    printf(\"<blockquote>This is a password protected blog post. If you have a password for the post, enter it below.</blockquote>\");

    printf(\"<form method=\\"post\\" action=\\"post.php?id=%s\\"><p><strong><label for=\\"username\\">Username:</label></strong><br /><input type=\\"text\\" name=\\"username\\" id=\\"username\\" /></p><p><strong><label for=\\"pass\\">Password:</label></strong><br /><input type=\\"password\\" name=\\"pass\\" id=\\"pass\\" /></p><p><input type=\\"submit\\" name=\\"submit\\" id=\\"submit\\" value=\\"submit\\" /></p></form>\",$id);
    print \"</div>\";
    }
    }
    else { ?>

    <div class=\"post\">
    <h1><a href=\"post.php?id=<?php echo $id; ?>\"><?php echo $title; ?></a></h1>


    <div class=\"postmeta\">Posted on <?php echo $date; ?>. Posted in <?php echo $category['category_name']; ?>. <?php

    $result2 = mysql_query (\"SELECT id FROM php_blog_comments WHERE entry='$id'\");
    $num_rows = mysql_num_rows($result2);

    if ($num_rows > 0) {
    echo \"<a href=\\"post.php?id=\" . $id . \"\\">\" . $num_rows . \"Comments</a>\";
    }
    else {
    echo \"<a href=\\"post.php?id=\" . $id . \"\\">Leave a Comment</a>\";
    } ?></div>
    <?php echo $entry; ?><br />

    </div>

    <?php
    }
    }
    $commenttimestamp = strtotime(\"now\");
    $sql = \"SELECT * FROM php_blog_comments WHERE entry='$id' ORDER BY timestamp\";
    $result = mysql_query ($sql) or print (\"Can't select comments from table php_blog_comments.<br />\" . $sql . \"<br />\" . mysql_error());
    while($row = mysql_fetch_array($result)) {
    $timestamp = date(\"l F d Y\", $row['timestamp']);
    printf('<div class=\"comment\">');
    printf(\"<h3><a href='%s'>%s</a>. On %s</p></h3>\", stripslashes($row['url']), stripslashes($row['name']), $timestamp);
    print(\"<p>\" . stripslashes($row['comment']) . \"</p>\");
    printf(\"</div>\");
    }

    ?>
    <div class=\"post\">
    <form method=\"post\" action=\"process.php\">

    <p><input type=\"hidden\" name=\"entry\" id=\"entry\" value=\"<?php echo $id; ?>\" />

    <input type=\"hidden\" name=\"timestamp\" id=\"timestamp\" value=\"<?php echo $commenttimestamp; ?>\">

    <strong><label for=\"name\">Name:</label></strong> <br /><input type=\"text\" name=\"name\" id=\"name\" size=\"40\" /><br /><br />

    <strong><label for=\"email\">E-mail:</label></strong> <br /><input type=\"text\" name=\"email\" id=\"email\" size=\"40\" /><br /><br />

    <strong><label for=\"url\">URL:</label></strong> <br /><input type=\"text\" name=\"url\" id=\"url\" size=\"40\" value=\"http://\" /><br /><br />

    <strong><label for=\"comment\">Comment:</label></strong> <br />
    <textarea cols=\"40\" rows=\"5\" name=\"comment\" id=\"comment\"></textarea></p>

    <p><input type=\"submit\" name=\"submit_comment\" id=\"submit_comment\" value=\"Add Comment\" /></p>

    </form></div>

    <div class=\"post\"><center><?php $sql_prev = \"SELECT * FROM php_blog WHERE id < '$id' ORDER BY id DESC LIMIT 1\";
    $result_prev = mysql_query ($sql_prev) or print (\"Can't select previous entry id table php_blog.<br />\" . $sql_prev . \"<br />\" . mysql_error());

    while ($row = mysql_fetch_array($result_prev)) {
    $prev = $row['id'];
    }

    if (isset($prev)) {
    // print a previous link
    printf(\"<a href=\\"post.php?id=%s\\">Previous </a> \", $prev);
    }
    else {
    // just print the word \"previous\"

    print \"Previous \"; ?> |

    <?php

    $sql_next = \"SELECT * FROM php_blog WHERE id > '$id' ORDER BY id LIMIT 1\";
    $result_next = mysql_query ($sql_next) or print (\"Can't select next entry id table php_blog.<br />\" . $sql_next . \"<br />\" . mysql_error());

    while ($row = mysql_fetch_array($result_next)) {
    $next = $row['id'];
    }

    if (isset($next)) {
    // print a next link
    printf(\"<a href=\\"post.php?id=%s\\"> Next</a>\", $next);
    }
    else {
    // just print the word \"next\"

    print \" Next\";
    }
    } ?></center></div>
    <?php include('footer.php'); ?>


    I'm really unsure on what do do, Michael. :roll:
  • I don't know for sure. Is it possible that you don't have permissions set up on the database for that user? does retro have full read, write access
  • As cybershot said above, a "Access denied for user" error usually indicates either incorrect permissions or an incorrect database username or password. I'd double check your database login details and make sure they are correct.