mysql displaying data from multiple tables - php

I want show field status , jumlah, harga, and total.
this is my structure of table
order {id_order, id_user, status}
order_detail {id_order,jumlah, harga, total}
this is my query function:
function shopstat($user_id) {
return $this->db->query("SELECT * from order_detail left join order on order_detail.id_order=order.id_order where order.id_user=$user_id");
}
but I have an error syntax :
Error Number: 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 'order on order_detail.id_order=order.id_order where order.id_user=16' at line 1

ORDER is a reserved word, for ORDER BY. You must wrap it in backticks. I also used aliases o and od so you don't have to make even more backticks. More characters = more likelihood for a typo, but that part is up to you.
$query = "SELECT * from order_detail od
LEFT JOIN `order` o ON od.id_order=o.id_order
WHERE o.id_user=$user_id";
return $this->db->query($query);
The complete list of reserved words are here:
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html ( From Fouad )

order is a reserved word in MySQL. See : http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

"Order" is a reserved word you can't use it as identifier. Use something else instead

Related

getting specific columns from three tables

I need to get specific columns from three tables through joins.every time it goes wrong.my code is
$saf=mysqli_query($db , "select pod.mobile, tpaidamount.Aptdate, follow.cent, pdate, time from pod,tpaidamount, follow where tpaidamount.pid = follow.pid and pod.Customer Id = tpaidamount.pid and pod.Customer Id =follow.pid ");
$i=1;
while($sfg=mysqli_fetch_assoc($saf) or die(mysqli_error($db)))
;
?>
pod,tpaidamount,follow are tables and other coloumns
Getting 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 'Id = tpaidamount.pid and pod.Customer Id =follow.pid' at line 1
is it a typo? Is setfollowup.pid or follow.pid?
select pod.mobile, tpaidamount.Aptdate, follow.cent, <table>.pdate, <table>.time
from pod, tpaidamount, follow
where tpaidamount.pid = follow.pid
and pod.`Customer Id` = tpaidamount.pid
and pod.`Customer Id` = follow.pid
You shouldn't Ever create columns names with spaces. The name: "pod.Customer Id" is a Bad attribute name, and you need to avoid use names like datatypes (or any SGBD reserved word) like: 'date', 'time', 'char', 'table', 'column'....
However, if you need to do it, try this SQL:
SELECT p.mobile
, t.Aptdate
, f.cent
, ???.pdate
, ???.`time`
FROM pod AS p
JOIN tpaidamount AS t ON o.`Customer Id` = t.pid
JOIN follow AS f ON t.pid = f.pid ON p.`Customer Id` = f.pid
Use alias for easy coding SQL queryes. Ex: table_name AS tn, table_a_name AS tan.
!!! I sugest you to watch some basic SQL Lessons.
Good Luck.

Symfony DQL syntax error while selecting last and next record

I want to get next and previous record in my database, this is my first time using DQL and I don't really understand why it doesn't work.
Here's how my custom query look like:
$em = $this->getDoctrine()->getManager();
$rsm = new ResultSetMapping();
$rsm->addScalarResult('id', 'id');
$query = $em->createNativeQuery(
'SELECT id
FROM SITEArticlesBundle:Article
WHERE
id = (SELECT id
FROM SITEArticlesBundle:Article
WHERE ? > 2 LIMIT 1)
OR
id = (SELECT id
FROM SITEArticlesBundle:Article
WHERE ? < 2 LIMIT 1)', $rsm);
$query->setParameter(1, $id);
$query->setParameter(2, $id);
$nextAndPrevNews = $query->execute();
I want to get array of 2 id, like that:
[
['id' => 1]
['id' => 3]
]
But I get a SQL syntax error
SQLSTATE[42000]: Syntax error or access violation: 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 ':Article
WHERE
id = (SELECT id
FROM ' at line 2
I would be really grateful if someone could help me.
Thanks
One issue is the colon character in the table name. That character isn't allowed, unless the table name is escaped.
I suspect that SITEArticlesBundle:Article isn't the actual identifier for the MySQL table. In a native query, you need to specify the actual name of the MySQL table.
If that is your tablename, then you can enclose it in backtick characters:
FROM `SITEArticlesBundle:Article`
I'm not sure why you'd be comparing the value of $id with a literal 2, and not comparing that to the value of the id column in the table. I don't get it.
Unless you are trying to pass in the column name as a bind parameter, which won't work. You can only pass in values to a prepared SQL statement, you can't pass identifiers or keywords or other SQL constructs in as bind parameters.
Personally, I'd avoid the OR condition and the subqueries, and just use a UNION ALL operation.
SELECT MAX(p.id) AS id
FROM `SITEArticlesBundle:Article` p
WHERE p.id < ?
UNION ALL
SELECT MIN(n.id) AS id
FROM `SITEArticlesBundle:Article` n
WHERE n.id > ?

inserting a mysql query in codeigniter

$query = $this->db->query('select
sum(like) as atotal
from
like
where
sfid = '.$short);<br>
print_r($query);
and the error is
Error Number: 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 'like) as atotal from like where sfid = 11' at line 2
select sum(like) as atotal from like where sfid = 11
Filename: C:\wamp\www\don\system\database\DB_driver.php
Line Number: 330
'like' in sum(like) is column name and 'like' after 'from' is table name
thank you in advance
$data = array(
"category_id"=>$this->input->post('category'),
"name"=>$this->input->post('name'),
"description"=>$this->input->post('descri')
);
$this->db->insert('measurement', $data);
$inserted_id = $this->db->insert_id();
like is a MySQL reserved word
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
which needs to be wrapped in backticks.
Either do that, or rename your table/column to something else.
Using reserved words is discouraged to be used. You can use them, but require special attention.
I suggest you rename your table to likes and your column to another word.
If renaming them isn't an option, then modify your code to read as this:
$query = $this->db->query('select
sum(`like`) as atotal
from
`like`
where
sfid = '.$short);
print_r($query);
You shouldn't use like keyword in your query.
It is sql-specific word treated as special keyword. To solve this task, you should change column name in database table or wrap like keyword in backticks either:
$query = $this->db->query('select sum(`like`) as atotal from `like` where sfid = '.$short);
You can still use it. Just use `like` instead of like OR change the table name. The backQuote helps to override the MySql keyword override.

Get Average value of from one table for a specific row in another table php

I am trying to do following.
Select a Hospital with id = hid from table named as hospital.
Add a value "overall_rating" to it and get all the ratings and make avg of it from another table named as hrating
here is my query
$statement = $conn->prepare('SELECT hospital.*,(SELECT AVG(hrating.rating_h) FROM hrating WHERE hid = hospital.hid) as overall_rating WHERE hid=:hid LIMIT 1');
Getting this error
{"error":"SQLSTATE[42000]: Syntax error or access violation: 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 'WHERE hid='44' LIMIT 1' at line 1"}
Where am i being wrong.
It appears you don't have a " FROM hospital " bit in your query?
SELECT hospital.*,(SELECT AVG(hrating.rating_h)
FROM hrating
WHERE hid = hospital.hid) as overall_rating
FROM hospital -- this line seems to be missing ??
WHERE hid=:hid LIMIT 1
Try this:
SELECT hospital.*, temp.overall_rating
FROM hospital
LEFT JOIN (SELECT hid AVG(rating_h) as overall_rating
FROM hrating group by hid
) temp
on hid = hospital.hid
WHERE hospital.hid=:hid LIMIT 1

How to get all alphabetic letters in codeigniter from DB? PHP, MySQL, CodeIgniter

I'm using this to get all letters to create alphabetic list:
SELECT DISTINCT SUBSTRING(title, 1, 1) AS letter FROM games ORDER BY letter asc
Everything OK. But then I try to pass it to codeigniter, it fails (tryed) several variants:
$this->db->distinct();
$this->db->select("SUBSTRING(title,1,1) AS letter");
$this->db->order_by("letter", "asc");
$query = $this->db->get('games');
also this one:
$this->db->query("SELECT DISTINCT SUBSTRING(title,1,1) AS letter ORDER BY letter");
$query = $this->db->get('games');
All the time I'm getting:
Error Number: 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 'AS letter FROM (`games`) ORDER BY `letter` asc' at line 1
SELECT DISTINCT SUBSTRING(title, `1`, `1)` AS letter FROM (`games`) ORDER BY `letter` asc
Filename: E:\wamp\www\gamecenter\system\database\DB_driver.php
Line Number: 330
What can I try to resolve this?
Pass a second parameter to the select with FALSE
$this->db->select("SUBSTRING(title,1,1) AS letter", FALSE); //This way it will not put the ` characters.
$result = $this->db->query('SELECT DISTINCT SUBSTRING(title,1,1) AS letter FROM (games) ORDER BY letter asc')->result();
I think its help you.

Categories