2016-03-08 79 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

相關問題