Search code examples
phpmysqltestinglocalhostlive

MySQL returns only the first character of the fields, but works fine on local


I don't know what is going on exactly, but all only the first character is returned for all my columns when I uploaded my website. It works perfectly fine on a local machine.

I found a similar question here, but I didn't manage to find the answer:
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();

This outputs on localhost for example:

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

But on the live server:

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

Any ideas?

Edit

I think that this applies only to string types. The integer types return for example:

int(2893)


Solution

  • I'm assuming that your database or table config is similar to your localhost (better to double check your table). I noticed one mistake:

    1. You called store_result() before calling execute(). As per http://php.net/manual/en/mysqli-stmt.store-result.php execute() should be called first.

    See my code this might solve your problem:

        /* 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();
    

    Let me know if this solved your problem.

    Alternatively, you can use the straight forward way since you're not giving any input parameter like WHERE first_name LIKE <input here> to your query:

        $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>";