Hi everyone, i am a newbie to PHP and i am struggling with the following error. i am trying to follow this linkl tutorial but its not working.

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /Applications/MAMP/htdocs/functions.php on line 73

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /Applications/MAMP/htdocs/functions.php on line 73
No database selected

This is my functions.php file.

<?php
	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");
		}
		else {
		//do nothing: continue
		}
		
	}
function valid_email($eaddr) {
	if (!ereg("([[:alnum:]\.\-]+)(\@[[:alnum:]\.\-]+\.+)", $eaddr)) {
		return false;
	}
	return true;
}

function random_string($type = 'alnum', $len = 8)
{					
	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;
	}
}

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

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

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

Thanks for the help in advance

Recommended Answers

All 5 Replies

The reason you are getting the error is because mysql sent back an error instead of a result. Most likely there is something in your SQL statement that has caused a problem.

What the best thing to do is to check first if the result of the query is actually valid. If not then you can output the error so that you can find the problem here's some code on how to do it.

$qry = "This is a SQL query that won't work";

$result = mysql_query($qry); // this will produce an SQL error and not a proper result

if(!$result){
     die("SQL ERROR " . myql_error()); // if the result is not a proper result then we output the error
}

// continue on if there is not a problem

You can use the error printed out to find where your SQL query problem is.

It might be a good idea to test your query before you put it in your application. Use phpMyAdmin for testing.

The problem is quite clear in the error that was generated:

No database selected

Matti Ressler
Suomedia

try to rewrite your function checkUnique like this:

function checkUnique($table, $field, $compared)
{
	$tablenew=mysql_real_escape_string($table);
        $fieldnew=mysql_real_escape_string($field);
        $comparednew=mysql_real_escape_string($compared);
	
	$query="SELECT $fieldnew
		      FROM $tablenew
		      WHERE $fieldnew='$comparednew'",
	
	$result=mysql_query($query);
	$num=mysql_num_rows($result);
	
	if($num==0)
	{
		return TRUE;
	}
	else {
		return FALSE;
	}
}

The problem is quite clear in the error that was generated:

No database selected

Matti Ressler
Suomedia

Do you have mysql_select_db in your script ?

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /Applications/MAMP/htdocs/functions.php on line 73
No database selected

did you connect to your mysql server and select your database?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.