H8W25 Posted September 14, 2007 Share Posted September 14, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/ Share on other sites More sharing options...
darkfreaks Posted September 14, 2007 Share Posted September 14, 2007 <?php if ( mysql_num_rows( $query ) == "0") { return true;} else { return false; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348691 Share on other sites More sharing options...
syntaxerror Posted September 14, 2007 Share Posted September 14, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348697 Share on other sites More sharing options...
darkfreaks Posted September 14, 2007 Share Posted September 14, 2007 its the mysql_real_escape_string i betcha remove it from the sql and put it in a variable like $field= mysql_real_escape_string($field); Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348698 Share on other sites More sharing options...
TheFilmGod Posted September 14, 2007 Share Posted September 14, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348702 Share on other sites More sharing options...
H8W25 Posted September 14, 2007 Author Share Posted September 14, 2007 K thanks guys, i'll try it out Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348708 Share on other sites More sharing options...
H8W25 Posted September 14, 2007 Author Share Posted September 14, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348716 Share on other sites More sharing options...
BlueSkyIS Posted September 14, 2007 Share Posted September 14, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348721 Share on other sites More sharing options...
darkfreaks Posted September 14, 2007 Share Posted September 14, 2007 replace alll ` with ' Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348722 Share on other sites More sharing options...
H8W25 Posted September 14, 2007 Author Share Posted September 14, 2007 I replaced all ` with ' and I'm still getting Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348727 Share on other sites More sharing options...
H8W25 Posted September 14, 2007 Author Share Posted September 14, 2007 how do u select a database? Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348728 Share on other sites More sharing options...
darkfreaks Posted September 14, 2007 Share Posted September 14, 2007 ("SELECT 'Username' FROM 'users' WHERE 'ID' = $ID"); Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348734 Share on other sites More sharing options...
BlueSkyIS Posted September 14, 2007 Share Posted September 14, 2007 "how do u select a database?" mysql_select_db(); http://us.php.net/mysql_select_db Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348736 Share on other sites More sharing options...
darkfreaks Posted September 14, 2007 Share Posted September 14, 2007 try the statement i have above Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348740 Share on other sites More sharing options...
H8W25 Posted September 15, 2007 Author Share Posted September 15, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348742 Share on other sites More sharing options...
BlueSkyIS Posted September 15, 2007 Share Posted September 15, 2007 Your statement won't do anything if a database hasn't been selected. All you have to do is read the error at the beginning of the thread. Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348743 Share on other sites More sharing options...
darkfreaks Posted September 15, 2007 Share Posted September 15, 2007 sorry my bad he does need to select a db. i didnt know num_rows required a connection my bad. Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348744 Share on other sites More sharing options...
BlueSkyIS Posted September 15, 2007 Share Posted September 15, 2007 ay ay ay. before you use mysql_query(), you have to connect to the database server using mysql_connect() and then select a database using mysql_select_db(). I'm not going to write the code for you. Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348745 Share on other sites More sharing options...
darkfreaks Posted September 15, 2007 Share Posted September 15, 2007 i'm not either the best way to learn how to code is to do it yourself am i not right Bluesky? Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348746 Share on other sites More sharing options...
teng84 Posted September 15, 2007 Share Posted September 15, 2007 $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 Quote Link to comment https://forums.phpfreaks.com/topic/69401-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-348752 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.