1

I have this function that will check if a user already exists in the DB or not:

function checkTwitterAccount($user_id) {
$accountExists = false;
$a = "SELECT * FROM twitterAccounts WHERE user_id='".$user_id."'";
$ar=mysql_query($a) or die("Error selecting twitter account: ".mysql_error());
$ac = mysql_num_rows($ar); 
if ($ac > 0) {
$accountExists = true;
}
return $accountExists;
}

Now when I call this function how do I actually know if the user is or is not in the DB?

I mean how do I call this function and how do I check the result?

Is the below right?

If (checkTwitterAccount($user_id) = true) {
DO THIS
}else{
DO THAT
}

Please help me I am new to it.

Thanks

1
  • 1
    Side note : No need for selecting * in your function, you don't use any of the fields. Also, it makes sense to escape user_id (using, for example, mysql_real_escape_string or PDO). Commented Dec 26, 2011 at 16:01

6 Answers 6

1
if (checkTwitterAccount($user_id)) { //true
//do something
} else { //false
//do something
}
Sign up to request clarification or add additional context in comments.

Comments

1
if (checkTwitterAccount($user_id) == true) {
    //do this
}

else {
    //do that
}

You have to use == rather than = as the = operand sets the value to true in the code you wrote, whereas the == operand compares the returned value to true.

Comments

1

Since your returning a true or false value you can simply use:

If (checkTwitterAccount($user_id)) {
  //DO THIS
}else{
  //DO THAT
}

Note: that your original line:

If (checkTwitterAccount($user_id) = true) {

would result in an assignment error because a single "=" means assign a value which can't be done to a function. You wanted:

If (checkTwitterAccount($user_id) == true) {

because "==" compares a value. Further, == only compares the value so for example 0 is the compares positively with false, any number compares positively with true, if you want to also compare type you us "===" like this:

0 == false //true
0 === false //false
false == false //true
false === false //true
1 == true //true
1 === true //false
true == true //true
true === true //true

Comments

1
function checkTwitterAccount($user_id) {
    $user_id = intval($user_id);
    $a = "SELECT `user_id` FROM `twitterAccounts` WHERE `user_id` = '".mysql_real_escape_string($user_id)."'";
    $ar = mysql_query($a) or die("Error selecting twitter account: ".mysql_error());
    $ac = mysql_num_rows($ar); 
    return ($ac > 0);
}

if(checkTwitterAccount($someid)) {
    // Exists...
} else {
    // No such ID in the DB
}

Note that comparison operator is == not = (which is assign). So you could do:

if(checkTwitterAccount($someid) == true) {

However, it isn't necessary here.

Also remember to sanitize the data in the query.

2 Comments

SELECT * is not needed and thus inefficient.
@tuergeist - That's right. But the point here was to show how the condition should look. Anyways, I've edited the answer.
0
if (checkTwitterAccount($user_id) == true){
 do something if its true
} else {
 do something if its flase
}

should work.. given that you provide argument to that function which seems to be the int.. or id number from id column from users table in the db.

Basically you have a HTML Form that takes in a username and checks the database for that users id number in users table in the database. Once it has this number it will pass it on to the function checkTwitterAccount($user_id) if that function returns True that means guessing by the name of the function that the user has a twitter account else he does not have one.

you could do:

if (checkTwitterAccount($user_id) == true){
 echo "This user has a twitter account";
} else {
 echo "This user does not have a twitter account";
}

Comments

0

You can shorten the orig. function.

function checkTwitterAccount($user_id) {
    $a = "SELECT * FROM twitterAccounts WHERE user_id='" . $user_id . "'";
    $ar = mysql_query($a) or die("Error selecting twitter account: " . mysql_error());
    return mysql_num_rows($ar) > 0; // boolean (will be true or false)
}

Then use the answer from max_. (See comparison operators)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.