Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 802
Z0q Avatar asked Mar 08 '16 18:03

Z0q


People also ask

Which characters are not allowed in MySQL?

ASCII NUL (U+0000) and supplementary characters (U+10000 and higher) are not permitted in quoted or unquoted identifiers. Identifiers may begin with a digit but unless quoted may not consist solely of digits. Database, table, and column names cannot end with space characters.

How do I change the first character of a string in MySQL?

Use the MySQL REPLACE() function to replace a substring (i.e. words, a character, etc.) with another substring and return the changed string.

How do I Outfile in MySQL?

You can also use SELECT ... INTO OUTFILE with a VALUES statement to write values directly into a file. An example is shown here: SELECT * FROM (VALUES ROW(1,2,3),ROW(4,5,6),ROW(7,8,9)) AS t INTO OUTFILE '/tmp/select-values.


1 Answers

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>";
like image 55
Ram Avatar answered Sep 19 '22 00:09

Ram