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";
Related
How to loop php using ref data from mysql ?
work flow :
Loop If column 'xxx' == ''
{
$sql = "SELECT * FROM table WHERE xxx == '' order by id desc";
$result = mysql_query($sql);
$datas=mysql_fetch_array($result);{
$id = stripslashes(str_replace('\r\n', '<br>',($datas['id'])));
}
$sql = "UPDATE table SET xxx = 'test' WHERE id = '$id' ";
$dbQuery = mysql_query($sql);'
}
ypur query is confusing. why would you store ids with html in them? regardless, this should work
$sql = "SELECT * FROM table WHERE xxx != '' "; //ORDER BY will not work since ids are not int
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result){
$id = stripslashes(str_replace('\r\n', '<br>',($row['id'])));
$sql = "UPDATE table SET xxx = 'test' WHERE id = '$id' ";
$dbQuery = mysql_query($sql);'
}
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'
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.
I have now three different PHP pages that contain almost the same information so to be able to reduce this to one page I need to have a php variable inside the mysql query.
Today it is like this:
$query1 = "SELECT * FROM `Yrker` WHERE `Kategori` = '1' AND `Bruk` = '1' ORDER BY yearstart DESC, mndstart DESC";`
I need that the " AND Bruk = '1'" is removed from this query-line if i put ?nobruk=no in the adressbar. Is this possible and if so, how?
You don't want to (and can't) put an if inside your query; you want to use an if to create your query based on some condition. There are lots of ways to write this, one of which is
if (!empty($_GET['nobruk'])) {
$query1 = "SELECT ... WHERE `Kategori` = '1' ORDER BY ...";
}
else {
$query1 = "SELECT ... WHERE `Kategori` = '1' AND `Bruk` = '1' ORDER BY ...";
}
Another way, which is shorter and involves the ternary operator, is
$includeBruk = empty($_GET['nobruk']);
$query1 = "SELECT ... WHERE `Kategori` = '1' ".
($includeBruk ? "AND `Bruk` = '1' " : "").
"ORDER BY ...";
A simple if statement:
$query1 = "SELECT * FROM `Yrker` WHERE `Kategori` = '1'";
if ($_GET['nobruk']!='no') {
$query1.=" AND `Bruk` = '1'";
}
$query1.= " ORDER BY yearstart DESC, mndstart DESC";
Like this :
<?php
$query = ($_REQUEST['nobruk'] == "no") ? "SELECT * FROM `Yrker` WHERE `Kategori` = '1' ORDER BY yearstart DESC, mndstart DESC": "SELECT * FROM `Yrker` WHERE `Kategori` = '1' AND `Bruk` = '1' ORDER BY yearstart DESC, mndstart DESC";
echo $query;
?>
$query1 = "SELECT * FROM `Yrker` WHERE `Kategori`='1' ".($_GET['nobruk'] === 'no' ? "" : "AND `Bruk`='1' ")."ORDER BY yearstart DESC, mndstart DESC";
I have a sql query :
$cond = "";
if($cid >0 )
{
$quali = $this->getCandidatesQualification($cid);
$cond = "WHERE emp_qualification LIKE '%$quali%'";
}
$sql = "SELECT
emp_job_id,emp_job_profie,emp_qualification,emp_experience
FROM
tbl_emp_data
$cond
ORDER BY job_add_date DESC LIMIT 0,10
";
$res = $this->db->returnArrayOfObject($sql,$pgin = 'no', $odr='no');
Now what I want if emp_qualification field is equal to any_graduate I want to select all the jobs for the candidate even if his qualification is say BA .
so modify your WHERE clause to
WHERE emp_qualification LIKE '%$quali%'
OR emp_qualification = 'any_graduate'
$cond = '';
if($cid >0 ) {
if ($this->getCandidatesQualification($cid) != 'any_graduate') {
$cond = "WHERE emp_qualification LIKE '%{$this->getCandidatesQualification($cid)}%'";
}
}
simplified, try this