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.
Related
I wrote the following query:
$query1 = "SELECT * FROM session WHERE Id_session IN (SELECT * FROM students_in_session WHERE Username = '$email')";
$res = mysqli_query($conn,$query1);
$query2 ="SELECT * FROM students_in_session WHERE Username='$email'";
$res2 = mysqli_query($conn,$query2);
if (!$res) {
die(mysqli_error($conn));
}else{
while ($row = mysqli_fetch_array($res)) {
print_r($row);
$course = $row['Degree'];
$date = $row['Date'];
$hour = $row['Hour'];
$room = $row['Room'];
}
}
if(!$res2){
die(mysqli_error($conn));
}else{
while ($row = mysqli_fetch_array($res2)) {
print_r($row);
$prof = $row['Professor'];
$assis = $row['Assistent'];
}
}
return "\n\nDegree: ".$course."\n"."Date: ".$date."\n"."Hour: ".$hour."\n"."Room: ".$room."\n"."Prof: ".$prof."\n"."Assistent: ".$assis;
}
Currently using phpmyadmin and testing the query returns the expected result,but using the query in the code the variables are all empty.
These are DB's Table:
session
|Id_Session|Date|Hour|Room|Degree|
students_in_session
|Id_Session|Code|Name|Surname|Username|Professor|Assistent|
In query1 you are just try to compare Id_session with the whole table you need to compere with the session id only.
Just replace this
$query1 = "SELECT * FROM session WHERE Id_session IN
(SELECT * FROM students_in_session WHERE Username = '$email')";
with
$query1 = "SELECT * FROM session WHERE Id_session IN
(SELECT Id_Session FROM students_in_session WHERE Username = '$email')";
I thing it will work for you.
$query1 = "SELECT * FROM session WHERE Id_session IN (SELECT * FROM students_in_session WHERE Username = '$email')";
In this query you must need to select only one column like
$query1 = "SELECT * FROM session WHERE Id_session IN (SELECT Id_session FROM students_in_session WHERE Username = '$email')";
Check it.
when you use a IN over a nested query, the nested query can return only one column:
SELECT * FROM session WHERE Id_session IN
(SELECT Id_Session FROM students_in_session WHERE Username = '$email')
However a nested query is not the best approach in your case, because MySQL will have to execute 2 queries. You would better do an INNER JOIN :
SELECT S.*
FROM session S
INNER JOIN students_in_session SS ON SS.Id_Session=S.Id_Session
WHERE SS.Username = '$email'
I want to find out my userid from two table by username where username have already stayed in table2 or not. That means if username match in any table my query collect its userid.
table 1: username --- userid > john --- 100
table 2: username --- userid > colin --- 101
I read many article here nut I cannot understand what should I do.
Now think: I need john's uesrid,
So I tried:
$username = "john";
$q = "SELECT userid FROM table1,table2 WHERE table1.username = '$username' or table2.username = '$username'";
$result = mysqli_query($this->connection, $q);
From what i understand you want to check if the name already exist in one of your tables.
Here is the easy php code:
<?php
session_start();
include 'db_connect.php';
$check_1 = mysqli_query("SELECT * FROM Table_1 WHERE Username = '$Username'");
$check_2 = mysqli_query("SELECT * FROM Table_2 WHERE Username = '$Username'");
if(mysqli_num_rows($check_1)==0){
//Didnt exist in table 1
}
else if(mysqli_num_rows($check_2)==0){
//No result in table 2 either.
}
else{
//what you want to do if the username doesnt exist
}
Let me know how it worked out for you :)
You could select both userid fields and afterwards look if one of them is set:
$username = "john";
$q = "SELECT table1.userid AS userid1,table2.userid AS userid2 FROM table1,table2 WHERE table1.username = '$username' or table2.username = '$username'";
$result = mysqli_query($this->connection, $q);
Try this:
$username = "john";
$q = "SELECT table1.userid FROM table1 JOIN table2 ON table1.username=table2.username WHERE table1.username = '$username'";
$result = mysqli_query($this->connection, $q);
JOIN will join table2 to table1 if the maching pattern (table1.username=table2.username) equals true or exclude this row from table1 from results.
You can use UNION clause
$q="SELECT t1.userid FROM table1 as t1 WHERE username='$username' UNION SELECT t2.userid FROM table2 as t2 WHERE username='$username'";
You can use left join
$username = "username";
$q = "SELECT userid
FROM table1
LEFT JOIN table2 ON table1.username = table2.username
WHERE table1.username = '$username' or table2.username = '$username'";
$result = mysqli_query($this->connection, $q);
I have to tables in one database.
users
user_activate
I have to variables in php
$username = foo;
$key = jas823k123ksd34324;
Now I want to select from the table users the attribute user_id where user_username == $username
I do this with this statement
$sql = "SELECT user_id FROM users WHERE user_username = '$username'";
$result = mysqli_query($db, $sql);
while($row = mysqli_fetch_assoc($result)){
$user_id = $row['user_id'];
}
Now I want to select from the table user_activate the attribute user_activate_key where user_activate_key == $key;
For this I use this statement:
$sql2 = "SELECT user_activate_key FROM user_activate WHERE user_activate_key = '$key'";
$result2 = mysqli_query($db, $sql2);
while($row = mysqli_fetch_assoc($result)){
$user_key = row['user_activate_key'];
}
Can I do both statements in one statement?
As you've written it, two seperate queries is the correct way to do it. But I suspect that there's some kind of relationship between users and user_activate that might make what you're asking for make sense. Assuming that a user_activate_key is tied to a specific user_id, you could do something like the following:
select users.user_id, ua.user_activate_key
from users u
left join user_activate ua
on u.user_id = ua.user_id
and ua.user_activate_key = '$key'
where u.username = '$username'
The LEFT JOIN means that the user will be shown even if there isn't a matching user_activate_key record.
I am new to both mysql and php.
I have two tables which are 'members' and 'points'. Both of them including the column 'username'. I want to select all the values from these two tables where username= $POST[username].
So I wrote this but this is not working.
$username = $_POST['username'];
$sql = $con->prepare("SELECT *.members, *.points FROM members, points WHERE
username=?");
$sql->bind_param("s", $username);
$sql->execute();
$result = $sql->get_result();
$row = $result->fetch_assoc();
And I tried this :
$sql = $con->prepare("SELECT * FROM members INNER JOIN points
ON username.points = username.members WHERE username=?");
$sql->bind_param("s", $username);
$sql->execute();
$result = $sql->get_result();
$row = $result->fetch_assoc();
And this:
$sql = $con->prepare("SELECT *.points, *.members FROM members INNER JOIN points
ON username.points = username.members WHERE username=?");
$sql->bind_param("s", $username);
$sql->execute();
$result = $sql->get_result();
$row = $result->fetch_assoc();
I can't use UNION because the number of columbs are not equel in these tables.
So, Please help me what is wrong with the code? What is the proper way to select all from multiple tables.
Alias are meant to be used to specify to which table those column belong, so you need to prepend table name to your columns
SELECT * FROM members
INNER JOIN points
ON points.username = members.username
WHERE points.username = ?
You can otherwise assign an alias to your table while selecting and use them
SELECT * FROM members a
INNER JOIN points b
ON a.username = b.username
WHERE a.username = ?
You were close with this:
SELECT *.points, *.members
FROM members
INNER JOIN points ON username.points = username.members
WHERE username=?
Try this instead:
SELECT *
FROM members
INNER JOIN points ON members.username = points.username
WHERE members.username=?
check this
SELECT * FROM points,members WHERE points.username="'.$_POST['username'].'" AND members.username="'.$_POST['username'].'";
you can check this query it is very simple.
How can I grab the count value from the query MySQL query below using PHP.
Here is My MySQL code.
$dbc = mysqli_query($mysqli,"SELECT COUNT(*) FROM((SELECT users_friends.id
FROM users_friends
INNER JOIN users ON users_friends.user_id = users.user_id
WHERE users_friends.user_id = 1
AND users_friends.friendship_status = '1')
UNION
(SELECT users_friends.id
FROM users_friends
INNER JOIN users ON users_friends.friend_id = users.user_id
WHERE users_friends.friend_id = 1
AND users_friends.friendship_status = '1')) as friends");
using SQL_CALC_FOUND_ROWS should simplify things:
$dbc = mysqli_query($mysqli,"SELECT SQL_CALC_FOUND_ROWS users_friends.id
FROM users_friends
INNER JOIN users ON users_friends.user_id = users.user_id
WHERE users_friends.user_id = 1
AND users_friends.friendship_status = '1'
");
then afterwards do
$rs = mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
$rec = $rs->fetch_array();
$count = $rec[0];
This method will return the number of records that match the query, ignoring any LIMIT statement, whereas using $rs->num_rows will only give you the number of records actually returned. Depends which one you want.
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {
printf("Select returned %d rows.\n", mysqli_num_rows($result));
/* free result set */
mysqli_free_result($result);
http://us.php.net/manual/en/mysqli.query.php
Assuming that you are correctly connected to the MySQL server and your query are executed correctly, you can use the following code:
$values = mysql_fetch_row($dbc);
$count = $values[0];
Your query should look like SELECT COUNT(*) as numThings FROM xxx
The numThings is what you will reference in PHP:
$result = mysql_query("SELECT COUNT(*) as `numThings` FROM xxx");
$row = mysql_fetch_assoc($result);
$count = $row['numThings'];