简体   繁体   English

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

[英]Get output from database on server, but not on php script

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. 此代码的用法:我想通过AJAX进行操作,每次我单击“进一步”按钮时仅5条记录。 I´m not so far yet.... 我还没走呢....

This script just have to get me 5 records from a specific user. 该脚本只需要从特定用户那里获得5条记录即可。

The Problem: 问题:

So the sql queries run without any problems on the Database. 因此,SQL查询在数据库上运行没有任何问题。 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. 但是,当我想在PHP脚本中执行此操作时,仅从第一个查询中获取counter(),第二个查询从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

ok, done! OK完成! 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 :) 因此,将bindParam(用于整数)或bindValue(用于字符串)添加到代码中,这样会很顺利:)

Have seen that on PHP.net and with debugDumpParams() on PDOStatement you can see, how your Query is working with the Params you setted. 在PHP.net上以及在PDOStatement上的debugDumpParams()已经看到了,您可以看到查询如何与您设置的参数一起使用。

Here the manual http://php.net/manual/de/pdostatement.debugdumpparams.php 这里的手册http://php.net/manual/de/pdostatement.debugdumpparams.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM