mysql dynamic table standard - php

$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

Related

Getting an my Sql Error

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);

How to use php variables in mysql query while php variable contains mysql query?

How can I implement something like this in mysql?
$query1 = "SELECT id FROM table WHERE username = 'John'";
$query2 = "SELECT id FROM table WHERE username= 'Parsa'";
$query = "SELECT * FROM table WHERE id BETWEEN $query1 AND $query2";
$result = mysql_query($query) or die('Query faild'.mysql_error());
$myrecord = mysql_fetch_assoc($result);
Try this
$query1 ="SELECT GROUP_CONCAT(id) FROM table WHERE firstname in('John','Parsa')";
$query = "SELECT * FROM table WHERE id IN ($query1)";
you have two identical queries , you could just have one . and use IN , not BETWEEN.
You can put those 3 queries in to one query:
$query = "SELECT * FROM table WHERE id
BETWEEN
( SELECT id FROM table WHERE firstname = 'John' GROUP BY id )
AND
( SELECT id FROM table WHERE firstname = 'Parsa' GROUP BY id )
";
although your query doesn't mean anything; you need "()" for subqueries to work.
$query1 = "(SELECT id FROM table WHERE username = 'John')";
$query2 = "(SELECT id FROM table WHERE username= 'Parsa')";
$query = "SELECT * FROM table WHERE id BETWEEN $query1 AND $query2";
u can use a subselection:
SELECT * FROM table WHERE id BETWEEN ($query1) AND ($query2)
But be careful: The Subselection result must be an Integer.

Perform a query with MySQL and PHP

How can I do something like this:
$query = "SELECT a,b FROM c ORDER BY a";
$query1 = "SELECT a,b FROM '".$query."' WHERE a='".$number."'";
Thank you very much
REAL CASE
$query2 = "SELECT numero,spartenza,sarrivo,opartenza,oarrivo FROM treni ORDER BY opartenza";
$query1 = "SELECT spartenza,sarrivo,opartenza,oarrivo,TIMEDIFF(oarrivo,opartenza) FROM (".$query2.") AS 'ordinata' WHERE numero = '".$id_treno."' ORDER BY opartenza";
Wrap it in parenthesis:
$query1 = "SELECT a,b FROM (".$query.") AS `alias` WHERE a='".$number."'";
Subqueries like this need to be aliased.
MySQl Subquery Documentation
REAL CASE
$query = "SELECT spartenza,sarrivo,opartenza,oarrivo,TIMEDIFF(oarrivo,opartenza) FROM treni WHERE numero = '".$id_treno."' ORDER BY opartenza";
You do not need a subquery at all for this. You can ORDER BY a column that you aren't selecting. One suggestion though would be to alias your TIMEDIFF function like this sothat it will be easier to retrieve.
$query = "SELECT spartenza,sarrivo,opartenza,oarrivo,TIMEDIFF(oarrivo,opartenza) AS `timediff_alias` FROM treni WHERE numero = '".$id_treno."' ORDER BY opartenza";

PHP variables in WHERE clause, how to?

I have following PHP script. I want to count and print comments for each article.
The id for each article can be "recalled" by this: <?php echo $listing['Listing']['listing_id'];?> (this return the contentid number)
Now, I have this script:
<?php
$db =& JFactory::getDBO();
$query = "SELECT COUNT(comments) AS totalcount WHERE contentid = ????? ";
$db->setQuery($query);
$count = $db->loadResult();
echo ($count); ?>
I tried to add in WHERE clause this:
"... WHERE contentid = {$listing['Listing']['listing_id']}"
but $count returns "0" zero.
How can I add this variable in the WHERE clause?
Thanks in advance!
In the case of an integer:
$query = "SELECT
COUNT(comments) AS totalcount
WHERE
contentid = " . ((int) $listing['Listing']['listing_id']);
In the case of a string:
$query = "SELECT
COUNT(comments) AS totalcount
WHERE
contentid = " . mysql_real_escape_string($listing['Listing']['listing_id']);
The biggest thing to be weary of is SQL injection. This makes your queries safe. The explicit cast to int will ensure an int value is passed, even if the value is erroneous, at least you wont be open to any attack.
Use sprintf and escape the string.
$query = sprintf("SELECT COUNT(comments) AS totalcount WHERE contentid = '%s'",mysql_real_escape_string($listing['Listing']['listing_id']));
try
$query = "SELECT COUNT(comments) AS totalcount WHERE contentid = '".mysql_real_escape_string($listing['Listing']['listing_id'])."'";
or
$query = "SELECT COUNT(comments) AS totalcount WHERE contentid = ".mysql_real_escape_string($listing['Listing']['listing_id']);
depending on the data type.

How to select with a binary field ? (php,mysql)

Try to select use "where" clause in a mysql statement:
e.g.
Table: X with a ID column which is BINARY data type. Then save in a variable in php
$aid = $row["id"];
How do i use this variable later when I try to select from table
$where = "where `ID` = '$aid'";
$query = "SELECT * FROM X ".$where;
Return 0 row.
Does anyone know why?
Answering my own question.
Just figured out:
$where = "where HEX(ID) = 'bin2hex($aid)'";
$query = "SELECT * FROM X ".$where;
Does anyone know better solution?
Try below :
add BINARY in where clause.
$where = "where BINARY ID = '$aid'";
$query = "SELECT * FROM X ".$where;

Categories