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

Z0q*_*Z0q 5 php mysql testing localhost live

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

我在这里找到了类似的问题,但我找不到答案:https:
//stackoverflow.com/questions/10507848/mysql-query-returns-only-the-first-letter-of-strings-only-当页面-是-看通

    // 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();
Run Code Online (Sandbox Code Playgroud)

这在localhost上输出,例如:

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

但在实时服务器上:

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

有任何想法吗?

编辑

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

INT(2893)

Ram*_*Ram 6

我假设您的数据库或表配置与您的localhost类似(最好仔细检查您的表).我注意到一个错误:

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();
Run Code Online (Sandbox Code Playgroud)

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

或者,您可以直接使用,因为您没有提供任何类似于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>";
Run Code Online (Sandbox Code Playgroud)