Jump to content

Functions


NLT

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;
}

 

 

Link to comment
Share on other sites

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']);

Link to comment
Share on other sites

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');

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.