0

I have a table and i am reading n% of records from it. For that i have prepared a query:

SET @rows := (SELECT COUNT(*)*0.5 FROM trending);
PREPARE STMT FROM 'SELECT * FROM trending ORDER BY Count LIMIT ?';
EXECUTE  STMT USING @rows;

The output I get is:

SET @rows := (SELECT COUNT(*)*0.5 FROM trending);# MySQL returned an empty result set (i.e. zero rows). PREPARE STMT FROM 'SELECT * FROM trending ORDER BY Count LIMIT ?';# MySQL returned an empty result set (i.e. zero rows). EXECUTE STMT USING @rows;# Rows: 8

How can i fetch the records from the table?

Drew
  • 24,851
  • 10
  • 43
  • 78
Jai Saxena
  • 255
  • 1
  • 2
  • 15

1 Answers1

-1
SET @rows := (SELECT COUNT(*)*0.5 FROM trending);
PREPARE STMT FROM 'SELECT * FROM trending order by somecolumn LIMIT ?';
EXECUTE  STMT USING @rows;

works fine on my system. odd number of rows gets you a round-up

Edit:

create table thing2
(   id int auto_increment primary key,
    theWhat varchar(40) not null,
    `count` int not null
);

Insert 5 rows:

insert thing2(theWhat,`count`) values ('anchovies',6),('tomato',1),('cat',99),('mouse',8),('spoon',70);

SET @rows := (SELECT COUNT(*)*0.5 FROM thing2);
PREPARE STMT FROM 'SELECT * FROM thing2 LIMIT ?';
EXECUTE  STMT USING @rows;


count might need to be in backticks (not on my system), if that is actually a column and you are doing
 the following :

SET @rows := (SELECT COUNT(*)*0.5 FROM thing2);
PREPARE STMT FROM 'SELECT * FROM thing2 order by `count` LIMIT ?';
EXECUTE  STMT USING @rows;
+----+-----------+-------+
| id | theWhat   | count |
+----+-----------+-------+
|  2 | tomato    |     1 |
|  1 | anchovies |     6 |
|  4 | mouse     |     8 |
+----+-----------+-------+

SET @rows := (SELECT COUNT(*)*0.5 FROM thing2);
PREPARE STMT FROM 'SELECT * FROM thing2 order by theWhat LIMIT ?';
EXECUTE  STMT USING @rows;
+----+-----------+-------+
| id | theWhat   | count |
+----+-----------+-------+
|  1 | anchovies |     6 |
|  3 | cat       |    99 |
|  4 | mouse     |     8 |
+----+-----------+-------+

Revision1 (PHP shown)

This revision is due to the op mentioning PHP. So this shows a multi-query, using the result set given from a particular line of that query. Shows the use of an @ variable that clearly retains its value. And a heredoc variable $theSql being set for the multi-query itself.

<?php
    error_reporting(E_ALL);
    //mysqli_report(MYSQLI_REPORT_ALL);
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    ini_set('display_errors', 1);

    try {
        $mysqli= new mysqli('localhost', 'dbusername', 'thePassword', 'theDbname'); // tweak accordingly
        if ($mysqli->connect_error) {
            die('Connect Error (' . $mysqli->connect_errno . ') '
                . $mysqli->connect_error);
        }
        echo "I am connected and feel happy.<br/>";

        // Note the 3rd statment below (the EXECUTE) and the code below it that cares about output for the 3rd one only

$theSql = <<<SQL
    SET @rows := (SELECT COUNT(*)*0.5 FROM thing2); 
    PREPARE stmt123 FROM 'SELECT * FROM thing2 LIMIT ?'; 
    EXECUTE stmt123 USING @rows; 
    DEALLOCATE PREPARE stmt123;
SQL;
        $shouldDebug=false; // change to true to see more debug info (such as the skipped multi-query results)
        $theCounter=0;
        $weCareAbout=3; // the 3rd line of the command: "EXECUTE stmt123 USING @rows; "
        if ($mysqli->multi_query($theSql)) {
            do { // Note this loop poached from http://php.net/manual/en/mysqli.multi-query.php
                if ($shouldDebug) echo "1a.<br/>";
                $theCounter++;
                // store first result set
                if ($result = $mysqli->store_result()) {
                    if ($shouldDebug)  echo "1b.<br/>";
                    if ($theCounter==$weCareAbout) {
                        while ($row = $result->fetch_row()) {
                            echo $row[0]." ".$row[1]." ".$row[2]."<br>";
                        }
                    }
                    if ($shouldDebug)  echo "1c.<br/>";
                    $result->free();
                    if ($shouldDebug)  echo "1d.<br/>";
                }
                // print divider 
                if ($mysqli->more_results() && $shouldDebug) {
                    printf("-----------------\n"); 
                }
                if ($shouldDebug) "1e.<br/>";
            } while ($mysqli->next_result() && $mysqli->more_results());
            // above line to avoid error: Strict Standards: mysqli::next_result(): There is no next result set. ...
            // ...Please, call mysqli_more_results()/mysqli::more_results() to check whether to call ...
            //
            // the Manual page is not exactly clear on this. In fact, faulty.
            // http://php.net/manual/en/mysqli.multi-query.php
        }

        $mysqli->close();   // just showing it, what the heck
    } catch (mysqli_sql_exception $e) { 
        throw $e; 
    } 
?>

Browser output:

I am connected and feel happy.
1 anchovies 6
2 tomato 1
3 cat 99

The long and short of that above do/while is that we are in that loop 4 times based on the query passed.

Please see the Manual Page mysqli.multi-query and mysqli.store-result.

Community
  • 1
  • 1
Drew
  • 24,851
  • 10
  • 43
  • 78
  • i know i'm not getting any errors, but all it does is EXECUTE the statement. how do i get the records? how do i display the result? – Jai Saxena Aug 21 '15 at 16:55
  • well so far we are only talking about mysql. You have a programming language you are using to make the call? And have a resultset object, such as a C# resultset, or PHP rows you can iterate ? – Drew Aug 21 '15 at 17:02
  • yes i have a php code, but since the statement isn't returning any records, my php code is receiving null. check out my output, that's all i get – Jai Saxena Aug 21 '15 at 17:52
  • i guess if it was wrapped in a stored proc this wouldn't be a problem, as your query is not stand-alone. – Drew Aug 21 '15 at 21:25
  • I assumed this was in a stored proc. @JaiSaxena, if this is not in a stored proc, why not build the query string in code? – Uueerdo Aug 24 '15 at 21:59
  • @Uueerdo because LIMIT is not taking variable as it is, but only when i am binding it – Jai Saxena Aug 25 '15 at 15:47
  • this can be made into a prepared statement (building any string like @uueerdo said) ... I will give an example – Drew Aug 25 '15 at 17:38