Getting an my Sql Error - php

I am trying to figure out whats wrong in this code but can't make it work
Could you please help me?
$email = $_SESSION['email'];
$email = mysql_real_escape_string($email);
$depst = "SELECT dept FROM stud_reg WHERE email='$email'";
$colls = "SELECT coll FROM stud_reg WHERE email='$email'";
$query="SELECT * FROM stud_reg WHERE coll='$coll' AND dept='$depst'";
$evesel="SELECT id FROM events WHERE `group`='($depst)' AND coll_id='($colls)'";
$studsel="SELECT drs_id FROM event_reg WHERE eve_id='$evesel'";
$query="select * from students WHERE nsite_id='$studsel'";
$result=mysql_query($query) or die(mysql_error());
$num=mysql_num_rows($result);
Here's the error i am getting
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 'k#***.com')' AND coll_id='(SELECT coll FROM stud_reg WHERE email='k#****' at line 1
PS- All the tables and rows in this code exits

The query you're trying to run is
select * from students WHERE nsite_id='SELECT drs_id FROM event_reg WHERE eve_id='SELECT id FROM events WHERE `group`='(SELECT dept FROM stud_reg WHERE email='$email')' AND coll_id='(SELECT coll FROM stud_reg WHERE email='$email')'''
But this is not going to give you what you expect, even if you did fix up the issues with the quoted strings within quoted strings.
I suspect that instead want to combine that all up as joins, so perhaps something a little like:
SELECT s.*
FROM students AS s
INNER JOIN event_reg AS er
ON er.drs_id = s.nsite_id
INNER JOIN events AS e
ON er.eve_id = e.id
INNER JOIN stud_reg AS grp
ON grp.dept = e.group
AND grp.coll = e.coll_id
WHERE grp.email='$email'
As with any syntax error with SQL, when running from PHP, it's best to get the query working in MySQL first, before trying to plug it in to your application.

"SELECT id FROM events WHERE `group`='".$depst."' AND coll_id='".$colls."'";

Based on the error message you got, I think it just about the single-quotes and double-quotes problem.
This is the corrected query:
$email = $_SESSION['email'];
$email = mysql_real_escape_string($email);
$depst = 'SELECT dept FROM stud_reg WHERE email="$email"';
$colls = 'SELECT coll FROM stud_reg WHERE email="$email"';
$query="SELECT * FROM stud_reg WHERE coll='$coll' AND dept='$depst'";
$evesel="SELECT id FROM events WHERE `group`='($depst)' AND coll_id='($colls)'";
$studsel="SELECT drs_id FROM event_reg WHERE eve_id='$evesel'";
$query="select * from students WHERE nsite_id='$studsel'";
$result=mysql_query($query) or die(mysql_error());
$num=mysql_num_rows($result);
You must be careful on placing quotes in the query.

try like his
$query="select * from students WHERE nsite_id='$studsel';";
And the first queries are never actually executed, and you seem to overwrite the first $query. Either make a 'join', or use subqueries , something like
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);

Why don't you try single query. The problem is quotes in your case.
$query = "SELECT * FROM students WHERE nsite_id =
( SELECT drs_id FROM event_reg WHERE eve_id = (
SELECT id FROM events WHERE
`group` = (SELECT dept FROM stud_reg WHERE email = '".$email."')
AND
coll_id = (SELECT coll FROM stud_reg WHERE email = '".$email."')
)
)";
$result=mysql_query($query) or die(mysql_error());
$num=mysql_num_rows($result);

Related

how to create double select query

<?
session_start();
$username = $_SESSION["username"];
$lecturername = "SELECT lecturername FROM lecturer WHERE username='$username'";
$sql = "SELECT * FROM publication WHERE lecturername='$lecturername'";
$records = mysql_query($sql);
?>
Try using joins like this (just an example):
SELECT * FROM lecturer t1 JOIN publication t2 ON (t1.lecturername = t2.lecturername) WHERE username='$username'
This will return data of the combined tables lecturer and publication, using username to filter the results from lecturer table, and therefore publications.

PHP / mysql join

I need to get 3 tables's values , from first I need to get aff_id where v_id = 5 , from second I need to get user id where aff_id = first's aff_id , and from third I need to get username , email , id where id = second's aff_id . I can't write correct mysql query to get data , please , help me to get it . Here is my wrong code
SELECT * FROM wp_vendor_affiliates WHERE vendor_id = 21 LEFT JOIN wp_affiliate_wp_affiliates
SELECT wp_vendor_affiliates.affiliate_id ,
wp_affiliate_wp_affiliates.user_id
FROM wp_vendor_affiliates
INNER JOIN wp_affiliate_wp_affiliates INNER JOIN
Please , hetp me , and correct my query . Thanks fot helping and for support
Use this code
$id1 = 5;
$query1 = "SELECT * FROM table1 WHERE v_id='{$id1}'";
$result1 = mysqli_query($con,$query1);
$row1 = mysqli_fetch_array($result1);
$id2 = $row1['aff_id'];
$query2 = "SELECT * FROM table2 WHERE aff_id='{$id2}'";
$result2 = mysqli_query($con,$query2);
$row2 = mysqli_fetch_array($result2);
$id3 = $row2['aff_id'];
$query3 = "SELECT * FROM table3 WHERE id='{$id3}'";
$result3 = mysqli_query($con,$query3);
$row3 = mysqli_fetch_array($result3);
$id2 = $row3['aff_id'];
Here $con is the connection to your database
$con= mysqli_connect("localhost","root","","data_base");
I used here connection to the localhost. which you'd have to change most certainly. It's a bit long but basic

Cannot store SELECT query into PHP variable

I'm trying to store a query result in order to use it in another SELECT statement but it isn't working..
$username = $_SESSION['username'];
$result = "SELECT sensorid FROM users WHERE username = '$username' ";
$result is supposed to have an integer but how can I use that variable into another select like...
$sql = "SELECT * FROM sensor WHERE sensorid = '$result'";
You need to join users table with sensor table on sensorid column.
$query = "select s.* from users u join sensor s on s.id = u.sensorid where u.username = $username"
See this

PHP - How to Use ORDER BY and GROUP BY together

I created a table that contains message, sender, to, time I want group by sender and order by time this is my code
$query= mysql_query("SELECT * FROM `table` ORDER BY `time` GROUP BY `sender`")or die(mysql_error());
while($arr = mysql_fetch_array($query)){
$num = mysql_num_rows($query);
$msg = $arr ['message'];
echo '</br>';
echo $msg;
}
that shows me this error
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 'GROUP BYsender' at line 1
So how to fix that problem?
Thanks
Klaus
Note your precedence. After the results have been grouped , it should be ordered.
"SELECT * FROM `table` GROUP BY `sender` ORDER BY `time`"
try this code
$query= mysql_query("SELECT * FROM `table` GROUP BY `sender` ORDER BY `time` ")or die(mysql_error());
// ^^--will be before order
$num = mysql_num_rows($query); // out of the while loop
while($arr = mysql_fetch_array($query)){
$msg = $arr['message'].'<br />';
// ^^--remove space here
echo $msg;
}
how to use self-joins to get the max/min/something-n rows per group.
In your situation, it can be applied to the effect you want like so:
SELECT * FROM
(SELECT group_id, MAX(`yourdate`) AS yourdate FROM tbl_name GROUP BY group_id)
AS x JOIN tbl_name USING (`group_id`,yourdate);

mysql dynamic table standard

$query = 'SELECT * FROM tbl as t WHERE t.id = 1';
in the above statement would it be wrong to do the following?
`t`.`id`
if yes then whats the correct way by the mysql standards ?
ist good in both cases:
$query = 'SELECT * FROM tbl as t WHERE t.id = 1';
$query = 'SELECT * FROM tbl as t WHERE `t`.`id` = 1';
the apostrophes are good because the column name could be the same as mysql function name like FROM so in that case to prevent error you put the column name into apostrophes

Categories