php - MySQL 只返回字段的第一个字符,但在本地工作正常

标签 php mysql testing localhost live

我不知道到底发生了什么,但是当我上传我的网站时,我的所有专栏都只返回第一个字符。它在本地机器上运行良好。

我在这里发现了一个类似的问题,但我没能找到答案:
https://stackoverflow.com/questions/10507848/mysql-query-returns-only-the-first-letter-of-strings-only-when-page-is-viewed-on

    // Log Table Query
    unset($stmt);
    $stmt = $db->stmt_init();
    $stmt = $db->prepare( "SELECT * FROM logs ORDER BY `id` DESC" );

    $stmt->store_result();
    $stmt->bind_result($r_id, $r_time, $r_logger, $r_message, $r_category);
    $stmt->execute();

    while( $stmt->fetch() )
    {
        var_dump($r_message);
        var_dump($r_category);
    }

    $stmt->close();

例如在本地主机上输出:

string(5) "Hello"String(3) "Cow"

但是在实时服务器上:

string(1) "H"String(1) "C"

有什么想法吗?

编辑

我认为这仅适用于字符串类型。整数类型返回例如:

int(2893)

最佳答案

我假设您的数据库或表配置类似于您的本地主机(最好仔细检查您的表)。 我注意到一个错误:

1。你打给了store_result()打电话前execute() .根据 http://php.net/manual/en/mysqli-stmt.store-result.php execute()应该先调用。

查看我的代码,这可能会解决您的问题:

    /* unsetting doesn't matter you're
    going to overwrite it anyway */
    unset($stmt);

    /* you dont need to initialize $stmt with $db->stmt_init(),
    $db->prepare() method will create it for you */
    $stmt = $db->stmt_init();
    $stmt = $db->prepare("SELECT * FROM logs ORDER BY `id` DESC");

    /* execute the query first before storing
    the result and binding it your variables */
    if (!$stmt->execute()) {
        echo "query execution error";
        exit();
    }

    /* store the result */
    $stmt->store_result();

    /* then bind your variables */
    $stmt->bind_result($r_id, $r_time, $r_logger, $r_message, $r_category);

    /* fetch data and display */
    while($stmt->fetch()) {
        var_dump($r_message);
        var_dump($r_category);
    }

    $stmt->close();

如果这解决了您的问题,请告诉我。

或者,您可以使用直接的方式,因为您没有提供任何输入参数,例如 WHERE first_name LIKE <input here>您的查询:

    $result = $db->query("SELECT * FROM logs ORDER BY `id` DESC");

    if ($result === false) {
        echo "query execution error";
        exit();
    }

    /* You can use either MYSQLI_NUM or MYSQLI_ASSOC os MYSQLI_BOTH
    see php.net for more info */
    echo "<pre>";
    while($line = $result->fetch_array(MYSQLI_NUM)) {
        print_r($line);
        echo "\n";
    }
    echo "</pre>";

关于php - MySQL 只返回字段的第一个字符,但在本地工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35875079/

相关文章:

来自两个表的 MySQL 评级

python - 在 Python 中查找/测试未修饰的字符串文字(没有 b"或 u")

testing - 我可以访问测试用户的提要(自己的帖子)吗?

php - 为什么我在 Codeigniter 上收到错误 'Call to undefined function delete_cookie ()'?

php - 在自定义函数中从 WooCommerce 动态获取优惠券代码

php - 产品表连接库存

php - 如何在 PHP 中向现有数组添加新的键值

javascript - 对 HTML 进行编码并将其从 PHP 传递到 Javascript

Java运行Ms Access查询

testing - Symfony2 演示测试问题