Perform a query with MySQL and PHP - 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";

Related

alternative sql syntax for joined table update

I need alternative syntax for my sql, to deduct the qty from another table to my raw material table
$sql3 = "UPDATE material_inventory
join product_check
ON material_inventory.qty = material_inventory.qty
SET material_inventory.qty = material_inventory.qty - product_check.qty
WHERE product_check.pc_id = '$id'
AND product_check.date2 = '$date'
LIMIT 1";
$result3 = mysqli_query($conn,$sql3);
It looks like limit doesn't work in join update with join clause.
anyway thanks
Looks like your ON condition is wrong. Until you add schema of relevant tables, all I can suggest from given scenario is following query:
$sql3 = "UPDATE material_inventory
join product_check
ON material_inventory.qty = product_check.qty //modified line
SET material_inventory.qty = material_inventory.qty - product_check.qty
WHERE product_check.pc_id = '$id'
AND product_check.date2 = '$date'
LIMIT 1";
$result3 = mysqli_query($conn,$sql3);
If the above query doesn't get the desired output, please add schema so that I can provide more accurate solution.

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.

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;

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