PHP PDO - Using MySQL Variables

ash

I'm trying to run a query in PHP using PDO. The query has some variables at the top to determine a rank, except the when using the SET @var in the $sql, it returns an empty rowset. If I remove the offending SQL however, it returns fine.

I don't want to return @prev_value, @rank_count or @rank_increasing in my script, only the rank it creates in the SELECT.

Can you let me know what I am doing wrong please?

Thanks

    $sql = "
    SET @prev_value = NULL;
    SET @rank_count = 0;
    SET @rank_increasing = 0;
    SELECT a.*
         , @rank_increasing := @rank_increasing + 1 AS row_num
         , CASE
           WHEN @prev_value = score 
              THEN @rank_count
           WHEN @prev_value := score 
              THEN @rank_count := @rank_increasing
           END AS rank
      FROM ( 
           -- INLINE VIEW --
           ) a
    ";
    try {
        $sth = $dbh->prepare($sql);
        $sth->execute(array($var1, $var2));
        return $sth->fetchAll(PDO::FETCH_ASSOC);
    } catch (Exception $e) {
        return $e;
    }
ash

Found the solution here: https://stackoverflow.com/a/4685040/1266457

Thank you :)

To fix:

// Prepare and execute the variables first
$sql = "
SET @prev_value = NULL;
SET @rank_count = 0;
SET @rank_increasing = 0;
";
$sth = $dbh->prepare($sql);
$sth->execute();

// Run the main query
$sql = "
SELECT a.*
     , @rank_increasing := @rank_increasing + 1 AS row_num
     , CASE
       WHEN @prev_value = score 
          THEN @rank_count
       WHEN @prev_value := score 
          THEN @rank_count := @rank_increasing
       END AS rank
  FROM ( 
       -- INLINE VIEW --
       ) a
"; ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using MySQL IN() with PHP and PDO

From Dev

pagination using php pdo mysql

From Dev

PHP docker and using MySQL PDO

From Dev

PDO Fetch setting variables mysql php

From Dev

Using PHP Variables in MySQL

From Dev

Creating a MYSQL Trigger using PDO / PHP

From Dev

How to notice MySQL errors using PDO in PHP

From Dev

Inserting Data to MySQL using PDO for PHP Xcode

From Dev

PDO Insert operation using PHP and MYSQL not working

From Dev

PHP Upload image to mysql database using PDO?

From Dev

PDO UPDATE array using php mysql

From Dev

Creating a secure website using PHP, PDO and MySQL

From Dev

Send SMS to mysql contact using php pdo

From Dev

Update one row in MySQL using php PDO

From Dev

MySQL Variables and PDO

From Dev

PDO/PHP Undefined variables

From Dev

PHP PDO and Mysql

From Dev

How to connect to MySQL using PHP PDO in easyphp 16.1.1

From Dev

insert into using same row(same primary key) mysql php pdo

From Dev

Refactoring code in OOP PHP and using PDO for mysql queries

From Dev

Send Android Push Notifications to Multiple Devices using PHP, PDO, MYSQL

From Dev

Retrieve img from mysql database using PDO PHP

From Dev

PHP PDO error when using placeholders in the LIMIT clause of a MySQL query

From Dev

Fetching corrupted data from MySQL DB using PDO in php

From Dev

php mysql join query on same table using pdo

From Dev

Perform a JOIN between SQLite and MySQL using PDO in PHP?

From Dev

Query works as expected direct in MySQL or using mysqli in PHP, but not in PDO

From Dev

PDO MYSQL Prepared Update Statement using PHP Not executing

From Dev

Fetching corrupted data from MySQL DB using PDO in php

Related Related

HotTag

Archive