2016-03-08 85 views
4

我不知道到底发生了什么,但是当我上传我的网站时,所有列中只有第一个字符被返回。它在本地机器上工作得很好。MySQL只返回字段的第一个字符,但在本地工作正常

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

+0

是您的模式在两个系统上一样吗?运行这个查询直接产生什么? – tadman

+0

@tadman是的,它是。我实际上连接到相同的外部托管数据库。当在MySQL Workbench中直接运行查询时,它会产生预期的结果。 – Z0q

+0

我猜你正在使用'mysqli',也许你考虑从你的localhost和你的web服务器的php版本,以及你的数据类型。 – yul757

回答

6

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

1.您在致电​​之前拨打了store_result()。根据http://php.net/manual/en/mysqli-stmt.store-result.php​​应该先被调用。

见我的代码,这可能会解决你的问题:

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

谢谢!问题解决了。这是我的答案:'你在调用execute()'之前调用了store_result()。 P.S .:我连接到本地主机上和我的实时网站上的完全相同的数据库。它连接到一个外部托管的数据库。 – Z0q

+0

赏金在哪里? :) – Ram

+0

我需要等13小时才能给它。但我一定会把它给你:) – Z0q

0

正如你链接的问题,建议,请尝试代码而不unset($stmt)$stmt = db->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(); 
+0

它不能解决问题。 – Z0q

相关问题