selecting where in php mysql if where is equals to array index - php

hey guys im trying to query something in php but the WHERE is a index from array this is what i did
$data=array();
while ($row = mysql_fetch_array($result))
{
$item = array(
'sec_id'=>$row['section_id'],
'sec_name'=>$row['section_name'],
'sec_dept'=>$row['section_department'],
'sec_lvl'=>$row['section_level'],
'advisory_id'=>$row['advisory_id'],
'first_name'=>$row['f_firstname'],
'last_name'=>$row['f_lastname'],
'middle_name'=>$row['f_middlename'],
'advisor_id'=>$row['faculty_id'],
);
$get_subjects = "SELECT subject_name
FROM subjects
WHERE level = '".$row['section_level']."' ";
$result_get_subjects =mysql_query($get_subjects)or die(mysql_error());
$subjects_count = mysql_num_rows($result_get_subjects);
$check_archive_subjects = " SELECT b.subject_name
FROM registrar_grade_archive a
LEFT JOIN subjects b ON(a.subject_id=b.subject_id)
LEFT JOIN section c ON(a.section_id = c.section_id)
WHERE a.advisor_faculty_id ='".$row['faculty_id']."'
WHERE a.section_id ='".$row['section_id']."'
GROUP BY b.subject_name ASC
" ;
$query_checking =mysql_query($check_archive_subjects)or die(mysql_error());
$subjects_count_sent = mysql_num_rows($query_checking);
but unfortunately i got an error in $check_archive_subjects that says:
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 a.section_id ='24'
is there any other way to put an array index in where clause in mysql. I know mysql is deprecated and ill be switching to mysqli after i finished this project so pardon me guys. thanks in advance

Multiple WHERE conditions should be joined using boolean keywords AND or OR. You don't issue multiple WHERE clauses.
Also, please read this regarding the MySQL extension - https://stackoverflow.com/a/12860046/283366

Try the below query
SELECT b.subject_name
FROM registrar_grade_archive a
LEFT JOIN subjects b ON(a.subject_id=b.subject_id)
LEFT JOIN section c ON(a.section_id = c.section_id)
WHERE a.advisor_faculty_id ='".$row['faculty_id']."' AND a.section_id ='".$row['section_id']."'
GROUP BY b.subject_name ASC
Explanation
Using multiple WHERE clause like above is not accepted in MySQL. Try replacing the 2nd WHERE using an AND or using an OR depending on your requirement. I have used AND

Related

How to user SQL two table field.? PHP

Here I want to access two table field but I cant get success. Here is my little code. please check that. I want to access Analysis.Right and tabl3.right.
I am printing its with foreach loop. like $res['Right'] of Analysis and $res['right'] of tabl3. when I try to print this it's show me error
Undefind index Right.
any one can help me
$qry = "select Analysis.Q_Id, tabl3.Q_Id, Analysis.Right, tabl3.right from tabl3 INNER JOIN Analysis ON Analysis.Q_Id = tabl3.Q_Id where Analysis.Q_Id = 3";
please help..
you have tow column with right name related to different table so there is not a column name right but 'Analysis.Right ' or 'tabl3.right'
or you can assign an alias for set the column name equalt to Right where you need .. eg:
$qry = "select
Analysis.Q_Id
, tabl3.Q_Id
, Analysis.Right as Right
, tabl3.right as Right_t3
from tabl3
INNER JOIN Analysis ON Analysis.Q_Id = tabl3.Q_Id where Analysis.Q_Id = 3";
Your result set has columns with the same name. Give them different names:
select t3.Q_Id, a.Right as a_right, t3.right as t3_right
from tabl3 t3 inner join
Analysis a
on a.Q_Id = t3.Q_Id
where a.Q_Id = 3;
When you look for the names in your code, look for a_right and t3_right.
Note that you don't need to return Q_Id twice. The ON clause guarantees that the values are the same.

MySql union Error in codeigniter

I have to fetch values from two tables, I have wrote separate query for fetching from both tables. and both queries worked properly. but I need to get this in one result object. so that I have joined queries using UNION operator. but it throws some error. the query is given below
$query1 = "SELECT dev_members.name,dev_members.id,dev_members.age,dev_members.family_id,dev_family.house_name,dev_ib_account_registration.account_id FROM (dev_members)
JOIN dev_family ON dev_family.id=dev_members.family_id
JOIN dev_ib_account_registration ON dev_ib_account_registration.member_id=dev_members.id
UNION
SELECT dev_members.name,dev_members.id,dev_members.age,dev_members.family_id, dev_family.
house_name,dev_ib_sub_member_registration.account_id FROM (dev_members)
JOIN dev_family ON dev_family.id=dev_members.family_id
JOIN dev_ib_sub_member_registration ON dev_ib_sub_member_registration.member_id=dev_members.id";
$result = $this->db->query($query);
return $result->result();
and the error is:
1064 - 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 'JOIN dev_family ON dev_family.id=dev_members.family_id JOIN dev_ib_s' at line 8
There is something wired with the usage of brackets in your second query
(dev_members) <---
when i use your query without them in Fiddle query works ,but using with brackets it produces syntax error ,so try them without ()
updated query
SELECT
dev_members.name,
dev_members.id,
dev_members.age,
dev_members.family_id,
dev_family.house_name,
dev_ib_account_registration.account_id
FROM
dev_members
JOIN dev_family
ON dev_family.id = dev_members.family_id
JOIN dev_ib_account_registration
ON dev_ib_account_registration.member_id = dev_members.id
UNION
SELECT
dev_members.name,
dev_members.id,
dev_members.age,
dev_members.family_id,
dev_family.house_name,
dev_ib_sub_member_registration.account_id
FROM
dev_members
JOIN dev_family
ON dev_family.id = dev_members.family_id
JOIN dev_ib_sub_member_registration
ON dev_ib_sub_member_registration.member_id = dev_members.id

How should I use array_intersect function in php instead of MySQL intersection?

I have got the results from the several sql phrase in php-MySQL.
My $sql1 and the result query phrases is as like below.
$sql1= "SELECT code_co.code, code_co.disease_co, code_en.disease_en, ds.ds_url
FROM code_co
LEFT JOIN code_en ON code_en.code = code_co.code
LEFT JOIN note ON note.code = code_co.code
...
where condition
$result1= mysqli_query($con,$sql1);
But I have the 9 sql query phrases as like above code. selected columns are same in the 9 sql query. That is
$sql2= "SELECT code_co.code, code_co.disease_co, code_en.disease_en, ds.ds_url
FROM code_co
LEFT JOIN code_en ON code_en.code = code_co.code
LEFT JOIN note ON note.code = code_co.code
...
where condition
$result2= mysqli_query($con,$sql2);
...
$sql9= "SELECT code_co.code, code_co.disease_co, code_en.disease_en, ds.ds_url
FROM code_co
LEFT JOIN code_en ON code_en.code = code_co.code
LEFT JOIN note ON note.code = code_co.code
...
where condition
$result9= mysqli_query($con,$sql9);
Finally I want to get the intersection of the 9 results, but MySQL is not supported.
So I want to solve that by php code.
That is array_intersect() function.
But I am at a loss how I use that.
while($row1= mysqli_fetch_array($result1))
while($row2= mysqli_fetch_array($result2))
....
while($row9= mysqli_fetch_array($result9))
$intersect = array_intersect($row1, $row2, $row3, $row4, ...., $row9);
print_r($intersect);
mysqli_close($con);
?>
There is no error message but, nothing is displayed.
I first use array_intersect function in this code.
please, Give me a piece of advice.
Thank you for your concern.
Doing it with PHP is going to be DEATHLY, and highly resource consumer. If you just made an INNER JOIN between all big queries using as condition in the ON the key field you want to intersect, you'd directly have the intersection you look for.
Other thing, maybe nothing is displayed because you're not throwing any error to the screen, check your PHP error configuration in your system and application to enable them where developing.

mySQL 5.5 syntax error

Can someone please tell me what is wrong with the syntax shown below for MySQL 5.5
$sql = "select tm.*, tt.tax_rate as tax from ".TABLE_MEMBER." tm left join ".TABLE_TAX_RATES." tt on tt.tax_rates_id = tm.rent_tax_class_id where plan_id='".$planid."'";
$rs = tep_db_query($sql);
$row_days = tep_db_fetch_array($rs);
if (!$row_days['tax']) $row_days['tax'] = 0;
tt.* AS is invalid syntax because you cannot alias multiple columns. You have to write out each column individually (and alias those as needed), or simply accept using *, which you should avoid using in production. The query in your comment is different from the one in your question which should not cause this error.
You should also be using properly parameterized queries -- your query is vulnerable to injection.
You missed to mention table alias in where clause
The sql should be
$sql = "select tm.*, tt.tax_rate as tax from ".TABLE_MEMBER." tm left join ".TABLE_TAX_RATES." tt on tt.tax_rates_id = tm.rent_tax_class_id where tm.plan_id='".$planid."'";
or
$sql = "select tm.*, tt.tax_rate as tax from ".TABLE_MEMBER." tm left join ".TABLE_TAX_RATES." tt on tt.tax_rates_id = tm.rent_tax_class_id where tt.plan_id='".$planid."'";

PHP MySQL - selecting values from table 1 which are not in table 2

This may be simple to solve but I'm having trouble with this piece of code - I'm a self taught newbie with PHP, and the code I've come up with doesn't seem to want to work.
The pages are for an online entry system for a sports competition. Judges' details are stored in table "club_judges", and users can enter them into a competition by copying them into "competition_judges". This is done via a checkbox form in a table.
I want to add functionality whereby judges who are already added to the competition do not appear in the import form, however my code does not seem to work. I am using a unique field of "bg_number" (the sport's identification number) to search for an existing entry.
Current code:
$existing_judges = mysql_query("SELECT bg_number FROM competition_judges WHERE competition='Test Competition'");
$existing_judges_fetch = mysql_fetch_array($existing_judges);
$existing_judges_array = "('" . implode( "', '", $existing_judges_fetch ) . "');" ;
$query = "SELECT * FROM club_judges WHERE (`club`='Test Club') AND (`bg_number` NOT IN '$existing_judges_array') ORDER BY name ";
$result = mysql_query($query) or die(mysql_error());
Error displayed:
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 ''('1234567', '1234567');') ORDER BY name' at line 1
For reference 1234567 is the bg_number for my test judge.
Any help would be greatly appreciated!
why dont u use just one query
SELECT *
FROM club_judges
WHERE `club`='Test Club'
AND `bg_number`
NOT IN (SELECT bg_number FROM competition_judges WHERE competition='Test Competition')
ORDER BY name
Use can use a single query instead like this:
$query = "SELECT * FROM `club_judges` WHERE `club`='Test Club'
AND
`bg_number` NOT IN
(SELECT `bg_number` FROM `competition_judges` WHERE `competition`='Test Competition')
ORDER BY `name` ";
$existing_judges_fetch = mysql_fetch_array($existing_judges);
$result = mysql_query($query) or die(mysql_error());
here's a JOIN version
SELECT a.*
FROM club_judges a
LEFT JOIN competition_judges b
ON a.bg_number = b.bg_number AND
b.competition='Test Competition'
WHERE b.bg_number IS NULL AND
a.club = 'Test Club'
ORDER BY a.name
To fully gain knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins

Categories