Jump to content

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resourc


Recommended Posts

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/cooking/public_html/login/functions.php on line 78

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/cooking/public_html/login/functions.php on line 78

No database selected

 

Now the area of code thats troublesome...

 

function checkUnique($field, $compared)
{
	$query = mysql_query ( "SELECT `" . mysql_real_escape_string ( $field ) . "` FROM `users` WHERE `" . mysql_real_escape_string ( $field ) . "` = '" . mysql_real_escape_string ( $compared ) . "'" );
	if ( mysql_num_rows ( $query ) == 0 )
	{
		return TRUE;
	}
	else {
		return FALSE;
	}
}

 

 

Can anyone help me debug this? I'm not very experienced with php thus I cannot tell whats wrong. Thanks

try to echo your $query to the browser, and execute it in MySQL

 

or easier if you have the query browser, execute it there

 

i believe i got this previous error due to some syntactical error on my sql query

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/cooking/public_html/login/functions.php on line 78

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/cooking/public_html/login/functions.php on line 78

No database selected

 

Now the area of code thats troublesome...

 

function checkUnique($field, $compared)
{
	$query = mysql_query ( "SELECT `" . mysql_real_escape_string ( $field ) . "` FROM `users` WHERE `" . mysql_real_escape_string ( $field ) . "` = '" . mysql_real_escape_string ( $compared ) . "'" );
	if ( mysql_num_rows ( $query ) == 0 )
	{
		return TRUE;
	}
	else {
		return FALSE;
	}
}

 

 

Can anyone help me debug this? I'm not very experienced with php thus I cannot tell whats wrong. Thanks

 

Your answer is right there.

Ok, I'm lost.

 

Heres the code for the whole page

<?php
// ------------------------------------------------------------------------

/**
 * Check login
 *
 * Applyes restrictions to visitors based on membership and level access
 *
 * @access	public
 * @param	string
 * @return	bool
 */


function checkLogin ( $levels )
{
	if(!$_SESSION['logged_in'])
	{
		$access = FALSE;
	}
	else {
		$kt = split(' ', $levels);

		$query = mysql_query('SELECT Level_access FROM users WHERE ID = "'.mysql_real_escape_string($_SESSION['user_id']).'"');
		$row = mysql_fetch_assoc($query);

		$access = FALSE;

		while(list($key,$val)=each($kt))
		{
			if($val==$row['Level_access'])
			{//if the user level matches one of the allowed levels
				$access = TRUE;
			}
		}
	}
	if($access==FALSE)
	{
		header("Location: login.php");
	}

}

// ------------------------------------------------------------------------

/**
 * Validate if email
 *
 * Determines if the passed param is a valid email
 *
 * @access	public
 * @param	string
 * @return	bool
 */

function valid_email($str)
{
	return ( ! preg_match ( "/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str ) ) ? FALSE : TRUE;
}

// ------------------------------------------------------------------------

/**
 * Check unique
 *
 * Performs a check to determine if one parameter is unique in the database
 *
 * @access	public
 * @param	string
 * @param	string
 * @return	bool
 */


function checkUnique($field, $compared)
{
	$query = mysql_query ( "SELECT ` . mysql_real_escape_string ( $field ) . ` FROM `users` WHERE ` . mysql_real_escape_string ( $field ) . " = '" . mysql_real_escape_string ( $compared ) . "'" );
	if ( mysql_num_rows($query) == "0" )
	{
		return TRUE;
	}
	else {
		return FALSE;
	}
}

// ------------------------------------------------------------------------

/**
 * Validate if numeric
 *
 * Validates string against numeric characters
 *
 * @access	public
 * @param	string
 * @return	bool
 */


function numeric($str)
{
	return ( ! ereg("^[0-9\.]+$", $str)) ? FALSE : TRUE;
}

// ------------------------------------------------------------------------

/**
 * Validate if alfa numeric
 *
 * Validates string against alpha numeric characters
 *
 * @access	public
 * @param	string
 * @return	bool
 */

function alpha_numeric($str)
{
	return ( ! preg_match("/^([-a-z0-9])+$/i", $str)) ? FALSE : TRUE;
}

// ------------------------------------------------------------------------

/**
 * Create a Random String
 *
 * Useful for generating passwords or hashes.
 *
 * @access	public
 * @param	string 	type of random string.  Options: alunum, numeric, nozero, unique
 * @param	none
 * @return	string
 */


function random_string($type = 'alnum', $len = 
{					
	switch($type)
	{
		case 'alnum'	:
		case 'numeric'	:
		case 'nozero'	:

				switch ($type)
				{
					case 'alnum'	:	$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
						break;
					case 'numeric'	:	$pool = '0123456789';
						break;
					case 'nozero'	:	$pool = '123456789';
						break;
				}

				$str = '';
				for ($i=0; $i < $len; $i++)
				{
					$str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
				}
				return $str;
		  break;
		case 'unique' : return md5(uniqid(mt_rand()));
		  break;
	}
}

// ------------------------------------------------------------------------

/**
 * Get username - Returns the username of the logged in member based on session ID
 *
 * @access	public
 * @param	id
 * @return	string
 */


function get_username ( $id )
{
	$query = mysql_query("SELECT `Username` FROM `users` WHERE `ID` = '" . mysql_real_escape_string ( $id ) . "'");

	if ( mysql_num_rows ( $query ) == 1 )
	{
		$row = mysql_fetch_array ( $query );

		return $row['Username'];
	}
	else {
		return FALSE;
	}
}

// ------------------------------------------------------------------------

/**
 * Is admin - Determines if the logged in member is an admin
 *
 * @access	public
 * @param	id
 * @return	bool
 */


function isadmin ( $id )
{
	$query = mysql_query("SELECT `Level_access` FROM `users` WHERE `ID` = '" . mysql_real_escape_string ( $id ) . "'");

	if ( mysql_num_rows ( $query ) == 1 )
	{
		$row = mysql_fetch_array ( $query );

		if ( $row['Level_access'] == 1 )
		{
			return TRUE;
		}
		else {
			return FALSE;
		}
	}
	else {
		return FALSE;
	}
}
?>

 

Now it spits out

Parse error: syntax error, unexpected '`' in /home/cooking/public_html/login/functions.php on line 77

what's on line 77? Perhaps a SQL statement with a bad ` character in it?

 

By the way, the correct answer was already posted:

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/cooking/public_html/login/functions.php on line 78

No database selected <--------------------------- Your answer is right there.

this is what it looks like:

// ------------------------------------------------------------------------

/**
* Check unique
*
* Performs a check to determine if one parameter is unique in the database
*
* @access public
* @param string
* @param string
* @return bool
*/


function checkUnique($field, $compared)
{
$query = mysql_query ( "SELECT '" . mysql_real_escape_string ( $field ) . "' FROM 'users' WHERE '" . mysql_real_escape_string ( $field ) . "' = '" . mysql_real_escape_string ( $compared ) . "'" );
if ( mysql_num_rows ( $query ) == "0" )
{
return TRUE;
}
else {
return FALSE;
}
}

// ------------------------------------------------------------------------

if i were to make that select a database how would i do it?

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

//$con  connection you might have a problem when dealing inside a function
function checkUnique($field, $compared,$con )
{
mysql_select_db("my_db", $con); 

$query = mysql_query ( "SELECT " . mysql_real_escape_string ( $field ) . " FROM users WHERE " . mysql_real_escape_string ( $field ) . " = '" . mysql_real_escape_string ( $compared ) . "'" ) or die(mysql_error());
if ( mysql_num_rows ( $query ) == "0" )
{
return TRUE;
}
else {
return FALSE;
}
}

 

try something like that and tell us the error

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.