Jump to content

[SOLVED] I can't seem to use any MySQL functions after connecting to a database.


Recommended Posts

Hello, I've been having an issue in which I can not successfully call any MySQL functions within PHP after establishing a connection to a database.

 

After taking a quick look around this forum I was unable to find a solution, so without further ado, I'll just dive right into the code;

 

The following is a very simple login script (I know it can obviously be a lot better and more efficient, but I just drafted it hastily because it's only to be used by one or two people):

<?php
include("include/sqlconfig.php");
$ip = $_SERVER['REMOTE_ADDR'];
$un = $_POST['u'];
$pw = $_POST['p'];
$un = mysql_real_escape_string(stripslashes($un));
$pw = mysql_real_escape_string(stripslashes($pw));
if (strlen($_POST['u']) >= 4 && strlen($_POST['p']) >= 4) {
	if (strlen($_POST['u']) <= 20 && strlen($_POST['p']) <= 20) {
		if(ctype_alnum($_POST['u']) && ctype_alnum($_POST['p'])) {
			$q = "SELECT * FROM `users` "
				."WHERE `username`='" . $un . "' "
				."AND `password`=PASSWORD('" . $pw . ")' "
				."LIMIT 1";
			$r = mysql_query($q);
			if ($obj = @mysql_fetch_object($r)) {
				$_SESSION["valid_id"] = $obj->r_id;
				$_SESSION["valid_user"] = $obj->username;
				$_SESSION["valid_admin"] = $obj->admin;
				$_SESSION["valid_time"] = time();
				$q = "UPDATE `users` SET `lasttime`=NOW() `loginIP`=" . $ip . " WHERE `r_id`='" . $obj->r_id . "'";
				$r = mysql_query($q);
				header("Location: " . $indexPage);
			} else {
				$q = "INSERT INTO `logs` (`time`, `message`) "
					."VALUES (NOW(), 'Failed login attempt; IP Address: " . $ip . "; Username: " . $un . "; Password: " . $pw . "; Error: The login credentials are incorrect.'";
				$r = mysql_query($q);
				header("Location: " . $indexPage . "?action=error&m=4");
			}
		} else {
			$q = "INSERT INTO `logs` (`time`, `message`) "
				."VALUES (NOW(), 'Failed login attempt; IP Address: " . $ip . "; Username: " . $un . "; Password: " . $pw . "; Error: The inputs are not alphanumeric.'";
			$r = mysql_query($q);
			header("Location: " . $indexPage . "?action=error&m=3");
		}
	} else {
		header("Location: " . $indexPage . "?action=error&m=2");
	}
} else {
	header("Location: " . $indexPage . "?action=error&m=1");
}
?>

 

Here is a censored copy of the code within "include/sqlconfig.php":

<?php
$host = "localhost";
$user = "********_Cheese";
$pass = "*******";
$db = "********_Database";
$ms = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
?>

 

And finally, here are the errors I receive when testing it out using post data from a simple form with invalid credentials:

NOTE: The line numbers may be off by two or three lines.

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user '********'@'localhost' (using password: NO) in /home/********/public_html/include/adminlogin.php on line 6

 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/********/public_html/include/adminlogin.php on line 6

 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user '********'@'localhost' (using password: NO) in /home/********/public_html/include/adminlogin.php on line 7

 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/********/public_html/include/adminlogin.php on line 7

 

Warning: mysql_query() [function.mysql-query]: Access denied for user '********'@'localhost' (using password: NO) in /home/********/public_html/include/adminlogin.php on line 16

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/********/public_html/include/adminlogin.php on line 16

 

Warning: mysql_query() [function.mysql-query]: Access denied for user '********'@'localhost' (using password: NO) in /home/********/public_html/include/adminlogin.php on line 30

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/********/public_html/include/adminlogin.php on line 30

 

Warning: Cannot modify header information - headers already sent by (output started at /home/********/public_html/include/adminlogin.php:6) in /home/********/public_html/include/adminlogin.php on line 31

 

Disregard the last error in the list, as it would most likely not exist if the previous errors were not displayed.

 

Anyways, as you can see, I don't get any errors when establishing a connection to the database, but all of the MySQL functions that I call are trying to connect to a database using default information. (As if I didn't already establish a connection) :-\

 

By the way, here is some information about the MySQL server I am using:

# Server version: 5.0.81-community

# Protocol version: 10

# Server: Localhost via UNIX socket

 

Any help on this matter is greatly appreciated, and I hope I didn't overlook anything.

 

Thanks in advance,

- CheeseKeg

Best guess is your actual include/sqlconfig.php, relative to the script that is including it, isn't the code you posted. You either have more than one and the actual one has nothing in it or you are using short open tags <? in it and the php code is not being seen as php code.

Best guess is your actual include/sqlconfig.php, relative to the script that is including it, isn't the code you posted. You either have more than one and the actual one has nothing in it or you are using short open tags <? in it and the php code is not being seen as php code.

 

Trust me, both files exist in their respective locations. When I add code to echo within "sqlconfig.php", it does right before all of the errors.

 

Also, I never don't use the short open tag for fear of future migration issues, and even if I did, it wouldn't be a problem because my current host has it enabled.

 

Thank you for the quick response, though.

 

[attachment deleted by admin]

The only way you will get those error messages is if there is no database connection. The only way you won't have a db connection is if one is not created or if you close it.

 

Is the following posted include statement exactly like it is in the file or did you alter is, such as removing a http://yourdomain.com/ from it -

 

   include("include/sqlconfig.php");

 

 

The only way you will get those error messages is if there is no database connection. The only way you won't have a db connection is if one is not created or if you close it.

 

Is the following posted include statement exactly like it is in the file or did you alter is, such as removing a http://yourdomain.com/ from it -

 

   include("include/sqlconfig.php");

 

 

 

After doing a few quick checks after seeing your response, I noticed something odd.

I was accidentally uploading the adminlogin.php into the include directory. After removing it I noticed that my form was pointing to a page that no longer existed: 'include/adminlogin.php'.

 

So it turns out that I was making something more complicated than it needed to be, and overlooked the fact that I was uploading file to an incorrect directory during the past few times that I modified it.

 

Also, when I said that I was able to make the page echo, I was getting errors from an older piece of code that I had previously debugged by accident, and had not looked at them, and therefore didn't see that they were different errors.

 

But anyways, problem solved.

Thank you for your responses.  :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.