NLT Posted April 4, 2012 Share Posted April 4, 2012 OK, so if I've got this in my one of my class files: public function userr($user) { $thisquery = mysql_query("SELECT * FROM users WHERE username='". $user ."'"); if(mysql_num_rows($thisquery) == 1) { return true; } return false; } Could I use something like this: if($class->userr($user)) { // Something } If not, how would I go about doing something like it? And is there any way I could use a mysql_fetch_assoc in a function? Quote Link to comment Share on other sites More sharing options...
Maq Posted April 4, 2012 Share Posted April 4, 2012 Yes, that's how you would use that function and yes you can use mysql_fetch_assoc in a function. Quote Link to comment Share on other sites More sharing options...
NLT Posted April 4, 2012 Author Share Posted April 4, 2012 Yes, that's how you would use that function and yes you can use mysql_fetch_assoc in a function. OK, thanks, but how would I get around that? And in the function I'm using at the minute, I've got this: http://pastebin.com/JXFzjqfa But if I try using this in a file: if($users->testOut($_POST['username'])) { // Echo something if successful } else { // Echo something if failed } But it will always show something as if it has failed and an error: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in class.php on line 32 Line 32 being: if(mysql_num_rows($query) == 1) Would that mean that nothing is being found? Although, I'm not sure why because it's echoing the right username (When I do echo) and my user does exist. Quote Link to comment Share on other sites More sharing options...
samshel Posted April 4, 2012 Share Posted April 4, 2012 change $thisquery = mysql_query("SELECT * FROM users WHERE username='". $user ."'"); to $thisquery = mysql_query("SELECT * FROM users WHERE username='". $user ."'") or die(mysql_error()); It will show what exactly is wrong... May be the table name is wrong, DB is incorrect or the connection is not working. Quote Link to comment Share on other sites More sharing options...
NLT Posted April 4, 2012 Author Share Posted April 4, 2012 change $thisquery = mysql_query("SELECT * FROM users WHERE username='". $user ."'"); to $thisquery = mysql_query("SELECT * FROM users WHERE username='". $user ."'") or die(mysql_error()); It will show what exactly is wrong... May be the table name is wrong, DB is incorrect or the connection is not working. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Test'' at line 1 Do you see any error in the query? o_o Quote Link to comment Share on other sites More sharing options...
samshel Posted April 4, 2012 Share Posted April 4, 2012 Well.... There are 2 single quotes, so i am assuming your $user already has single quotes. you want to find a user with username Test or 'Test' [single quotes are part of username]. If first case, then try entering the username without quotes wherever you are entering it. Secondly you need to sanitize your inputs to any query using mysql_real_escape_string $thisquery = mysql_query("SELECT * FROM users WHERE username='".mysql_real_escape_string ($user)."'") or die(mysql_error()); Also when in doubt....echo the query Quote Link to comment Share on other sites More sharing options...
NLT Posted April 4, 2012 Author Share Posted April 4, 2012 Well.... There are 2 single quotes, so i am assuming your $user already has single quotes. you want to find a user with username Test or 'Test' [single quotes are part of username]. If first case, then try entering the username without quotes wherever you are entering it. Secondly you need to sanitize your inputs to any query using mysql_real_escape_string $thisquery = mysql_query("SELECT * FROM users WHERE username='".mysql_real_escape_string ($user)."'") or die(mysql_error()); Also when in doubt....echo the query My $user is simply: $user= mysql_real_escape_string($_POST['username']); If I did an echo for $user, it would simply bring up whatever I had input in the form. I feel an idiot. I didn't have an = for some reason. o_o. Anyhow, one last thing, how would I use a mysql_fetch_assoc in a function for a table? Quote Link to comment Share on other sites More sharing options...
samshel Posted April 4, 2012 Share Posted April 4, 2012 the same way as you would use any mysql_* function. public function userr($user){ $thisquery = mysql_query("SELECT * FROM users WHERE username='". $user ."'"); if(mysql_num_rows($thisquery) == 1){ $row = mysql_fetch_assoc($thisquery); echo $row['user_id']; // will echo user_id field from row, you can use any other field you want. return true; } return false; } Quote Link to comment Share on other sites More sharing options...
NLT Posted April 5, 2012 Author Share Posted April 5, 2012 the same way as you would use any mysql_* function. public function userr($user){ $thisquery = mysql_query("SELECT * FROM users WHERE username='". $user ."'"); if(mysql_num_rows($thisquery) == 1){ $row = mysql_fetch_assoc($thisquery); echo $row['user_id']; // will echo user_id field from row, you can use any other field you want. return true; } return false; } I think I worded it bad. I mean, could I use something like $class->getData(['name']); Quote Link to comment Share on other sites More sharing options...
samshel Posted April 5, 2012 Share Posted April 5, 2012 public function getData($user, $strField){ $thisquery = mysql_query("SELECT * FROM users WHERE username='". $user ."'"); if(mysql_num_rows($thisquery) == 1){ $row = mysql_fetch_assoc($thisquery); if(isset($row[$strField])) { return $row[$strField]; } else { return false; } } else { return false; } } $class->getData($user, 'name'); Quote Link to comment Share on other sites More sharing options...
NLT Posted April 5, 2012 Author Share Posted April 5, 2012 public function getData($user, $strField){ $thisquery = mysql_query("SELECT * FROM users WHERE username='". $user ."'"); if(mysql_num_rows($thisquery) == 1){ $row = mysql_fetch_assoc($thisquery); if(isset($row[$strField])) { return $row[$strField]; } else { return false; } } else { return false; } } $class->getData($user, 'name'); Thank you. Quote Link to comment 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.