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
if (checkTwitterAccount($user_id)) { //true
//do something
} else { //false
//do something
}
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.
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
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.
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";
}
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)
Related
So I made page with unban request and in users table I save if user already sent request or not so I don't have multiple unban requests from one user.Now when I check if user sent request it's not working.In database it stands 0 and it's still showing me error pop out.
Here is code, thanks for help in advance
if(isset($_POST['btn-unban_req']))
{
if($unban_sent = 0)//THIS IS WHERE I CHECK
{
//MY THIGNS HERE
if($connection ->query($unbanquery) === TRUE)
{
//MY THIGNS HERE
if ($connection->query($sentquery) === TRUE)
{
}
else
{
echo $connection->error;
}
}
else
{
}
}
else // AND I GET THIS ERROR EVEN IF IT STANDS 0 IN DATABASE
{
echo "Unban already sent!";
}
}
You are not comparing the values, this is assigning the values:
if($unban_sent = 0) // assigning values
This should be:
if($unban_sent == 0) // comparing values
Basic Example:
Lets say,
1 = 1 its assigning
1 == 1 its checking the condition will return TRUE.
For more help: PHP Comparison Operators
You're using this code which is wrong.
$unban_sent = 0
$unban_sent = 0 means to assign 0 to $unban_sent
It should be:
$unban_sent == 0
$unban_sent == 0 means $unban_sent is equal to 0
== is for comparison, = is for assignment, and === is for identical or same type.
More information at http://php.net/manual/en/language.operators.comparison.php.
In line 3 you missed a = for comparision. Instead, you set $unban_set to 0
Try it with this code:
if(isset($_POST['btn-unban_req']))
{
if($unban_sent == 0) //<- Now you are checking it here
{
//MY THIGNS HERE
if($connection ->query($unbanquery) === TRUE)
{
//MY THIGNS HERE
if ($connection->query($sentquery) === TRUE)
{
}
else
{
echo $connection->error;
}
}
else
{
}
}
else // AND I GET THIS ERROR EVEN IF IT STANDS 0 IN DATABASE
{
echo "Unban already sent!";
}
}
I have a variable that can be int or bool, this is because the db from where im querying it change the variable type at some point from bool to int, where now 1 is true and 0 is false.
Since php is "delicate" with the '===' i like to ask if this is the correct why to know if that var is true:
if($wallet->locked === 1 || $wallet->locked === true)
I think in this way im asking for: is the type is int and one? or is the var type bool and true?
How will you approach this problem?
Your code is the correct way.
It indeed checks if the type is integer and the value is 1, or the type is boolean and the value is true.
The expression ($x === 1 || $x === true) will be false in every other case.
If you know your variable is an integer or boolean already, and you're okay with all integers other than 0 evaluating to true, then you can just use:
if($wallet->locked) {
Which will be true whenever the above expression is, but also for values like -1, 2, 1000 or any other non-zero integer.
$wallet->locked = 1;
if($wallet->locked === true){
echo 'true';
}else{
echo 'false';
}
will produce:
false
and
$wallet->locked = 1;
if($wallet->locked == true){
echo 'true';
}else{
echo 'false';
}
will produce:
true
Let me know if that helps!
Your solution seems to be perfect, but You can also use gettype. After that You can check the return value with "integer" or "boolean". Depending on the result You can process the data the way You need it.
solution #1. If $wallet has the value of either false or 0, then PHP will not bother to check its type (because && operator is short-circuit in PHP):
$wallet = true;
//$wallet = 1;
if( $wallet && (gettype($wallet) == "integer" || gettype($wallet) == "boolean") )
{ echo "This value is either 'true and 1' OR it is '1 and an integer'"; }
else { echo "This value is not true"; }
solution #2 (depending on what You want to achieve):
$wallet = 0;
//$wallet = 1; // $wallet = 25;
//$wallet = true;
//$wallet = false;
if($wallet)
{ echo "This value is true"; }
else { echo "This value is not true"; }
I wrote this:
$a[] = "guy";
$b[] = "g";
function login($a1, $b1)
{
if( user($a1) == true and pass1($b1) == true)
{
login2($a1, $b1);
}
else
{
echo "error!!!!";
}
}
function login2($a1, $b1)
{
if (array_search($_REQUEST["user"],$a1) == array_search($_REQUEST["pass"],$b1))
{
echo "you are logged in";
}
else
{
echo "erorr";
}
}
function user($user1)
{
if(in_array($_REQUEST["user"],$user1))
{
echo "gooooooood?";
}
}
function pass1($pas)
{
if(in_array($_REQUEST["pass"],$pas))
{
echo "goooooooood!!!!!!!!";
}
else
{
echo "bad";
}
}
login($a, $b);
and I know that pass() and user() are true because I changed their positions on the function login() and every time I did this the first argument was returned as true and it didn't check the second one. Does anyone know why this happens?
user and pass1 functions should return true or false, not echo out.
Your user and pass1 functions are not returning an explicit value, so they are implicitly returning the NULL value. As described on this page in the PHP manual the NULL type is converted to false when a boolean is expected. So both your user and pass1 functions return false every time.
The && and and logical operators in PHP use short-circuiting for efficiency (see the first code example on this page of the PHP manual) so in any and statement whose first operand evaluates to false it can never be possible for the whole and statement to evaluate to true, so the second operand (in the case of your code above, the second operand is the call to pass1($b1)) is never evaluated because it would be a waste of time to do so.
Which means you're seeing the user function being called, but never the pass1 function.
Try using this instead:
$a[] = "guy";
$b[] = "g";
function login($a1, $b1)
{
if( user($a1) == true && pass1($b1) == true)
login2($a1, $b1);
else
echo "error!!!!";
}
function login2($a1, $b1)
{
if (array_search($_REQUEST["user"],$a1) == array_search($_REQUEST["pass"],$b1))
echo "you are logged in";
else
echo "erorr";
}
function user($user1)
{
if(in_array($_REQUEST["user"],$user1))
echo "gooooooood?";
}
function pass1($pas)
{
if(in_array($_REQUEST["pass"],$pas))
echo"goooooooood!!!!!!!!";
else
echo "bad";
}
login($a, $b);
I am trying to verify a Minecraft username is paid or not.
By typing in the username at the end of the URL, it returns true or false.
$input = 'Notch';
function checkPlayer($player) {
$mcURL = 'http://www.minecraft.net/haspaid.jsp?user=';
$auth = file_get_contents($mcURL . $player);
if ($auth === true) {
echo $player. ' is valid';
} else {
echo $player. ' is not valid';
}
}
checkPlayer($input);
But it doesn't return true. By going to the page http://www.minecraft.net/haspaid.jsp?user=Notch, it does return true. How do I properly check? I think file_get_contents is the wrong function to use for this matter. I'm not sure though.
change this line :
if ($auth === true) {
with
if (trim($auth) == "true") {
ok forgive my technique in writing here, but i can't seem to understand why this code recognizes things and then doesn't recognize some other things.
my code:
if (!isset($id)) {
$_SESSION["logged_in"] = False;
printf("Could not retrieve records: %s\n", mysqli_error($mysqli));
if ($_SESSION["logged_in"] = False)
{ echo "session variable is set at False"; }
if ( $_SESSION["logged_in"] = True)
{ echo "session variable is set at True"; }
}
now as I see it, if it enters this bit of code at all, the first thing that happens should be that the variable gets set to "false". elsewhere in some code not shown it gets set to true and that part works fine but when i try to force it into this for a false setting it remains true.. can anyone see why this wouldn't get set to "False" at this point in the execution?
You should be using == for comparison here, not = for assignment:
if ($_SESSION["logged_in"] = False)
// ---------------^^^
// Should be
if ($_SESSION["logged_in"] == False)
// Also here:
if ( $_SESSION["logged_in"] = True)
//--------------------------^^
// Should be
if ($_SESSION["logged_in"] == True)
You are making a classic mistake by assigning the variable in your if statement instead of comparing it. So, change your if statements to:
if ($_SESSION["logged_in"] == false)
Instead of (where you are assigning):
if ($_SESSION["logged_in"] = false)
By the way, you're statement has now some duplication since the variable itself already is true of false. So, there is no need to check it against the boolean. So, this can be enough:
if ($_SESSION["logged_in"]) //equals true if user is logged in
if (!$_SESSION["logged_in"]) // equals true if user is NOT logged in
You need == or ===
Using = means its equal :)
if (!isset($id)) {
$_SESSION["logged_in"] = False;
printf("Could not retrieve records: %s\n", mysqli_error($mysqli));
if ($_SESSION["logged_in"] == False)
{ echo "session variable is set at False"; }
if ( $_SESSION["logged_in"] == True)
{ echo "session variable is set at True"; }
}
You should be using the == operator rather than = in your if statements.
EG
if ($_SESSION["logged_in"] == False)