/    Sign up×
Community /Pin to ProfileBookmark

How To Check SQL COUNT Equalts To 1 ?

Php Folks,

Doing an Sql query, I am trying to check if a certain email exists or not. Trying to check with the SQL COUNT.
It’s like this …
You see a form asking you for your email. You input your email.
I want php to see if the email exists in the db or not. Check using the COUNT.
If it does exist in mysql db, it means you are already registered. And so here, the the login() function should trigger for you to login.
Else the register() function should trigger so the user’s inputted email gets registered (dumped into the db).
No, the form submit button won’t say “login” or “register” but “submit”.
I don’t want to create 2 different pages or 2 different forms. One for “login” and one for ‘register”.
To cut code short, i want the same form to be able to do both. Login and Register.

So, login() function looks something like this …

[code]
$sql_query = “SELECT id,username,first_name,surname,FROM users WHERE personal_email = ?”;
[/code]

And register() function looks something like this ..

[code]
$sql_query = “INSERT INTO users (personal_email) VALUES (?)”;
[/code]

Understand ? Simple. Not rocket science.
Now, at the very beginning, when the user inputs his email, I must get php to check first whether the inputted email exists or not. And then trigger the appropriate function (login/register) based on the result found.
Now, how to check if the email exists in the db or not using the COUNT ?

CODE CONTEXT …

[code]
$sql_query = “SELECT COUNT(personal_email) FROM users WHERE personal_email = ?”;
$stmt = mysqli_prepare($conn,$sql_query);
if($stmt == false)
{
// just for debugging for now:
die(“<pre>Prepare failed:n”.mysqli_error($conn).”n$sql_query</pre>”);
}
else
{
//Bind Variables to the Prepared Statement as parameters.
mysqli_stmt_bind_param($stmt,’s’,$email);
//Attempt to execute the Prepared Statement.
if(!mysqli_stmt_execute($stmt))
{
//Close Connection.
mysqli_close($conn);

die(“Could not mysqli_stmt_execute! Please try again later!”);
}

//mysqli_stmt_bind_result($stmt,$email);
$result = mysqli_stmt_get_result($stmt);

if($sql_query == 1) //This means the COUNT = 1 as email found in db.
{
//$_SESSION[‘session_type’] = ‘account’;
$_SESSION[‘session_type’] = ‘login’;
login();

}
else //This means the COUNT = 0 as email NOT found in db.
{
$_SESSION[‘session_type’] = ‘register’;
register();
}
//Close Statement.
//mysqli_stmt_close($stmt);

//Close Connection.
//mysqli_close($conn);
}
[/code]

Q1a. Which of the following is correct ?
Q1b. Which of the following correct one would you use over the other(s) and why choose to use that particular one over the other correct one(s) ?
Q1c. Which one is incorrect and why is it incorrect ?

[code]
$sql_query = “SELECT COUNT(personal_email) FROM users WHERE personal_email = ?”;
$stmt = mysqli_prepare($conn,$sql_query);

/*
WHICH OF THE FOLLOWING IFS ARE CORRECT ? OUT OF THE CORRECT ONES, GIVE YOUR RANKINGS. I HAVE RANKED MY ONES BELOW.
*/
if(!$stmt) //Rank 1
if($stmt == false) //Rank 2
if($stmt !== true) //Rank 3.
if(!$stmt == true) //Rank 4.
if($stmt != true) //Rank 5. I think this is incorrect. Let me know if I am write or wrong.
if(!$stmt = true) //Rank 6. I think this is incorrect. Let me know if I am write or wrong.
if($stmt == 0) //Rank 7. I think this is incorrect. Let me know if I am write or wrong.
if($stmt < 1) //Rank 8. I think this is incorrect. Let me know if I am write or wrong.
if($stmt !> 0) //Rank 9. I think this is incorrect. Let me know if I am write or wrong.
if(!$stmt > 0) //Rank 10. I think this is incorrect. Let me know if I am write or wrong.
[/code]

Q2a. Which of the following is correct ?
Q2b. Which of the following correct one would you use over the other(s) and why choose to use that particular one over the other correct one(s) ?
Q2c. Which one is incorrect and why is it incorrect ?

[code]
/*
WHICH OF THE FOLLOWING IFS ARE CORRECT ? OUT OF THE CORRECT ONES, GIVE YOUR RANKINGS. I HAVE RANKED MY ONES BELOW.
*/
//Bind Variables to the Prepared Statement as parameters.
mysqli_stmt_bind_param($stmt,’s’,$email);
//WHICH OF THE FOLLOWING IFS ARE CORRECT ? OUT OF THE CORRECT ONES, GIVE YOUR RANKINGS. I HAVE RANKED MY ONES BELOW.
if(!mysqli_stmt_execute($stmt)) //Rank 1
if(mysqli_stmt_execute($stmt) == false) //Rank 2
if(mysqli_stmt_execute($stmt) !== true) //Rank 3
if(!mysqli_stmt_execute($stmt) == true) //Rank 4
if(mysqli_stmt_execute($stmt) != true) //Rank 5. I think this is incorrect. Let me know if I am write or wrong.
if(!mysqli_stmt_execute($stmt) = true) //Rank 6. I think this is incorrect. Let me know if I am write or wrong.
{
//Close Connection.
mysqli_close($conn);

die(“Could not mysqli_stmt_execute! Please try again later!”);
}
[/code]

Q3a. Which of the following is correct ?
Q3b. Which of the following correct one would you use over the other(s) and why choose to use that particular one over the other correct one(s) ?
Q3c. Which one is incorrect and why is it incorrect ?

[code]
//$result = mysqli_stmt_bind_result($stmt,$email);
$result = mysqli_stmt_get_result($stmt);

/*
WHICH OF THE FOLLOWING IFS ARE CORRECT ? OUT OF THE CORRECT ONES, GIVE YOUR RANKINGS. I HAVE RANKED MY ONES BELOW.
*/
if($sql_query = 1)
if(!$sql_query < 1)
if($sql_query > 0)
if($sql_query == 1)
if($sql_query !< 1)
/*
Remember, the $sql_query is doing a COUNT for the found email. If email found in db then COUNT will be 1, else COUNT would be 0.
Eg. $sql_query = “SELECT COUNT(personal_email) FROM users WHERE personal_email = ?”;
*/
{

//$_SESSION[‘session_type’] = ‘account’;
$_SESSION[‘session_type’] = ‘login’;
login();
}
else
{
$_SESSION[‘session_type’] = ‘register’;
register();
}
[/code]

Q4a. Which of the following is correct ?
Q4b. Which of the following correct one would you use over the other(s) and why choose to use that particular one over the other correct one(s) ?
Q4c. Which one is incorrect and why is it incorrect ?

[code]
//$result = mysqli_stmt_bind_result($stmt,$email);
$result = mysqli_stmt_get_result($stmt);

/*
WHICH OF THE FOLLOWING IFS ARE CORRECT ? OUT OF THE CORRECT ONES, GIVE YOUR RANKINGS. I HAVE RANKED MY ONES BELOW.
*/
if(mysqli_num_rows($result) = 1)
if(!mysqli_num_rows($result) < 1)
if(mysqli_num_rows($result) > 0)
if(mysqli_num_rows($result) == 1)
if(mysqli_num_rows($result) !< 1)
/*
Remember, the $sql_query is doing a COUNT for the found email. If email found in db then COUNT will be 1, else COUNT would be 0.
Eg. $sql_query = “SELECT COUNT(personal_email) FROM users WHERE personal_email = ?”;
*/
{

//$_SESSION[‘session_type’] = ‘account’;
$_SESSION[‘session_type’] = ‘login’;
login();
}
else
{
$_SESSION[‘session_type’] = ‘register’;
register();
}
[/code]

Q5: Is it true that, to check whether the COUNT is 1 or 0, I should not be checking like any of the following …

[code]
if($sql_query = 1)
[/code]

[code]
if($sql_query == 1)
[/code]

But should be checking like one of the following…

[code]
if(mysqli_num_rows($result) = 1)
[/code]

[code]
if(mysqli_num_rows($result) == 1)
[/code]

[code]
if($result = 1)
[/code]

[code]
if($result == 1)
[/code]

Yes or no ? Answer this.

I am just experimenting. That is all.
Show me how to use the …

[code]
“if($sql_query ..”
[/code]

….. to check if the COUNT is 1 or 0.

Look at Q3 above. That question is most important as I am stuck there. Things go wrong there. When I check for an email that exists in the db, naturally the login() function should trigger but instead the register() function triggers. That is why I know I am doing something wrong here. Concentrate on teaching me the answer to Q4 and I should be able to take it from there.

Q6. To get the email COUNT with the SQL query, which of the following should I use ?

[code]
$result = mysqli_stmt_bind_result($stmt,$email);
[/code]

[code]
$result = mysqli_stmt_get_result($stmt);
[/code]

Thanks!

to post a comment
PHP

44 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 02.2020 — > @developer_web#1616873 if($result = 1)

Way too much stuff to read here, but that caught my eye as (almost) never correct. It's assigning the number 1 to $result, so the if() will _always_ return true regardless of what $result was set to.
Copy linkTweet thisAlerts:
@ginerjmApr 02.2020 — Yes - way WAY too much code for such a simple question. You do a simple query to select any field (it does not need to be "counted") and check how many rows were returned. THAT'S pretty simple. Don't know what the other 100+ lines are for.
Copy linkTweet thisAlerts:
@developer_webauthorApr 02.2020 — @ginerjm#1616879

Don't worry, the method you talking about I know how to do it. It is just I want to now learn how to do it with the COUNT.

Ok, forget the way too much code and just concentrate on my questions. Especially Q3c, Q4c and Q5.
Copy linkTweet thisAlerts:
@developer_webauthorApr 02.2020 — @NogDog#1616878

What do you mean ? You mean, no matter what the value of $result is, whether '1" or '0", it would always be true ? So, if($result) means "if $result holds any value atall, then do this ..." ?

So, how would you go about checking if the COUNT was 1 or 0 then ?
Copy linkTweet thisAlerts:
@ginerjmApr 02.2020 — No - he means you wrote the IF wrong.
Copy linkTweet thisAlerts:
@developer_webauthorApr 02.2020 — @ginerjm#1616882

So, how it should be then ?

Look, this is the main query to COUNT the presence of the inputted email ...
<i>
</i>$sql_query = "SELECT COUNT(personal_email) FROM users WHERE personal_email = ?";
$stmt = mysqli_prepare($conn,$sql_query);
//$result = mysqli_stmt_bind_result($stmt,$email);
$result = mysqli_stmt_get_result($stmt);


Now, at this step, which line out of the 2 comes next ?

This one ...
<i>
</i>if($sql_query == 1) //This means the COUNT = 1 as one instance of email is found in db.


Or, this one ....
<i>
</i>if($result == 1) //This means the COUNT = 1 as one instance of email is found in db.


Which one ? Let me know.

Anyway, if we be continuing the code, then it looks like this ....

CODE A
<i>
</i>if($sql_query == 1) //This means the COUNT = 1 as one instance of email is found in db.
{
$_SESSION['session_type'] = 'login';
login();

}
else //This means the COUNT = 0 as email NOT found in db.
{
$_SESSION['session_type'] = 'register';
register();
}
//Close Statement.
//mysqli_stmt_close($stmt);

//Close Connection.
//mysqli_close($conn);
}


Or, it should look like this ...

CODE B
<i>
</i>if($result == 1) //This means the COUNT = 1 as one instance of email is found in db.
{
$_SESSION['session_type'] = 'login';
login();

}
else //This means the COUNT = 0 as email NOT found in db.
{
$_SESSION['session_type'] = 'register';
register();
}
//Close Statement.
//mysqli_stmt_close($stmt);

//Close Connection.
//mysqli_close($conn);
}


You tell me which one is correct. Code A or Code B.

The first batch or the last batch.

Cheers!
Copy linkTweet thisAlerts:
@NogDogApr 02.2020 — > @developer_web#1616881 What do you mean

= is an _assignment_ operator, not a comparison operator. if($something = 1) is not comparing something to 1, it is _assigning_ the value of 1 to something. As a side effect, that means the expression will always evaluate as true in this example and always execute that if block (and if you assigned 0 instead of 1, would always be false).
Copy linkTweet thisAlerts:
@NogDogApr 02.2020 — PS: Neither A nor B will work, since you never actually execute the query. Take a look at Example #2 for the full process: https://www.php.net/manual/en/mysqli-stmt.get-result.php#example-1968
Copy linkTweet thisAlerts:
@ginerjmApr 02.2020 — Something is wrong with a poster who can write this much code and yet not know how to write a comparison......
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — @ginerjm,

Care to respond to my previous post ?

Thanks!
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — @ginerjm#1616890

I know "=" means "equal" and "==" means "equal to".

I know you use the former like these:
<i>
</i>$variable = something;

Assign value with the single equal sign.

And I know, you compare values with the double equal sign like so:
<i>
</i>if($session['account'] == 'loggedin';
{
do this;
}
{
else
{
do that;
}


And so, correct me if i am wrong, when you compare values you use the "==".

But in some places I have seen people using the "=" to compare values. Hence, the confusion. Maybe, they did bad coding ?

Also, on the sql php query, you use the "=" for comparing and not the "==". Hence, more confusion.
<i>
</i>$query = "SELECT Name, Population, Continent FROM Country WHERE Continent=? ORDER BY Name LIMIT 1";
Copy linkTweet thisAlerts:
@NachfolgerApr 05.2020 — @ginerjm#1616890

These are homework questions.
Copy linkTweet thisAlerts:
@ginerjmApr 05.2020 — > @developer_web#1616987 But in some places I have seen people using the "=" to compare values. Hence, the confusion. Maybe, they did bad coding

I'd love to see where you have seen the above statement to be true...

As for what you see and use in a query statement - that is correct. It is NOT PHP!!!
Copy linkTweet thisAlerts:
@NogDogApr 05.2020 — > @developer_web#1616987 I have seen people using the "=" to compare values

If so, either it was an error, or they were not _comparing_ values but instead were interested if checking if the result of an assignment was "truthy" or "falsey". Something like this saves a line or two of code, though that may be a false savings if it makes the code more difficult to maintain?
<i>
</i>if($myArray = json_decode($jsonString, 1)) {
// do stuff with $myArray
}
else { // json_decode() returned false or empty string
throw new Exception("Invalid json: '$jsonString')");
}
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — @NogDog#1616888

So you are saying, if I use the IF to compare some value then I must always use the "==" sign within the if brackets. using the "=" is pointless here. Is that what you are trying to tell me because that is what I am understanding.
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — @Nachfolger#1617001

I am asking from home. From my bedroom. ;)
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — @ginerjm#1617006

I know the query is to mysql. But I see sql query and php querying sql are slightly different.

Hence, I thought ...

<i>
</i>$query = "SELECT Name, Population, Continent FROM Country WHERE Continent=? ORDER BY Name LIMIT 1";


is php code which the php interpreter will convert it in the background to pure sql query (sql language) to query mysql db.

So, you are telling me that this line is not php code querying the mysql db but the line is basic sql language code itself ? Yes or no ?
Copy linkTweet thisAlerts:
@ginerjmApr 05.2020 — Query statements are ALWAYS written using sql syntax. Not php syntax. There are no "php" queries.

And the example given to you by NogDog is always the same. One is assigning the result of a query call to a variable. But - by including it inside an if PHP has a delightfully unusual style of allowing you to save a couple lines of code by treating it as a condition to be evaulated. It replaces:
<i>
</i>$result = $pdo-&gt;query($q);
if (!$result)
{
(handle error)
}
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — @NogDog#1617010

Mmm.

So, this would be wrong then ?

<i>
</i>if($myArray = json_decode($jsonString, 1)) {
// do stuff with $myArray
}
else { // json_decode() returned false or empty string
throw new Exception("Invalid json: '$jsonString')");
}
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — Thanks NogDog & Jinergm,

I hope I remember all this and don't forget it and open another thread to ask a question who's answers are in this thread. if i mistakenly open any then kindly refer me to this thread.

Cheers!
Copy linkTweet thisAlerts:
@ginerjmApr 05.2020 — Why do you think it is wrong?
Copy linkTweet thisAlerts:
@ginerjmApr 05.2020 — I would prefer you make the attempt to learn some of this stuff by doing the appropriate reading and remembering.

But that's the way I am. Don't know about everyone else who does this kind of work.
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — @ginerjm#1617015

Then, both these should work then.
<i>
</i>if($sql_query == 0)


<i>
</i>if(mysqli_num_rows($result) == 1)


Right ?

But which one would you use Ginerjm ?

Remember, the query looks like this:
<i>
</i>$sql_query = "SELECT COUNT(personal_email) FROM users WHERE personal_email = ?";
Copy linkTweet thisAlerts:
@ginerjmApr 05.2020 — $sql_query will be either False or a query result. Treat it that way and stop all this blustering about simple things. You have been on these forums for a couple of years now and it always comes back down to you not learning the stuff one should learn from day one. So boring......

Upon further contemplation, I apologize for my last.
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — @ginerjm#1617022

Don't know what you mean.

Expecting this to be a result:
<i>
</i>$sql_query = "SELECT COUNT(personal_email) FROM users WHERE personal_email = ?";


If the email exists in the mysql db, then the result should not be TRUE/FALSE but the COUNT should be 1.

And if the email doesn't exist in the db, then the result COUNT should be 0.

Now, look at this code:
<i>
</i>if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(!isset($_POST['email_account']) || !isset($_POST['email_service']))
{
$email_error = "&lt;font color='red'&gt;Input Email Address!&lt;/color&gt;";
}
else
{
echo "Line 13 triggered&lt;br&gt;"; //DELETE THIS

<i> </i> //Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
<i> </i> $conn = mysqli_connect("localhost","root","","test");
<i> </i>
<i> </i> if($conn === false)
<i> </i> {
<i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> echo "Line 24 triggered&lt;br&gt;"; //DELETE THIS
<i> </i> //Set Parameters.
<i> </i> $email = trim($_POST["email_account"]) . '@' . trim($_POST["email_service"]);
<i> </i> echo "line 27 triggered: $email&lt;br&gt;";
<i> </i>
<i> </i> $sql_query = "SELECT COUNT(personal_email) FROM users WHERE personal_email = ?";
<i> </i> $stmt = mysqli_prepare($conn,$sql_query);
<i> </i> if($stmt == false)
<i> </i> {
<i> </i> // just for debugging for now:
<i> </i> die("&lt;pre&gt;Prepare failed:n".mysqli_error($conn)."n$sql_query&lt;/pre&gt;");
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> echo "Line 38 triggered&lt;br&gt;"; //DELETE THIS
<i> </i> mysqli_stmt_bind_param($stmt,'s',$email);
<i> </i>
<i> </i> if(!mysqli_stmt_execute($stmt))
<i> </i> {
<i> </i> echo "Line 43 triggered&lt;br&gt;"; //DELETE THIS
<i> </i> //Close Connection.
<i> </i> mysqli_close($conn);
<i> </i>
<i> </i> die("Could not mysqli_stmt_execute! Please try again later!");
<i> </i> }
<i> </i>
<i> </i> //$result = mysqli_stmt_bind_result($stmt,$email);
<i> </i> $result = mysqli_stmt_get_result($stmt);
<i> </i>
<i> </i> echo "Line 53 triggered&lt;br&gt;"; //DELETE THIS
<i> </i> //if($sql_query == 1)
<i> </i> if(mysqli_num_rows($result))
<i> </i> {
<i> </i> echo "Line 57 triggered: Function login() will trigger!&lt;br&gt;"; //DELETE THIS
<i> </i> $_SESSION['session_type'] = 'login';
<i> </i> login();
<i> </i>
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> echo "Line 64 triggered: Function register() will trigger!&lt;br&gt;"; //DELETE THIS
<i> </i> $_SESSION['session_type'] = 'register';
<i> </i> register();
<i> </i> }
<i> </i> }
<i> </i> }
<i> </i>}

In that form, if you input an email that does not exist in the db, then the register() should trigger for your email to be registered. Else, the login() should trigger for you to login to your account.

Now, I ain't bothering you with the codes of both these register() and login() functions.

In that form, I am inputting a non-existing email. That means, the register() should trigger. But instead the login() is triggering as if the non-existant email is already in the db. It is not infact.

So, why is the wrong function getting triggered here ?

These are getting echoed ....
<i>
</i>Line 13 triggered
Line 24 triggered
line 27 triggered: [email protected]
Line 38 triggered
Line 53 triggered
Line 57 triggered: Function login() will trigger!


What is wrong ? I do not see anything wrong in these following lines. What am I missing ?
<i>
</i>$sql_query = "SELECT COUNT(personal_email) FROM users WHERE personal_email = ?";
$stmt = mysqli_prepare($conn,$sql_query);
mysqli_stmt_bind_param($stmt,'s',$email);
mysqli_stmt_execute($stmt)
$result = mysqli_stmt_get_result($stmt);


<i>
</i>if(mysqli_num_rows($result) == 1)
{
$_SESSION['session_type'] = 'login';
login();

}
else
{
$_SESSION['session_type'] = 'register';
register();
}


Since, I input a non-existing email, the $result should hold value = 0.

And so, the register() should trigger. Not login().

Puzzling!
Copy linkTweet thisAlerts:
@ginerjmApr 05.2020 — The first line of your last post performs a query and returns a result, as the MANUAL SAYS IT WILL. That result is stored in $sql_query as we have been trying to tell you.

That result VALUE will be either FALSE if the query call fails OR it will be what is specifically called (in PHP) a "resource", ie, the results of the query.

To view the results of a query you do a fetch (or fetchall in certain cases). Do you now how to do that? (If you don't RTFM.)

As for the rest of whatever you posted - I don't care. I don't even know if we are talking about the topic anymore.
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — @ginerjm#1617025

Mate!

We both overlooked something! I had a typo!

Supposed to be:
<i>
</i>mysqli_stmt_num_rows


Look at my previous post. The code is this ...
<i>
</i>mysqli_num_rows


I forgot the "stmt" part! Lol!

But don't go away! I'm experiencing new trouble. Will mention that in a few mins. Let me test a few more times. Ok ? Great!
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — @ginerjm#1617025

Don't worry! I know how to do the fetching.

I thought the $result would hold the value (1 or 0). You just made it clear that $result will hold no INT but a BOOLEAN. Did I understand right ? If so, thanks for teaching me this as i didn't know!

Good thing I pestered you folks!

Now, let me google and research why the $result would only hold BOOLEAN.

Why these would only hold BOOLEAN ...
<i>
</i>$result = mysqli_stmt_bind_result($stmt,$email);
$result = mysqli_stmt_get_result($stmt);
$result = mysqli_stmt_store_result($stmt);


These would only hold BOOLEAN. Correct ?

One thing, here I thought the mysqli_stmt_get_result($stmt) was doing the fetching.

I know this fetches ...
<i>
</i>mysqli_stmt_fetch($stmt)
Copy linkTweet thisAlerts:
@ginerjmApr 05.2020 — Read my post again. And again. And again until you get it right. It will not be 1 or 0. Jeez!!!

And yes - I did mis-read your first line of the previous. I thought it was doing the query but in fact it was just defining a query. My bad.
Copy linkTweet thisAlerts:
@ginerjmApr 05.2020 — And dont' go starting something new in this topic. I want to get out of this wild goose chase of yours.
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — @ginerjm#1617028

If I remember correctly, these should not hold boolean:
<i>
</i>$result = mysqli_stmt_bind_result($stmt,$email);
$result = mysqli_stmt_get_result($stmt);
$result = mysqli_stmt_store_result($stmt);
[code]

Certainly not this one ...
[code]
$result = mysqli_stmt_bind_result($stmt,$email);


Ok. Let me read, re-read & re re read what you said earlier ONs. ;)

But don't go away. JUST YET! Lol!
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — @ginerjm#1617025

So, results found from a quesry is called "resource" in php ? Thanks. I learnt something.

If you really must know, I don't understand a lot of things when I get errors. Like "objects" and stuffs when errors mention "objects".

Anyway, let me revise on
<i>
</i>$result = mysqli_stmt_bind_result($stmt,$email);
$result = mysqli_stmt_get_result($stmt);
$result = mysqli_stmt_store_result($stmt);


And the fetchall and stuff. Gone a bit rusty.
Copy linkTweet thisAlerts:
@ginerjmApr 05.2020 — I don't care about these statements. They are not part of our discussion. READ THE F..... MANUAL if you want to learn.
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — @ginerjm#1617032

Reading TFM.

Just learnt, you can't use 'mysqli_stmt_num_rows' without using 'mysqli_stmt_store_result'.

You lot could have told me this few days back. And I would have corrected this:
<i>
</i>$result = mysqli_stmt_get_result($stmt);
if(mysqli_stmt_num_rows($result) == 1)


to this:

<i>
</i>$result = mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_result($stmt) == 1)


or to this:

<i>
</i>$result = mysqli_stmt_store_result($stmt);
if($result) == 1)


And then tested which one works. Most likely the first one if not the last one.

Anyway, testing now.

Just hold on for me to bug you even more tonight as a punishment for not mentioning these things eariler!

(Joke)!
Copy linkTweet thisAlerts:
@ginerjmApr 05.2020 — I think you were told by others to use the EASY WAY TO DO THIS at the beginning. You just check the num rows function. Or you simply do a fetch and read the value of that count function you insisted on using.

Here it is again - DIRECT FROM THE MANUAL.:
<i>
</i>
if ($result = $mysqli-&gt;query("SELECT (whatever) FROM table where something = something");
{
/* determine number of rows result set */
$row_cnt = $result-&gt;num_rows;
if ($row_cnt == 0)
go do login
else
handle the fact that the user is logged in.
}

And no use of your function. Just the normal one for a query result.
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — @ginerjm#1617036

Ginerjm,

My mate, I already know how to do it like that.

Look, I did this few yrs back ...

(yeah, I know. The code is too much. But you just concentrate on the SQL QUERY and you will see I did it few yrs back as you suggested just few mins above and earlier another day. I did the SQL query without the COUNT.

<i>
</i>//Query 'users' tbl for User's "Account Details".
$query_for_profile_user_account_details = "SELECT id,date_and_time,username,password,account_activation_status,id_video_verification_status,id_verification_video_file_url,recruits_total,recruits_active,sponsor_recruited_number,sponsor_username,upline_2_username,upline_3_username,upline_4_username,upline_5_username,upline_6_username,age_range,primary_website_domain,primary_website_email,registering_country,registering_city,registering_ip,registering_browser,registering_os,registering_isp FROM users WHERE $querying_column = ?";
if($stmt_for_profile_user_account_details = mysqli_prepare($conn,$query_for_profile_user_account_details))
{
mysqli_stmt_bind_param($stmt_for_profile_user_account_details,'s',$login_username_or_email_or_domain);
mysqli_stmt_execute($stmt_for_profile_user_account_details);
$result_for_profile_user_account_details = mysqli_stmt_bind_result($stmt_for_profile_user_account_details,$db_user_id,$db_account_creation_date_and_time,$db_username,$db_password,$db_account_activation_status,$db_id_video_verification_status,$db_id_verification_video_file_url,$db_recruits_total,$db_recruits_active,$db_sponsor_recruited_number,$db_sponsor_username,$db_upline_2_username,$db_upline_3_username,$db_upline_4_username,$db_upline_5_username,$db_upline_6_username,$db_age_range,$db_primary_website_domain,$db_primary_website_email,$db_registering_country,$db_registering_city,$db_registering_ip,$db_registering_browser,$db_registering_os,$db_registering_isp);
mysqli_stmt_fetch($stmt_for_profile_user_account_details);

<i> </i> mysqli_stmt_close($stmt_for_profile_user_account_details);
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> mysqli_stmt_close($stmt_for_profile_user_account_details);
<i> </i>
<i> </i> mysqli_close($conn);
<i> </i>
<i> </i> die("&lt;h1 style="font-family:courier; color:red; text-size:25%; text-align:center"&gt;
<i> </i> &lt;b&gt;
<i> </i> ERROR STATEMENT 3: Sorry! Our system is currently experiencing a problem! Or, Incorrect Login Details!
<i> </i> &lt;/b&gt;
<i> </i> &lt;/h1&gt;
<i> </i> ");
<i> </i>}
<i> </i>
<i> </i>if (!password_verify($login_password,$db_password))
<i> </i>{
<i> </i> mysqli_close($conn);
<i> </i>
<i> </i> die("&lt;h1 style="font-family:courier; color:red; text-size:25%; text-align:center"&gt;
<i> </i> &lt;b&gt;
<i> </i> ERROR STATEMENT 4: Incorrect Login Details!
<i> </i> &lt;/b&gt;
<i> </i> &lt;/h1&gt;
<i> </i> ");
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> //Query 'details_personal' tbl for User's "Personal Details".
<i> </i> $query_for_profile_user_personal_details = "SELECT id,date_and_time,details_personal_verified_by_recruiter,title,first_name,middle_name,surname,gender,date_of_birth,skin_complexion,height,weight,sexual_orientation,religion,education,profession,marital_status,working_status,country_of_birth,bio FROM details_personal WHERE username = ?";
<i> </i> if($stmt_for_profile_user_personal_details = mysqli_prepare($conn,$query_for_profile_user_personal_details))
<i> </i> {
<i> </i> mysqli_stmt_bind_param($stmt_for_profile_user_personal_details,'s',$db_username);
<i> </i> mysqli_stmt_execute($stmt_for_profile_user_personal_details);
<i> </i> $result_for_profile_user_personal_details = mysqli_stmt_bind_result($stmt_for_profile_user_personal_details,$db_details_personal_submission_id,$db_details_personal_submission_date_and_time,$db_details_personal_verified_by_recruiter,$db_title,$db_first_name,$db_middle_name,$db_surname,$db_gender,$db_date_of_birth,$db_skin_complexion,$db_height,$db_weight,$db_sexual_orientation,$db_religion,$db_education,$db_profession,$db_marital_status,$db_working_status,$db_country_of_birth,$db_bio);
<i> </i> mysqli_stmt_fetch($stmt_for_profile_user_personal_details);
<i> </i>
<i> </i> mysqli_stmt_close($stmt_for_profile_user_personal_details);
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> mysqli_stmt_close($stmt_for_profile_user_personal_details);
<i> </i>
<i> </i> mysqli_close($conn);
<i> </i>
<i> </i> die("&lt;h1 style="font-family:courier; color:red; text-size:25%; text-align:center"&gt;
<i> </i> &lt;b&gt;
<i> </i> ERROR STATEMENT 5: Sorry! Our system is currently experiencing a problem!&lt;/b&gt;
<i> </i> &lt;/h1&gt;
<i> </i> ");
<i> </i> }
<i> </i>
<i> </i> //Query 'details_contact_home' tbl for User's "Home Contact Details".
<i> </i> $query_for_profile_user_home_contact_details = "SELECT id,date_and_time,personal_blog,personal_email,personal_mobile,home_land_line_phone,home_fax,home_zip,home_town,home_neighbourhood,home_borough,home_council,home_city,home_county,home_district,home_region,home_state,home_country FROM details_contact_home WHERE username = ?";
<i> </i> if($stmt_for_profile_user_home_contact_details = mysqli_prepare($conn,$query_for_profile_user_home_contact_details))
<i> </i> {
<i> </i> mysqli_stmt_bind_param($stmt_for_profile_user_home_contact_details,'s',$db_username);
<i> </i> mysqli_stmt_execute($stmt_for_profile_user_home_contact_details);
<i> </i> $result_for_profile_user_home_contact_details = mysqli_stmt_bind_result($stmt_for_profile_user_home_contact_details,$db_details_contact_home_submission_id,$db_details_contact_home_submission_date_and_time,$db_personal_blog,$db_personal_email,$db_personal_mobile,$db_home_land_line_phone,$db_home_fax,$db_home_zip,$db_home_town,$db_home_neighbourhood,$db_home_borough,$db_home_council,$db_home_city,$db_home_county,$db_home_district,$db_home_region,$db_home_state,$db_home_country);
<i> </i> mysqli_stmt_fetch($stmt_for_profile_user_home_contact_details);
<i> </i>
<i> </i> mysqli_stmt_close($stmt_for_profile_user_home_contact_details);
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> mysqli_stmt_close($stmt_for_profile_user_home_contact_details);
<i> </i>
<i> </i> mysqli_close($conn);
<i> </i>
<i> </i> die("&lt;h1 style="font-family:courier; color:red; text-size:25%; text-align:center"&gt;
<i> </i> &lt;b&gt;
<i> </i> ERROR STATEMENT 6: Sorry! Our system is currently experiencing a problem!
<i> </i> &lt;/b&gt;
<i> </i> &lt;/h1&gt;
<i> </i> ");
<i> </i> }


I used to know how to do it with the Sql COUNT but have forgotten it. That is why I have revolved this thread around the COUNT.

Question is, is it possible to use both Sql COUNT and Php's mysqli_stmt_num_rows on the same php script. That is the big question as i am trying to use both on the same php script.

Oh by the way, by the looks of things, you do not need to use mysqli_smt_store_result($stmt) before using mysqli_stmt_num_rows($stmt). Look what I been glossing over:

https://hotexamples.com/examples/-/-/mysqli_stmt_num_rows/php-mysqli_stmt_num_rows-function-examples.html
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — @ginerjm,

I was just told:

"Use COUNT (or EXISTS) function rather than mysqli_stmt_num_rows which will be 1 everytime. It count of results - no result value. If no mail exists, then COUNT result will be 0 and count of num_rows will be 1. Check result only. "

Can you show me how to use this "EXIST" that he is refering to ? Show me 2 examples.

And show me 2 examples how to use the COUNT too.

Thank you very much in advance!

Btw, if email doesn't exist in db and the Sql COUNT is 0, then why the mysqli_stmt_num_rows will be 1 ? Since zero rows get found then mysqli_stmt_num_rows result should be 0 too. Should not it ?
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — I was just told this ...

"Query for check exists: SELECT EXISTS(SELECT 1 FROM users WHERE personal_email = ?) AS mail_exists - result is 0 or 1 if exists."

Frankly, I did not understand any of that.

Let us see if I understood the following or not. Correct me if I am wrong.

"mysqli_stmt_num_rows return count of rows. COUNT return number of records which are equal your where statement. Number of records from COUNT will be one row - row with COUNT value."

Oh I get it. Mysql will output 1 single row with the value "0" or "nil" (to indicate email doesn't exist in db). Now mysqli_stmt_num_rows doesn't actually count matching rows (like I thought it did). It just counts what result been outputted. And since 1 result/row has been outputted with the value "0", then it would count as "1".

And therefore, I have been advised not to use the mysqli_stmt_num_rows($stmt) here but the Sql's COUNT or the SELECT EXIST. WIll have to checkout what this SELECT EXIST is. Guessing it is an SQL command and not php since it is in CAPITALS.

Anyway, Ginerjm or somebody welcome to show a php sample code how to make use of this EXIST.

Thanks
Copy linkTweet thisAlerts:
@NogDogApr 05.2020 — > @developer_web#1617012 So you are saying, if I use the IF to compare some value then I must always use the "==" sign within the if brackets. using the "=" is pointless here.

Basically, yes. A single "=" is _not_ a comparison operator, so do not use it if you want to compare if the things on either side of it are equal. That does not mean you can never use it in a conditional expression like in an if(), but only if doing an _assignment_ of the value on the right to the variable on the left makes sense -- which probably most of the time it does not.
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — 3:33am here and I am nakid! (Tired). I said "nakid" and not "naked"! Lol!

Going to sleep. Will mess just one more night or so with the COUNT and if I can manage to get things right then I continue with the COUNT. Else, will do as Jinergm suggested.

@ginerjm,

Don't forget to read and address my previous 2 posts.

Thanks man!
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — @NogDog#1617042

Cheers!

You're welcome to address my other posts. Especially the last 2 to Ginerjm. I think he's gone to sleep now.

Thanks and good night!
Copy linkTweet thisAlerts:
@developer_webauthorApr 05.2020 — NogDog,

See if you can explain to me, with 2 examples, what is meant by this ...

"Query for check exists: SELECT EXISTS(SELECT 1 FROM users WHERE personal_email = ?) AS mail_exists - result is 0 or 1 if exists."
Copy linkTweet thisAlerts:
@ginerjmApr 06.2020 — I am done with you.. As I should have been if I remember who you were.

Just a jerk.
Copy linkTweet thisAlerts:
@developer_webauthorApr 06.2020 — @ginerjm#1617048

What you do mean ?
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...