mr_badger Posted May 4, 2008 Share Posted May 4, 2008 I'm getting this error message 'Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /mydirectory/ on line 21 Access denied for user ''@'localhost' (using password: NO) why is it doing this. All my other pages are fine apart from this one. here is the code. <?php require"http.php"; session_start(); if ($_SESSION['access_lvl'] < 3) redirect('index.php'); ?> <script type="text/javascript"> <!-- function delBBCode(id) { window.location = "transact-admin.php?action=deleteBBCode&b=" + id; } function delForum(id) { window.location = "transact-affirm.php?action=deleteForum&f=" + id; } //--> </script> <?php $sql = "SELECT access_lvl, access_name FROM forum_access_levels" . "ORDER by access_lvl DESC"; $result = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $a_users[$row['access_lvl']] = $row['access_name']; } $menuoption = "boardadmin"; // default if (isset($_GET['option'])) $menuoption = $_GET['option']; $menuItems = array( "boardadmin" => "Board Admin", "edituser" => "Users", "forums" => "Forums", "bbcode" => "BBcode" ); echo "<p class=\"menu\">|"; foreach ($menuItems as $key => $value) { if ($menuoption != $key) { echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?option=$key\">"; } echo " $value "; if ($menuoption != $key) echo "</a>"; echo "|"; } echo "</p>"; switch ($menuoption) { case 'boardadmin': ?> <h3>Board Administration</h3> <form id="adminForm" method="post" action="transact-admin.php"> <table cellspacing="0" class="forumtable"> <tr> <th>Title</th><th>Value</th><th>Parameter</th> </tr> <?php foreach ($admin as $k => $v) { echo "<tr><td>". $v['title'] . "</td><td>" . "<input type=\"text\" name=\"". $k . "\" " . "value=\"" . $v['value'] . "\" size=\"60\">" . "</td><td>$k</td></tr>\n"; } ?> </table> <p class="buttonBar"> <input class="submit" type="submit" name="action" id="Update" value="Update"> </p> </form> <?php break; case 'edituser': ?> <h3>User Administration</h3> <div id="users"> <form name="myform" action="transact-admin.php" method="post"> Please select a user to admin:<br> <select id="userlist" name="userlist[]"> <?php foreach ($a_users as $key => $value) { echo "<optgroup label=\"". $value . "\">\n"; userOptionList($key); echo "\n</optgroup>\n"; } ?> </select> <input class="submit" type="submit" name="action" value="Modify User"> </form> </div> <?php break; case 'forums': ?> <h2>Forum Administration</h2> <table class="forumtable" cellspacing="0"> <tr><th class="forum">Forum</th><th> </th><th> </th></tr> <?php $sql = "SELECT * FROM forum_forum"; $result = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { echo "<tr><td><span class=\"forumname\">" . $row['forum_name'] . "</span><br><span class=\"forumdesc\">" .$row['forum_desc']. "</span></td><td>" . "<a href=\"editforum.php?forum=" . $row['id'] . "\">Edit</a></td><td>" . "<a href=\"#\" onclick=\"delForum(". $row['id'] . ");\">" . "Delete</a></td></tr>"; } ?> </table> <p class="buttonBar"> <a href="editforum.php" class="buttonlink">New Forum</a> </p> <?php break; case 'bbcode': ?> <h3>BBcode Administration</h3> <form id="bbcodeForm" method="post" action="transact-admin.php"> <table cellspacing="0" class="forumtable"> <tr> <th class="template">Template</th> <th class="replacement">Replacement</th> <th class="action">Action</th> </tr> <?php if (isset($bbcode)) { foreach ($bbcode as $k => $v) { echo "<tr class=\"row1\"><td>" . "<input class=\"mono\" type=\"text\" " . "name=\"bbcode_t" . $k . "\" " . "value=\"" . $v['template'] . "\" size=\"32\">" . "</td><td>" . "<input class=\"mono\" type=\"text\" " . "name=\"bbcode_r". $k . "\" " . "value=\"" . $v['replacement'] . "\" size=\"32\">" . "</td><td><input type=\"button\" class=\"submit\" " . "name=\"action\" id=\"DelBBCode\" value=\"Delete\" " . "onclick=\"delBBCode(".$k.");\">" . "</td></tr>\n"; } } ?> <tr class="row2"><td colspan="3"> </td></tr> <tr class="row2"><td> <input class="mono" type="text" name="bbcode-tnew" size="32"> </td><td> <input class="mono" type="text" name="bbcode-rnew" size="32"> </td><td> <input type="submit" class="submit" name="action" id="AddBBCode" value="Add New"> </td></tr> </table> <p class="buttonBar"> <input class="submit" type="submit" name="action" id="Update" value="Update BBCodes"> </p> </form> <?php break; } ?> </script> This is around line 21 <?php $sql = "SELECT access_lvl, access_name FROM forum_access_levels" . "ORDER by access_lvl DESC"; $result = mysql_query($sql) Link to comment https://forums.phpfreaks.com/topic/104066-need-help-with-this-code/ Share on other sites More sharing options...
gizmola Posted May 4, 2008 Share Posted May 4, 2008 Errror message seems pretty descriptive -- you don't seem to have a valid mysql connection when you're trying to execute the query. Link to comment https://forums.phpfreaks.com/topic/104066-need-help-with-this-code/#findComment-532700 Share on other sites More sharing options...
DarkWater Posted May 4, 2008 Share Posted May 4, 2008 Check: require"http.php"; And make sure the connection is getting through. Link to comment https://forums.phpfreaks.com/topic/104066-need-help-with-this-code/#findComment-532701 Share on other sites More sharing options...
mr_badger Posted May 8, 2008 Author Share Posted May 8, 2008 I have checked the require http and it looks fine. here is http.php <?php function redirect($url) { if (!headers_sent()) { header('Location: http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/' . $url); } else { die('Could not redirect; Headers already sent (output).'); } } ?> Link to comment https://forums.phpfreaks.com/topic/104066-need-help-with-this-code/#findComment-536013 Share on other sites More sharing options...
moselkady Posted May 8, 2008 Share Posted May 8, 2008 You should have a connection to the database using mysql_connect() somewhere in your code before doing mysql_query. Something like that: $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } Link to comment https://forums.phpfreaks.com/topic/104066-need-help-with-this-code/#findComment-536016 Share on other sites More sharing options...
discomatt Posted May 8, 2008 Share Posted May 8, 2008 The error explains itself. You have not attempted to connect to the MySQL database, and it has attempted to connect using default values. If you're still not sure what to do you should go back to the basics. Link to comment https://forums.phpfreaks.com/topic/104066-need-help-with-this-code/#findComment-536019 Share on other sites More sharing options...
947740 Posted May 8, 2008 Share Posted May 8, 2008 Just to add my advice, I would include the database information in an include file, so the passwords, etc. are not directly in the file you are using. Link to comment https://forums.phpfreaks.com/topic/104066-need-help-with-this-code/#findComment-536032 Share on other sites More sharing options...
discomatt Posted May 8, 2008 Share Posted May 8, 2008 That's only helpful if the file it outside of the web root... just to add. Link to comment https://forums.phpfreaks.com/topic/104066-need-help-with-this-code/#findComment-536037 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.