I have a users table and a users_banlist table.
On my registration form i want to check all in one query whether the username someone entered on form exists in the users table and see if it also exists on the users_banlist table.
I can do them on there own in individual queries but would rather do it all in one.
Here is what I got, but even though I enter a username that is taken it does not tell me it's already taken.
$stmt = $dbh->prepare("
SELECT
users.user_login,
users_banlist.user_banlist
FROM
users ,
users_banlist
WHERE
users.user_login = ? OR
users_banlist.user_banlist = ?");
// checker if username exists in users table or users_banlist table
$stmt->execute(array($username, $username));
if ( $stmt->rowCount() > 0 ) {
$error[] = 'Username already taken';
}
Basically I think it is something to do with the execute or rowCount(), could anyone tell me where I am going wrong? Being new to pdo I'm finding it a little confusing at the moment.
You could use the query
SELECT
users.user_login
FROM
users
WHERE
users.user_login = ?
UNION ALL
SELECT
users_banlist.user_banlist
FROM
users_banlist
WHERE
users_banlist.user_banlist = ?
and then proceed as normal. What you were using is a CROSS JOIN which returns the cartesian product that produce rows which combine each row from the first table with each row from the second table. If one of those tables returns 0 rows you get 0 results
Use SQL JOINS instead
SELECT
users.user_login,
users_banlist.user_banlist
FROM
users INNER JOIN
users_banlist ON users.ID=users_banlist.user_id
WHERE
users.user_login = ? OR
users_banlist.user_banlist = ?
where users.ID and users_banlist.user_id are same
Related
I am creating a Log in and I have separate tables for Users A and Users B.
What I want to do is check first in first table if the Users that trying to Login is in the Table A,
if YES, it will not go to the Table B to check the Login credentials, if NOT, go to Table B and check the Login credentials.
Table A
SELECT * FROM tableA WHERE userId='$userId' AND password='$password'
Table B
SELECT * FROM tableB WHERE accountNumber='$accountNumber' AND password='$password'
Note: The 2 Tables has different Field Name userId and accountNumber.
I presume you are fetching the values of username and password from client side so I will tell you only what you asked for.
$getUserBasic1=$db->prepare('SELECT * FROM tableA WHERE userId="$userId" AND password="$password"');
$getUserBasic1->execute();
$user= $getUserBasic1->fetchAll();
if(count($user)>0)
{
//if yes do what you want here
}
else
{
$getUserBasic2=$db2->prepare('SELECT * FROM tableB WHERE accountNumber="$accountNumber" AND password="$password"');
$getUserBasic2->execute();
$user2= $getUserBasic2->fetchAll();
//write your code here
}
You could use an INNER JOIN and select both table results taking Table A's result first if it exists, else take Table B's result.
Assuming both tables have some sort of reference like the User ID you can use something like this:
SELECT tbla.*, tblb.* FROM tableA tbla
INNER JOIN tableB tblb ON tbla.userId = tblb.userId
WHERE userId='$userId' OR accountNumber='$accountNumber' AND password='$password'
ORDER BY userId ASC
LIMIT 1
The query above uses the cross-reference (userId in this case) and joins both tables together before querying the results. It orders the results by Table A before Table B but limits the result to 1 bringing either Table A or Table B out depending which is null.
Try combining the tables, some thing like:
SELECT * FROM tableA, tableB WHERE tableA.userId='$userId' AND tableA.password='$password' OR tableB.accountNumber='$accountNumber' AND tableB.password='$password'
I have not checked, so may not work, but see if this gets what you are looking for!
Something like this:
$sql = "SQL QUERY FOR TABLEA";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// checking if result in TABLE A
}
else{
//search in TABLE B by updating your sql value.
}
I hope that you want to check for the registered user, the best way to do that is to keep one table and just search there itself keeping the userID as the primary key.
There are two signup tables one for hire and one for work pictures of the both tables are given below:
Table 1:
Table 2
but i want to create single login form for both hire and work user
now i wrote this query for extracting data from both tables,
$sql = " SELECT *
FROM signup_hire
LEFT OUTER JOIN signup_work ON signup_hire.id = signup_work.id
UNION
SELECT *
FROM signup_hire
RIGHT OUTER JOIN signup_work ON signup_hire.id = signup_work.id
WHERE signup_hire.email = '$myusername' and signup_hire.password = '$mypassword'
OR signup_work.email = '$myusername' and signup_work.password = '$mypassword'";
but I am receiving 'invalid email or password' - what is the correct query to use?
Have you tried a simple UNION? Query the first table and add a UNION with the same result on the second table, this will will return the results if it matches one of the tables, keep in mind that column names will the ones on the first table regardless which one matches. Also, be sure both tables have the same number of columns or list the fields you need instead of *
SELECT id,name,email
FROM signup_hire
WHERE signup_hire.email = '$myusername'
AND signup_hire.password = '$mypassword'
UNION
SELECT id,name,email
FROM signup_work
WHERE signup_work.email = '$myusername'
AND signup_work.password = '$mypassword';
I am using Codeigntier to run the a query like this:
$query = $this->db->query("SELECT users.*, user_profiles.* FROM users, user_profiles WHERE " . "users.id = $user_id AND user_profiles.user_id = $user_id");
$row = $query->row();
I have placed an echo to get the last query $this->db->last_query(). Which shows:
SELECT users.*, user_profiles.* FROM users, user_profiles WHERE users.id = 6850 AND user_profiles.user_id = 6850
The result it returns is:
object(stdClass)[24]
public 'id' => string '6849' (length=4)
The id the result shows is 6849. I nearly fell of my chair! I ran the same query on my MySQL database and the id returned for that exact same query is 6850. 6850 is the correct ID.
I have no idea how to debug this as this is the simplest of queries. Any help appreciated.
Explicitly select your columns, and AS *something* alias them. Then check the ID's.
It is likely that the id of the table user_profiles is being shown rather than the actual user id from the users table that you expect as you are not specifying the columns.
I have the following function:
function login_check($email, $password)
{
$email = mysql_real_escape_string($email);
$password = md5($password);
$login_query = mysql_query("SELECT COUNT(`id`) as `count`, `id` FROM `table_name` WHERE `email`='$email' AND `password`='$password'");
return (mysql_result($login_query, 0) == 1) ? mysql_result($login_query, 0, 'id') : mysql_error();
}
I want it to check if the user login is correct in two different tables not only one since I've made another table for users who have authenticated their twitter account with my site.
You'd be better off with a single table that has an "authenticated with Twitter" flag but you can check both with something like this:
select exists(
select 1 from table_name where email = '$email' and password = '$password'
union
select 1 from twitter_table where email = '$email' and password = '$password'
)
MySQL will give you a one (AKA true) if at least one of the tables has what you're looking for and a zero (AKA false) if neither has a match.
Using the select exists(select 1...) trick will also be faster than counting as the database only needs to find one match or check the indexes to know that there are no matches before it returns from the query.
You could create an union view of both tables:
CREATE VIEW combined_accounts AS
(SELECT id, twitter_mail AS mail, password FROM twitter_accounts)
UNION
(SELECT id, mail, password FROM my_accounts);
How would i go about counting the number of rows that match two variables?
I have a table called: users
and fields called: username & referral
I have another table called: comments
and fields called: comment_username
This is the situation, I need to fetch the number of referrals with at least 10 comments (rows in the comments' table) that a specific user has referred.
So i was thinking the code should be something like this crude outline.
$username = 'bob';
$validrefferalcount = 0;
function validreferrals($username){
$referreduser = SQL select * from users where referral='$username';
foreach ($referreduser) {
$numberofcomments = SQL count * from comments where comment_username ='$referreduser';
if ($numberofcomments >= 10){
$validreferralcount = $validreferralcount + 1;
}
}
return $validreferralcount;
}
I apologize for the bad syntax, etc...
Thanks for reading.
What about this query :
SELECT COUNT(*) FROM (
SELECT username, COUNT(*) AS c_comments
FROM users
JOIN comments ON username = comment_username
WHERE referral = 'referral'
GROUP BY username
) t
WHERE t.c_comments > 10;
You should use JOIN in your case. Something like (if I understand correctly)
SELECT count(*) FROM users
RIGHT JOIN comments ON comments.comment_username = users.username
WHERE users.referral = '$username'
You can find more information here
Since my actual post count does not allow for comments yet, some additions to christians answer.
A having clause against the count, so the >= 10 condition is matched would be a good idea