0

Good day,

I am getting an error on a log-in script:

mysql_query(): 4 is not a valid MySQL-Link resource

I've done some research on this and followed such advice as:

  • ensuring the mysql connection is closed after the results have been returned
  • testing any sql strings I define in phpMyAdmin or some other means directly against the database so I can determine if there's an error. This has proven that the syntax is correct

I have a file for constants and other functions that are universal to the site. This is being loaded with require_once. Database settings for this application are being defined:

define ("DB_HOST","localhost"); // set database host
define ("DB_USER","[I]username[/I]"); // set database user
define ("DB_PASS","[I]password[/I]");  // set database password
define ("DB_NAME","[I]db_name[/I]"); // set database name

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");

My first query in the log-in is to compare the submitted userid or e-mail and password against the database:

if (strpos($data['user_email'],'@') === false) {
		$user_cond = "user_name='$data[user_email]'";
		} else {
		$user_cond = "user_email='$data[user_email]'";
	}//end if...else on string position of user_email
	
	$sql = "SELECT `id`, `pwd`, `firstname`, 
		`user_name`, `approved`, `user_level`, `salt`
		 FROM users
		 where $user_cond AND `banned` = '0'";
	$record = mysql_query($sql, $link) or die(mysql_error());
	$num = mysql_num_rows($record);
	mysql_close($link);

I've checked in an IDE with a debugger, $user_cond is defining properly and concatenating properly in the $sql string.

Stepping through the code, the IDE tells me the value of $link is 4 and it remains so even after the close.

My script process log-in data and provided everything checks out all right will then insert some information in a session_log. (I am using a table-based session tracking as opposed to cookies.)

It is at the end of this block of code below that I receive:

mysql_query(): 4 is not a valid MySQL-Link resource

while(!$done) {
	$done = true;
	$sql = "insert into session_log
		(session_id,
		user_id,
		remote_ip,
		session_status,
		start,
		last_access)
		values (
		'$t',
		'$uid',
		'$_SERVER[REMOTE_ADDR]',
		'8',
		now(),
		now())";
		mysql_query($sql, $link) or $done = false;
	if (!done) {
		$t = sprintf("%.12f",microtime(true));
	}
}

Again, allowing the script to process variables and concatenate them into the string, I know I am getting a valid string. This is verified by copying the $sql variable from the IDE and testing more directly in phpMyAdmin.

Any ideas?

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Zexsz
0

I'm not by any means an expert. But, it appears you may be closing your mySQL connection prematurely. You should close your connection at the end of the script. Doing that insures all database calls are able to connect. In the situation of a multi-database script I would create two links and close them together using mysql_close(); with no arguments. This will close all open connections rather than the only one specified.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.