$query = 'SELECT COUNT(*) FROM Security WHERE ips=$currentip';
How do i get this to count the $currentip values that i make the variable equal to.
assuming you're using the mysqli function set:
$mysqli=new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$query = 'SELECT COUNT(*) as num FROM Security WHERE ips=$currentip';
$response=$mysqli->query($query);
$row=$response->fetch_assoc();
$count=$row['num'];
It would be preferable to clean $currentip. Or even better to use a prepared statement.
Related
I am working on a query on Doctrine 2 (with Symfony 2.8)
I have this query giving me relevant info:
$qb = $this->createQueryBuilder('ea');
$qb->join('ea.entity_b', 'eb')
->join('ea.entity_c', 'ec')
->join('ec.entity_d', 'ed')
->join('MainBundle:Entity_E', 'ee','WITH', 'ee.column1 = ea.id')
->join('MainBundle:Entity_F', 'ef', 'WITH', 'ea.column1 = ef.id');
Now, I need to add extra info to that query, but it comes from a native SQL, something like this:
SELECT * FROM DS ORDER BY id DESC LIMIT 1
And make sure that the id of the result from the native query is equal to ef.id
I hope I made any sense.
Thanks
Do you know DQL ?
Similar to SQL but adapted for Doctrine.
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html
You can then do queries like :
<?php
$query = $em->createQuery("SELECT u FROM User u JOIN u.address a WHERE a.city = 'Berlin'");
$users = $query->getResult();
I need some help about iterate over result in doctrine. This is my code for query:
$consulta = $em->createQuery('
SELECT bcr, SUM(bcr.volumenAlmacenado) as vol, SUM(bcr.pesoAlmacenado) as pes
FROM ResiduoBundle:BodegaContieneResiduo bcr
WHERE bcr.fechaIngreso BETWEEN :fechaP AND :fechaA AND bcr.fechaRetiro IS NULL
GROUP BY bcr.idResiduo
');
$consulta->setParameter('fechaA', $fechaActual);
$consulta->setParameter('fechaP', $fechaPasada);
return $consulta->getResult();
When I run in mysql return without problem. in symfony also get results. Now, when I tried to loop in twig I can't do, I think could be for my agregate functions in my query. I hope you can get me a clue or something about this. Grettings
You should added extension of mysql function SUM then you can use SUM function in doctrine query
another way you can direct execute your query or native query of doctrine
Try following query you can get result
$consulta = $em->executeQuery('SELECT bcr, SUM(bcr.volumenAlmacenado) as vol, SUM(bcr.pesoAlmacenado) as pes FROM ResiduoBundle:BodegaContieneResiduo bcr WHERE bcr.fechaIngreso BETWEEN :fechaP AND :fechaA AND bcr.fechaRetiro IS NULL GROUP BY bcr.idResiduo'),
array('fechaA'=> $fechaActual, 'fechaP' => $fechaPasada));
return $consulta->fetchAll();
You can also use native query of doctrine
use Doctrine\ORM\Query\ResultSetMapping;
$rsm = new ResultSetMapping();
$rsm->addEntityResult('ResiduoBundle:BodegaContieneResiduo', 'bcr');
$rsm->addFieldResult('bcr', 'idResiduo', 'idResiduo');
$rsm->addFieldResult('bcr', 'volumenAlmacenado', 'vol');
$rsm->addFieldResult('bcr', 'pesoAlmacenado', 'pes');
$sqlQuery = "SELECT bcr.idResiduo as idResiduo, SUM(bcr.volumenAlmacenado) as vol, SUM(bcr.pesoAlmacenado) as pes
FROM ResiduoBundle:BodegaContieneResiduo bcr
WHERE bcr.fechaIngreso BETWEEN :fechaP AND :fechaA
AND bcr.fechaRetiro IS NULL
GROUP BY bcr.idResiduo";
$query = $em->createNativeQuery($sqlQuery, $rsm)
->setParameter('fechaA', $fechaActual);
->setParameter('fechaP', $fechaPasada);
$query->getResult();
I am trying to build a query that retrieves all the most recent and upcoming activities from database.
The entity activity has a field named date of type DateTime. So in my repository I was thinking of building something like this:
$query = $repository
->createQueryBuilder('a');
$query->orderBy( 'DATEDIFF( a.date, NOW())' , 'ASC');
$query->setMaxResults( 6 );
return $query;
Unfortunately I get the following error:
[Syntax Error] line 0, col 59: Error: Expected end of string, got '('
The Dql that is generated by my query:
SELECT a FROM MyBundle\Entity\Activity a ORDER BY DATEDIFF( a.date, NOW()) ASC
I also tried installing beberlei/DoctrineExtensions, but either it is not working or I was unable to configure it correctly.
Anyone has any suggestion?
Thanks in advance
date_diff si already implemented as Doctrine DQL statement as described here
for use as ordering statement I suggest you to use the HIDDEN select keyword as explained in this article
So your DQL is like this:
SELECT
a,
DATE_DIFF( a.date, CURRENT_TIMESTAMP() ) AS HIDDEN score
FROM MyBundle:Entity a
ORDER BY score
And add the max result on the query. Let me know if you need help to adapt as query builder statement
Hope this help
Why don't you just use
$query = $repository
->createQueryBuilder('a');
$query->orderBy( 'DATEDIFF( a.date, CURRENT_TIMESTAMP())' , 'ASC');
$query->setMaxResults( 6 );
return $query;
?
I would like to select everything + MAX value and receive only rows having max values.
$query = $this->createQueryBuilder('s');
$query->where('s.challenge = :challenge')->setParameter('challenge', $challenge);
$query->groupBy('s.score');
$query->getQuery();
return $query->select('s.*, MAX(s.score) AS max_score')->getQuery()->getResult();
How could I achieve this in doctrine? I am getting an error that * property is not found. I have tried to select them all one by one but no luck either.
Goal is to achieve something like this
SELECT user, challenge, whateverelse, MAX(score) FROM users_scores_table GROUP BY user_id
Please help ;)
It's too late, but I write this for the records.
You can use "as HIDDEN" in SELECT statements to remove a field of the final result, this way you can use it for ordering or grouping without modifying result fields.
In your example:
$query = $this->createQueryBuilder('s');
$query->select('s, MAX(s.score) AS HIDDEN max_score');
$query->where('s.challenge = :challenge')->setParameter('challenge', $challenge);
$query->groupBy('s.user');
$query->setMaxResults($limit);
$query->orderBy('max_score', 'DESC');
Here is a final working query
$query = $this->createQueryBuilder('s');
$query->select('s, MAX(s.score) AS max_score');
$query->where('s.challenge = :challenge')->setParameter('challenge', $challenge);
$query->groupBy('s.user');
$query->setMaxResults($limit);
$query->orderBy('max_score', 'DESC');
return $query->getQuery()->getResult();
If I query the database using this code, I get an array of 4 objects as expected.
global $wpdb;
$rows = $wpdb->get_results("SELECT * FROM ppm_playlists");
var_dump($rows); die();
But if I query using this, I get an empty array.
global $wpdb;
$rows = $wpdb->get_results("SELECT * FROM ppm_playlists ORDER BY sort-order ASC");
var_dump($rows); die();
Is there a "trick" to using "ORDER BY" in the database class that I am missing in the documentation?
Thanks in advance.
Replace sort-order ASC to sort_order ASC
When having problems like this it helps to put the problematic query in the phpMyAdmin to locate the problem.
The reason the query failed is because sort-order is interpreted as sort - order (subtracting the column named order from the column named sort). If you wish to keep the hyphen in your column name, you would have to wrap the column in backticks:
SELECT * FROM ppm_playlists ORDER BY `sort-order` ASC;
Note, however, that using hyphens in column names is not recommended.