【问题标题】:MySQL returns only the first character of the fields, but works fine on localMySQL 仅返回字段的第一个字符,但在本地工作正常
【发布时间】:2016-06-22 20:28:31
【问题描述】:

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

我在这里找到了一个类似的问题,但我没有找到答案:
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();

例如在 localhost 上输出:

字符串(5)“你好”字符串(3)“牛”

但在实时服务器上:

字符串(1)“H”字符串(1)“C”

有什么想法吗?

编辑

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

int(2893)

【问题讨论】:

  • 您的架构在两个系统上是否相同?直接运行这个查询会产生什么?
  • @tadman 是的。我实际上正在连接到相同的外部托管数据库。当直接在 MySQL Workbench 中运行查询时,它会产生预期的结果。
  • 我猜你正在使用mysqli,也许你会考虑从本地主机到网络服务器的 php 版本,以及你的数据类型。
  • 我之前也有类似的问题。然后我发现在我的本地主机中,我的 sql 属性被定义为 varchar(100),但实时服务器中的相同属性被错误地定义为 varchar(1)。能否请您检查实时服务器中数据库上的属性类型?
  • 您是否尝试过不按照您找到的问题评论中的建议拨打unset($stmt);stackoverflow.com/questions/10507848/…

标签: php mysql testing localhost live


【解决方案1】:

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

1.您在调用execute() 之前调用了store_result()。按照http://php.net/manual/en/mysqli-stmt.store-result.phpexecute()应该先调用。

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

    /* 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>";

【讨论】:

  • 谢谢!问题解决了。这就是我的答案:You called store_result() before calling execute()。 P.S.:我在 localhost 和我的实时网站上连接到完全相同的数据库。它连接到外部托管的数据库。
  • 赏金在哪里? :)
  • 我需要等待 13 个小时才能给它。但我一定会把它给你:)
【解决方案2】:

根据您链接的问题中的建议,尝试不使用unset($stmt)$stmt = db-&gt;stmt_init() 的代码,而您可以使用free_result()

// Log Table Query
$stmt->free_result(); //Free old results first
$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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 2019-01-13
    相关资源
    最近更新 更多