3

I know when MySQL returned data, it is returned as strings, no metter of the MySQL data type.

So why when i run this prepared statement i get a integer ?

    $id = '1';

    sql = "SELECT * FROM
            USER
            WHERE user_id = ? ";

    $stmt = $this->conn->prepare($sql);
    $stmt->bind_param('i', $id);
    $stmt->execute();

    $user = $stmt->get_result();

    return ($user->num_rows == 1) ? $user : NULL ;


    if ($user) {

    while($row = $result->fetch_assoc()) {
        $data = array();
        $data["user_id"]      = $row["user_id"];

    return $data; 

When i run php gettype($data['user_id']) i get an integer.

I thought that datatype will always be converted to a string.

8
  • The 'i' is telling the mysqli binding function that this database column is an integer and $id should be treated as such when the parameter is replaced by this value. It is not describing what the contents of $id are Commented Feb 19, 2017 at 9:28
  • the contents of the variable $id = '1' Commented Feb 19, 2017 at 9:32
  • ??? Who said that it always returns a string ??? Commented Feb 19, 2017 at 9:33
  • @RiggsFolly So if i replace 'i' to 's' the query will work? beacuse the id column in database are integer. Commented Feb 19, 2017 at 9:35
  • @JordanH look at this questions link Commented Feb 19, 2017 at 9:37

1 Answer 1

2

It does not always return as string. Maybe you are using PHP version upper than 5.3( the version of PHP 5.3 is compiled with mysqlnd (and not old libmysql)), uses mysqlnd as the native driver and the native driver returns integer types appropriately. Please check which version you are currently using. For more details you can go through this link. http://blog.ulf-wendel.de/2008/pdo_mysqlnd-the-new-features-of-pdo_mysql/

Sign up to request clarification or add additional context in comments.

2 Comments

This is break my app, when i use the mysqli non prepared statement i always get a string, and now when i moved to prepared statement i always get a integer no metter if i passed $stmt->bind_param('s', $id); $_SESSION['id'] === $_GET['id'] not ture.
even now it is still breaking, sometimes is string, sometimes is integer, it seems to be a bug?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.