I have this mysql query which runs fine:
$query = "SELECT
USER.USER_ID, NAME, SURNAME, EMAIL, STATUS, PHOTO
FROM FRIENDLIST, USER, USER_PROFILE
WHERE FRIENDLIST.FRIEND_ID = ANY (
SELECT FRIEND_ID FROM FRIENDLIST WHERE USER_ID=".$userID."
)
AND APPROVED='YES'
AND USER.USER_ID=FRIENDLIST.FRIEND_ID
AND USER.USER_ID=USER_PROFILE.USER_ID
UNION
SELECT
USER.USER_ID, NAME, SURNAME, EMAIL, STATUS, PHOTO FROM FRIENDLIST, USER, USER_PROFILE
WHERE FRIENDLIST.FRIEND_ID = " . $userID . "
AND APPROVED = 'YES'
AND USER.USER_ID=FRIENDLIST.USER_ID
AND USER.USER_ID=USER_PROFILE.USER_ID";
When I add a few more fields in the query I get a "Document is empty" error.
$query = "SELECT
USER.USER_ID, NAME, SURNAME, EMAIL, STATUS, PHOTO, gender, cposition, rinterest, about
FROM FRIENDLIST, USER, USER_PROFILE
WHERE FRIENDLIST.FRIEND_ID = ANY (
SELECT FRIEND_ID FROM FRIENDLIST WHERE USER_ID=" . $userID . "
)
AND APPROVED='YES'
AND USER.USER_ID=FRIENDLIST.FRIEND_ID
AND USER.USER_ID=USER_PROFILE.USER_ID
UNION
SELECT
USER.USER_ID, NAME, SURNAME, EMAIL, STATUS, PHOTO
FROM FRIENDLIST, USER, USER_PROFILE
WHERE FRIENDLIST.FRIEND_ID = ".$userID."
AND APPROVED='YES'
AND USER.USER_ID=FRIENDLIST.USER_ID
AND USER.USER_ID=USER_PROFILE.USER_ID";
The new fields belong to USER_PROFILE table.
You're using UNION so both SELECT queries must have the exact same number of columns. Otherwise your query will fail and that could give errors like the one you see. This error message probably also indicates that the query result is not checked for errors or failures.
This would be much easier to spot if the code were formatted properly. It is allowed (and advised) to use enters inside a SQL query.
Related
Here is my code that i am trying to query from multiple table but same column data, but not execute this code for success. please help me.
$stmt = $conn->prepare("
SELECT id, name, department, session, username, email, rudf_position
FROM member, executive
WHERE username = ? AND mobile = ? ");
You need to use UNION to search both tables independently.
SELECT id, name, department, session, username, email, rudf_position
FROM member
WHERE username = ? AND mobile = ?
UNION
SELECT id, name, department, session, username, email, rudf_position
FROM executive
WHERE username = ? AND mobile = ?
Forgive me for the simplicity of this question but i am pretty new to this.
I have an access file from which till now retrieved data from one table through php and it was working like a charm. Now i have 2 tables in this file and i need to combine the results of the 2 tables in one table (not combine horizontally with same column but consider one blank table filled with results from both tables). Then i need to order this table. I tried with different syntax but failed.
Please give me your lights!
What i tried to do is this:
<?php
$sql = "SELECT id, Lastname, Firstname, Email FROM `NEW EMPLOYEES`";
$sql .= " UNION SELECT ID, `Last Name`, `First name`, Email FROM `OLD EMPLOYEES`";
$sql .= " ORDER BY Lastname ASC";
?>
As you can see i have different column names per column and this does not shows results.
Also, how could i order the results depending on lastname column?In my case from UNION from one table column name is Lastname and from other table same column is Last Name.
Thank you in advance
try this:
<?php
$sql = "SELECT * from (SELECT id, Lastname, Firstname, Email FROM `NEW EMPLOYEES`";
$sql .= " UNION SELECT ID, `Last Name`, `First name`, Email FROM `OLD EMPLOYEES`) as union_table";
$sql .= " ORDER BY union_table.Lastname ASC";
?>
I've table like this :
tb_users
id, name, pin_number
tb_attendance
pin, date_time, user_id
i've created simple query for tb_attendace like this :
$sql = "INSERT INTO tb_attendance
( pin, date_time)
values
('$PIN', '$DateTime')";
i want to insert colum user_id from tb_users where tb_users.pin_number = tb_attendance.pin
in mysql command i've success run this :
INSERT INTO tb_attendance (pin, date_time, entry_by)
SELECT pin, date_time, tb_users.id
FROM tb_attendance , tb_users
WHERE tb_attendance.pin = tb_users.pin_number
but i don't know how to create this query into php script.
can some one help me to complete the php script ?
I'm not sure why you need both the pin and user id, if you can just use JOIN to get the PIN.
The query that you want looks something like this:
INSERT INTO tb_attendance (pin, date_time, entry_by)
SELECT $PIN, $DATE_TIME, u.id
FROM tb_users u
WHERE u.pin_number = $PIN;
I would advise you to use query parameters, and not to insert the parameter values directly into the SQL string. That is dangerous -- both in terms of creating a SQL syntax error and in terms of security.
My problem is that the value "Arvin", "Tarrega", "Rizal", "Math", "Male" comes from another table which is "student". The value that I have there in the status and date column field comes from a user input. I want to put a statement query which will combine this two into one. Please help me. Btw, the other table doesn't have the status and date field. Only the attendance table has that 2 fields.
table name: attendance
Here is the code I'm using to get that result:
$sql = "INSERT INTO attendance(date, status) VALUES('$_POST[set_date]', '$_POST[status]');
INSERT into attendance(fname, lname, subject, section, gender) SELECT fname, lname, subject, section, gender from student;";
What I would do is to use the fact that you can "select" string in an sql query.
e.g.:
select 'hello' from any_table
In your case, I would do :
$sql = "INSERT into attendance(date, status, fname, lname, subject, section, gender) SELECT '$_POST[set_date]','$_POST[status]',fname, lname, subject, section, gender from student;";
This way you can have all the information you need to insert in one sql query.
I have this error message:
Column 'group_ID' in field list is ambiguous:
Below is my code:
$query = "INSERT INTO members(email, name, gender, dob, profile, password, group_ID)
SELECT group_ID
FROM operations
JOIN members
WHERE operations.group_ID = members.group_ID" ;
$result = $db->query($query);
Trying to insert data into members table, group_ID is from operations but it is a column in the members table.
Your insert won't work, you should learn insert query and join query first, for reference :JOIN and INSERT and then do something like this:
$query = "INSERT INTO members(email, name, gender, dob, profile, password, group_ID)
VALUES(email_value,name_value,gender_value,dob_value,profile_value,password_value,(
SELECT members.group_ID
FROM operations
JOIN members
ON operations.group_ID = members.group_ID) where condition=some_condition)" ;
$result = $db->query($query);