doulian4762
2017-07-07 13:16
浏览 34
已采纳

从服务器上的数据库获取输出,但不在PHP脚本上获取

I have an very nasty issue....

My usage for this code: I wanna get through AJAX, just 5 records everytime i click on the further-button. I´m not so far yet....

This script just have to get me 5 records from a specific user.

The Problem:

So the sql queries run without any problems on the Database. But when i wanna do it in PHP-script, i get ONLY from the first query the counter() AND the second query gives me an empty array from the PDO-Statement.

CODE:

if (empty($_SESSION['counter']) || ($_SESSION['counter'] < 0)) {

      $sql = 'SELECT count(record.record_id) AS counter FROM record 
              LEFT JOIN recordbook ON record.record_id = recordbook.record 
              WHERE user = ?';

              $sth = $this->dbc->prepare($sql);
              $sth->execute(array($u_id['user_id']));
              $count = $sth->fetch();

              $_SESSION['counter'] = $count['counter'];
              var_dump($_SESSION);
          }
          $_SESSION['counter'] = $_SESSION['counter'] - 5;

          $sql = 'SELECT record.record_id, status, place, record.record AS records, comment, recorddate 
                  FROM record LEFT JOIN recordbook ON record.record_id = recordbook.record 
                  WHERE user = ? ORDER BY record.record_id DESC LIMIT ?,5';
          $sth = $this->dbc->prepare($sql);  
          $sth->execute(array($u_id['user_id'],
                                $_SESSION['counter']));

          $output = $sth->fetchAll();
}

HOPE someone know how to solve this...

table structure

Field | Type | Null | Key | Default | Extra
record_id | int(10) unsigned | NO | PRI NULL | auto_increment
status | varchar(200) | YES | NULL | 
place | varchar(200) | YES | NULL 
record | longtext | YES NULL 
comment | longtext | YES | NULL 
recorddate | timestamp | YES | CURRENT_TIMESTAMP 
attachment_id | int(10) unsigned | YES | MUL | NULL
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douganggu4392 2017-07-08 10:47
    已采纳

    ok, done! No error....

    need to bind the variables to the query. Seems so, that only with prepare and execute, is it not clear enough. So add bindParam(for integer) or bindValue(for Strings) to code, than it goes nice and smoothly :)

    Have seen that on PHP.net and with debugDumpParams() on PDOStatement you can see, how your Query is working with the Params you setted.

    Here the manual http://php.net/manual/de/pdostatement.debugdumpparams.php

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题