PDO fetch returns nothing [duplicate] - php

This question already has answers here:
PHP PDOException: "SQLSTATE[HY093]: Invalid parameter number"
(4 answers)
Closed 8 years ago.
I've encountered a little problem.
I have the following code:
$query = $db->prepare('(SELECT last_visit, last_ip FROM user_log WHERE user_id = :id)
UNION
(SELECT time AS last_visit, packet_hex AS last_ip FROM crack_log WHERE target_id = :id)
ORDER BY last_visit DESC LIMIT 0,10;');
$query->execute(array(':id'=> $_SESSION['id']));
$log = $query->fetchAll(PDO::FETCH_ASSOC); //Last visit/IP
var_dump($log);
Which returns:
array(0) { }
I've tried the query in phpmyadmin and it worked fine. Can you please help me find the error?

Accorrding to the documentation
You cannot use a named parameter marker of the same name twice in a
prepared statement.
You cannot bind multiple values to a single named parameter in, for
example, the IN() clause of an SQL statement.
In you case, you should use something like
$query = $db->prepare('(SELECT last_visit,
last_ip
FROM user_log
WHERE user_id = :id_1
)
UNION
(SELECT time AS last_visit,
packet_hex AS last_ip
FROM crack_log
WHERE target_id = :id_2
)
ORDER BY last_visit DESC LIMIT 0,10;'
);
$query->execute(array(':id_1'=> $_SESSION['id'],
':id_2'=> $_SESSION['id']
)
);

Related

MySQL GROUP BY is not working in PDO prepared statement [duplicate]

This question already has answers here:
Why does this PDO statement silently fail?
(2 answers)
Closed 1 year ago.
I have two statements: one is written with mysql_query, another with PDO:
mysql_query:
$sql = $mysql_query("SELECT `user_report`.* FROM `user_report` LEFT JOIN `user` ON
`user_report`.`user_id` = `user`.`user_id` WHERE `user`.`userBlocked` = 0 GROUP BY
`user_id` ORDER BY `reports` DESC, `user_report`.`timestamp` ASC");
PDO prepared:
$sql = $pdo->prepare("SELECT `user_report`.* FROM `user_report` LEFT JOIN `user` ON
`user_report`.`user_id` = `user`.`user_id` WHERE `user`.`userBlocked` = 0 GROUP BY
`user_id` ORDER BY `reports` DESC, `user_report`.`timestamp` ASC");
$sql->execute();
PDO doesn't work with GROUP BY user_id.
Reason I need it is if two results come with the same user_id I want to show it as one.
How is it possible that PDO doesn't allow GROUP BY function ... in MySQL statement is there any substitute to that?
Thank you.
Try this
$sql = $pdo->prepare("SELECT user_report.* FROM `user_report` LEFT JOIN `user` ON
user_report.user_id = user.user_id WHERE user.userBlocked = 0 GROUP BY
user.user_id DESC, user_report.timestamp ASC");
$sql->execute();
changes i did `` removing on selecting tables and
GROUP BY is pointing to which can be (user.user_id or user_report.user_id) you need to confirm?
AND ORDER BY (reports) is pointing to ? so i removed ?
You try the above sql, and provide your output if there is any error.

sum(value) gives undefined index [duplicate]

This question already has answers here:
Get sum of MySQL column in PHP
(9 answers)
Closed 1 year ago.
I'm having trouble calculating the sum of one column in the database from the first 5 rows. My code is:
$mysqli=mysqli_connect($host,$user,$password,$database);
$sql = "SELECT SUM(medie) FROM (SELECT medie FROM stelewar WHERE nume='".$nume."' ORDER BY id DESC LIMIT 5) medie";
$result=mysqli_query($mysqli,$sql);
$row=mysqli_fetch_assoc($result);
$sumamedie= $row["medie"];
The last row of code throws the error "Undefined index: medie" and I cannot figure out why...
Any thoughts?
$mysqli=mysqli_connect($host,$user,$password,$database);
$sql ="(SELECT SUM(media) AS mysumvalue FROM stelewar WHERE nume='".$nume."' ORDER BY id DESC LIMIT 5)";
$result=mysqli_query($mysqli,$sql);
while($row=mysqli_fetch_array($result))
{
$sumamedie= $row["mysumvalue"];
}

How to get total amount of rows while using limit when using PDO prepared statements? [duplicate]

This question already has an answer here:
How to get the total found rows without considering the limit in PDO?
(1 answer)
Closed 7 years ago.
I found some similar ones, but not the exact one that I need:
Do the work but not using PDO
Using PDO but not using LIMIT
Background information:
I am using MySQL with PDO class
Currently, I am using two queries as follows:
To get one page of data:
$sql = "SELECT * FROM `tab`
WHERE `condition` = :condition
LIMIT $page_size OFFSET $offset";
$array = array('condition'=>$condition);
$mysql = $pdo->prepare($sql);
$mysql->execute($array);
To get total amount of rows:
$sql = "SELECT COUNT(*) FROM `tab`
WHERE `condition` = :condition";
$array = array('condition'=>$condition);
$mysql = $pdo->prepare($sql);
$mysql->execute($array);
You can use the SQL_CALC_FOUND_ROWS command to tell MySQL to return the total of the matching records
$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM `tab`
WHERE `condition` = :condition
LIMIT $page_size OFFSET $offset";
To get the total rows found you can run this query
SELECT FOUND_ROWS()
However, it is often faster to execute 2 separate queries than using SQL_CALC_FOUND_ROWS. Here is a benchmark to explain
you can read more about this here

Invalid use of group function with MAX [duplicate]

This question already has answers here:
How do I select an entire row which has the largest ID in the table?
(6 answers)
Closed 9 years ago.
My code gives me the following error:
Invalid use of group function
$query = mysql_query("SELECT `text` FROM `text` WHERE `id`=max(id)");
if(!$query)
die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['text'];
}
Where is my mistake?
If you want the row with highest id you could use:
SELECT text FROM text ORDER BY id DESC LIMIT 1
WHERE clauses affect individual rows, whereas HAVING clauses affect aggregations (results of GROUP BY clauses). Row criteria must be limited to the WHERE clause, aggregate functions (like MAX) must be used in HAVING clauses.
You could do this:
SELECT *
FROM text
WHERE id = (SELECT MAX(id) FROM text);

Is this an efficient way to create a post stream, or is there a better way? [duplicate]

This question already has answers here:
PHP - Using PDO with IN clause array
(9 answers)
Closed 6 years ago.
$FSQL = $pdo->query('SELECT * FROM `connections` WHERE `uid`="'.$my_id.'" && `type`="1" ORDER by `id` DESC');
$myfriends = '`uid`="'.$my_id.'" ';
while($po = $FSQL->fetch(PDO::FETCH_ASSOC)){
$myfriends .= ' || `uid`="'.$po['cid'].'"';
}
$dsk = $pdo->query("SELECT * FROM `posts` WHERE ".$myfriends." ORDER by `id` DESC LIMIT ".$limitCount);
I have been trying to create a nice post stream, and I finally got my code down. But it seems so inefficient if you have a large amount of connections (connections being anything from friends, pages, or events).
Could someone tell me if there is a better way to do this?
--by the way: this is working perfectly already, but I feel like i'll run into issues down the line
$FSQL = $pdo->query('SELECT * FROMconnectionsWHEREuid="'.$my_id.'" &&type="1" ORDER byidDESC');
This is vulnerable to SQL Injection. You should be using parameters and prepared statements. See the Documentation.
Worked Example
$sql = $pdo->prepare('SELECT * FROM `table` WHERE `uid`=:uid');
// Create the SQL statement, with the parameter prefixed by a ":".
$userID = "username";
// Grab the value you wish to bind to your parameter.
$sql->bindParam(':uid', $userID);
// Bind the values, using the bindParam method.
$sql->execute();
// Execute the statement with the parameters bound to the SQL query.
You don't want to use a subquery?
Something like this...
$dsk = $pdo->query(
"SELECT *
FROM `posts`
WHERE uid IN (
SELECT cid
FROM `connections`
WHERE `uid`="'.$my_id.'" && `type`="1"
)
ORDER BY `id` DESC LIMIT " . $limitCount);
And try not to use * when you don't need all fields.

Categories