This question is originates from an earlier question .
When i run this script on mysql shell (see below)
mysql> SELECT -> t1.post_id, -> 'multiple' AS multiple, -> COUNT(*) as count -> FROM people_publication AS t1 GROUP BY t1.post_id -> UNION -> SELECT -> t2.post_id, -> REPLACE( -> CONCAT( -> 'multiple_', -> @curRow:=CASE -> WHEN @postId = t2.post_id THEN @curRow + 1 -> ELSE 0 -> END, -> @postId:=t2.post_id -> ), -> t2.post_id, -> '' -> ) AS multiple, -> t2.pid -> FROM people_publication AS t2 -> ORDER BY post_id, multiple; +---------+------------+-------+ | post_id | multiple | count | +---------+------------+-------+ | 7209 | multiple | 3 | | 7209 | multiple_0 | 27 | | 7209 | multiple_1 | 34 | | 7209 | multiple_2 | 1 | | 7210 | multiple | 3 | | 7210 | multiple_0 | 51 | | 7210 | multiple_1 | 11 | | 7210 | multiple_2 | 37 | | 7211 | multiple | 1 | | 7211 | multiple_0 | 36 | | 7212 | multiple | 1 | | 7212 | multiple_0 | 11 | | 7213 | multiple | 1 | | 7213 | multiple_0 | 15 | | 7215 | multiple | 1 | | 7215 | multiple_0 | 42 | +---------+------------+-------+ 16 rows in set (0.00 sec)
$query = "SELECT t1.post_id, 'multiple' AS multiple, COUNT(*) as count FROM people_publication AS t1 GROUP BY t1.post_id UNION SELECT t2.post_id, REPLACE( CONCAT( 'multiple_', @curRow:=CASE WHEN @postId = t2.post_id THEN @curRow + 1 ELSE 0 END, @postId:=t2.post_id ), t2.post_id, '' ) AS multiple, t2.pid FROM people_publication AS t2 ORDER BY post_id, multiple"; $sql=$con->prepare($query); $sql->execute(); $sql->setFetchMode(PDO::FETCH_ASSOC); while($row=$sql->fetch()){ echo "<pre>";print_r($row); }
+---------+---------+------------+------------+ | meta_id | post_id | meta_key | meta_value | +---------+---------+------------+------------+ | 816 | 7209 | multiple | 3 | | 817 | 7209 | multiple_0 | 34 | | 818 | 7209 | multiple_0 | 1 | | 819 | 7209 | multiple_0 | 27 | | 820 | 7210 | multiple | 3 | | 821 | 7210 | multiple_0 | 37 | | 822 | 7210 | multiple_0 | 51 | | 823 | 7210 | multiple_0 | 11 | | 824 | 7211 | multiple | 1 | | 825 | 7211 | multiple_0 | 36 | | 826 | 7212 | multiple | 1 | | 827 | 7212 | multiple_0 | 11 | | 828 | 7213 | multiple | 1 | | 829 | 7213 | multiple_0 | 15 | | 830 | 7215 | multiple | 1 | | 831 | 7215 | multiple_0 | 42 | +---------+---------+------------+------------+
The connection file looks like this
<?php //session_start(); function dbConnect() { $host = 'localhost'; $dbname = 'mydb'; $user = 'root'; $pass = ''; try { $con= new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); return $con; $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo 'Cannot connect to database'; exit(); } }?>
Please check the mysql connection class in your php code
one more thing to check that on localhost mysql connects using sockets while on server it uses port. It could cause an issue if they are to different instance
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.