How to optimize mysql query registration - php

At the time of registration, I am checking for there field username, email, phonenumber
With three query. snippet is following:
$query = "SELECT *
FROM
users
WHERE username='$userName'";
$result = mysql_query($query, $this->con);
$count_username = mysql_num_rows($result);
if($count_username <= 0){
$query = "";$result = "";
$query = "SELECT *
FROM
users
WHERE email='$email' ";
$result = mysql_query($query, $this->con);
$count_email = mysql_num_rows($result);
}
if($count_username <= 0 && $count_email <= 0){
$query = "";$result = "";
$query = "SELECT *
FROM
users
WHERE phone_number='$phone'";
$result = mysql_query($query, $this->con);
$count_phone = mysql_num_rows($result);
}
Is there is way to do this with single query, or is there is other way to optimize this code???
Sorry for using mysql extension.
If i use single query:
$query = "SELECT * FROM users WHERE username='$userName' && email='$email' && phone_number='$phone'";
I am unable to show different error:
username exists
email exists
phone exists
I don't want to show error like:
username/phone/exists exists

You can have something like this:
SELECT CASE WHEN username = '$userName' THEN 'username exists'
WHEN email = '$email' THEN 'email exists'
ELSE 'phone exists'
END Result
FROM users
WHERE username = '$userName'
OR email = '$email'
OR phone_number = '$phone'

Related

update table for multiple users

I am looking to count the number of times 'yes' in present for a user in a table, then post the result into anther table for that same user. Both tables have the username. I would like this done for each user. I have the following but it is not working.
$sql = $item_count = "SELECT SUM(if(strike='yes',1,0)) AS strike_total FROM weekpicks WHERE username = 'username'";
// execute SQL query and get result
$sql_result = mysql_query($sql) or die (mysql_error());
if (!$sql_result) {
echo "Something has gone wrong!";
}
else {
//loop through record and get values
while ($row = mysql_fetch_array($sql_result)) {
$item_result = ($row = #mysql_query($item_count)) or die(mysql_error());
$strike_total = ($row = #mysql_result($item_result,"strike_total"));
$strikes = ($row = $strike_total ['strike_total']);
$username = $row["username"];
// the following will insert number of strikes into table for each user.
$sql = "UPDATE authorize SET strikes = '($strikes)' WHERE username='$username'";
//mysql_query(" UPDATE authorize SET " . "strikes = '" . ($strikes) . "' WHERE username='$username' ");
$result = mysql_query($sql) or die (mysql_error());
Just one query should be enough
Update for single user..
UPDATE authorize SET strikes = (select count(*) from weekpicks WHERE username = '$username' and strike='yes') WHERE username='$username';
For bulk update all users
UPDATE authorize as A SET strikes = (select count(*) from weekpicks B WHERE strike='yes' and A.username=B.username group by B.username)
Isn't that simple.

POST query error

Before I say anything else, I know my code is a mess and very bad.
ok, for some reason when I submit the form everything works fine but it gives me this error yet I'm not sure why at all.
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 'WHERE username = 'Nick'' at line 1
This is my code
if (isset($_POST['slot1']))
{
$token = mysql_real_escape_string($_POST['token']);
$tokenn = strip_tags($token);
$sql55 = "SELECT * FROM user_pokemon WHERE
belongsto = '".$_SESSION['username']."' AND (id='".$tokenn."')";
$result55 = mysql_query($sql55) or die(mysql_error());
$poke55 = mysql_fetch_array($result55);
$_SESSION['idpoke5'] = $poke55['id'];
$sql23 = "SELECT * FROM pokemon WHERE name='".$poke55['pokemon']."'";
$result23 = mysql_query($sql23) or die(mysql_error());
$battle_get23 = mysql_fetch_array($result23);
$result666 = mysql_query("UPDATE users SET ep=ep+".$battle_get23['ep']." WHERE username = '".$_SESSION['username']."'")
or die(mysql_error());
$result5done = mysql_query("DELETE FROM user_pokemon WHERE id = '".$_SESSION['idpoke5']."'")
or die(mysql_error());
}
It's supposed to pretty much exchange the pokemon for points, it does it just fine. but I get the error for some reason afterwards.
Any help will be appreciated, thanks.
try this
$username = mysql_real_escape_string($_SESSION['username']);
$batleget = $battle_get23['ep'];
$result666 = mysql_query("UPDATE users SET ep=ep+CAST($batleget AS UNSIGNED)
WHERE username = '".$username."' ")
or this
$username = mysql_real_escape_string($_SESSION['username']);
$batleget = $battle_get23['ep'];
$result666 = mysql_query("UPDATE users SET ep=ep+ $batleget
WHERE username = '".$username."' ")

Cannot make an SQL query work from PHP

i'm trying to run a very simple PHP function :
function evalLoggedUser($db_conx,$id,$e,$p){
$sql = "SELECT ip FROM users WHERE id={$id} AND email= '$e' AND password= '$p' AND activated=1 LIMIT 1 ";
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if($numrows > 0){
return true;
} else {
echo $sql;
}
}
as part of a user authentication. The problem is that the query is not working and I dont know why! I know the mysql connection is working as I have checked the mysqli_errno and not getting anything there - Can anyone help?
Try this one..
function evalLoggedUser($db_conx,$id,$e,$p){
$sql = "SELECT ip FROM users WHERE id='$id' AND email= '$e' AND password= '$p' AND activated=1 LIMIT 1 ";
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if($numrows > 0){
return true;
} else {
echo $sql;
}
}
Values gets substituted by default inside double quotes. But make sure you check the connection too.
$sql = "SELECT ip FROM users WHERE id=$id AND email= $e AND password= $p AND activated=1 LIMIT 1 ";

include all where cases to make one sql query

Here is the code
if ($st) active_code = '1';
if (!$st) active_code > '0';
SELECT username FROM users WHERE active_code = '1'
SELECT username FROM users WHERE active_code > '0'
Is there is a way to make then one sql query ?
Any idea please ?
$cond = $st ? "= '1'":" > '0'";
$sql = "SELECT username FROM users WHERE active_code $cond";
Just define a variable that will hold the condition:
if ($st) {
$condition = "active_code = '1'";
} else {
$condition = "active_code > '0'";
}
$sql = "SELECT username FROM users WHERE $cond";

how to shorten function query?

hello i have this function:
function mail_exists($email){
global $db;
$email = sanitize($email);
$query = $db->query("SELECT Email FROM table1 WHERE Email= '$email' ");
$check = $query->num_rows;
$query2 = $db->query("SELECT Email FROM table2 WHERE Email= '$email' ");
$check2 = $query->num_rows;
return ($check > 0 || $check2 > 0) ? true : false;
}
first of all i would like to know how i can shorten it by using only one query and second thing is, why this does not work when using two queries? both tables have a different structure. in table1 the field email is no. 16 and on table2 it is field no.6.
thanks alot.
First of all you made a logic error *here in the last lines: $query->num_rows; should be: $query2->num_rows; and then resulting into:
function mail_exists($email){
global $db;
$email = sanitize($email);
$query = $db->query("SELECT Email FROM table1 WHERE Email= '$email' ");
$check = $query->num_rows;
$query2 = $db->query("SELECT Email FROM table2 WHERE Email= '$email' ");
$check2 = $query2->num_rows; // *here
return ($check > 0 || $check2 > 0) ? true : false;
}
Second, you should be using two different queries if you are dealing with two completely different contexts. Don't join queries when you don't need to. If you are just counting rows you can easily do:
function mail_exists($email){
global $db;
$email = sanitize($email);
$query = $db->query("SELECT COUNT(*) FROM table1 WHERE Email= '$email' ");
$query2 = $db->query("SELECT COUNT(*) FROM table2 WHERE Email= '$email' ");
$count1 = $query->fetch_row();
$count2 = $query2->fetch_row();
return ($count1[0] || $count2[0]);
}
The SQL COUNT() function is there to give you the most performant way to count rows.
You might try:
SELECT t1.Email from table1 t1 inner join table2 t2 on t2.Email=t1.Email WHERE t1.Email = '$email'
and then:
return $query->num_rows > 0;
I'm not a PHP expert, so your mileage may vary.
Use can UNION ALL to return an overall count:
SELECT Email
FROM (
SELECT Email FROM table1 WHERE Email= '$email'
UNION ALL
SELECT Email FROM table2 WHERE Email= '$email') t
Or you can use SELECT COUNT(Email) to return the count.

Categories