inserting a mysql query in codeigniter - php

$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.

Related

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 > ?

Getting values from different tables in mysql query (practical example)

I have two tables: first and second
this and that are primary keys, common and always present on both tables, so I guess there is no need of left joins
$query = "SELECT
first.one,first.going,first.what,first.ever,second.another,second.outre,second.oneplus,second.more,second.anotherthing,second.alldifferent
WHERE second.THIS = first.THAT AND first.is = '1' AND
first.yet = '$variable' AND second.againe = '1'";
The error is the following
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 second.THIS = first.THAT AND first.is = '1' AND first.y' at line 1
But I can't get to understand why this happens.
Any help on this one? Ty very much
You need to specify a table name.
$query = "SELECT first.one, ... ,second.alldifferent WHERE ...";
Should be
$query = "SELECT first.one, ... ,second.alldifferent FROM first, second WHERE ...";

mysql displaying data from multiple tables

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

MySql Syntax error - 42000

I'm getting this error :
Warning: PDO::query() [pdo.query]: 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 'When, RDV, Comments FROM distributions WHERE IDFond = 1' at line 1 in /Applications/XAMPP/xamppfiles/htdocs/JG/DistributionManager.class.php on line 56
When executing this code :
$Distribution_Manager->getListByFunds($Selected_Fond->id());
foreach ($Distribution_Manager as $Distrib)
{
echo $Distrib->Comments();
}
Here is the concerned function :
public function getListByFunds($FundID)
{
$Distribution = array();
$q = $this->_db->query('SELECT id, IDClient, IDFond, Who, When, RDV, Comments FROM distributions WHERE IDFond = '.$FundID);
while ($donnees = $q->fetch(PDO::FETCH_ASSOC))
{
$Distribution[] = new Distribution($donnees);
}
return $Distribution;
}
Should be a little mistake but I'm lagging on it for almost 50 minutes !
Thanks in advance for the help ;)
WHEN is a mysql reserved word, so try using a different column name or enclose WHEN in backquotes.
When is mysql keyword, try this
SELECT id, IDClient, IDFond, Who, When AS anything..
or enclose this keyword to backquotes
SELECT id, IDClient, IDFond, Who, `When`, RDV..
Give back quotes for column names like id and try it
You're never closing your quotes on your statement.

Mysql query for using count on a view in php

I have a query:
$result = mysql_query("CREATE VIEW temporary(IngList) AS (
SELECT DISTINCT (r1.Ingredient)
FROM recipes r1,
recipes r2
WHERE r1.Country = '$temp'
AND r2.Country = '$temp2'
AND r1.Ingredient = r2.Ingredient)
SELECT COUNT(*) FROM temporary");
I want the query to make a view called temporary and have it return a count of the number of rows in the view temporary. I know this code works without the SELECT COUNT(*) because I checked my database and the view is created.
Yet this code throws the 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 'SELECT COUNT(*) FROM temporary' at line 1
I checked the syntax and it seems to be correct. What seems to be the problem because its quite frustrating.
From the mysql_query documentation:
mysql_query() sends a unique query (multiple queries are not supported)...
You can't create the view, and select from it in a single mysql_query. The view is unnecessary:
$sql = sprintf("SELECT COUNT(DISTINCT r1.Ingredient)
FROM recipes r1
WHERE r.country = '%s'
AND EXISTS(SELECT NULL
FROM recipes r2
WHERE r2.Country = '%s'
AND r1.Ingredient = r2.Ingredient)",
$temp, $temp2);
$result = mysql_query($sql);
For starters you have two statements. What you're writing looks more like a stored procedure. Even if it worked, you would need a semicolon at the end of the first statement. And another statement somewhere saying "DROP VIEW ...." when you are done.
And a temp view is a bit of a non sequitur. I can't find any reference to "CREATE VIEW temporary". Or maybe it's to create a view named temporary with an argument? Views don't take arguments.
I think you might get what you want with a semi-simple SQL statement something like:
$result = mysql_query(
"SELECT COUNT(DISTINCT r1.Ingredient)
FROM recipes r1
JOIN recipes r2 ON r1.Ingredient = r2.Ingredient
WHERE r1.Country = '$temp'
AND r2.Country = '$temp2'");

Categories