Server

Debian 4.9.65-3+deb9u1 (2017-12-23) x86_64 Mysql 5.7 PHP 7.2 

Table in database

 CREATE TABLE `test` ( `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `user` INT(11) NULL DEFAULT NULL, `project` INT(11) NULL DEFAULT NULL, `text` TEXT NULL COLLATE 'utf8_general_ci', PRIMARY KEY (`id`) ) COLLATE='utf8_unicode_ci' ENGINE=InnoDB AUTO_INCREMENT=2; 

PHP script

 <?php $mysqli = new mysqli('localhost','****','****','***'); $product_name = $mysqli->query("SELECT * FROM test WHERE id = 1")->fetch_object(); var_dump($product_name); $mysqli->close(); 

Server response

object(stdClass)#3 (4) { ["id"]=> string(1) "1" ["user"]=> string(1) "1" ["project"]=> string(1) "2" ["text"]=> string(10) "Текст" }

through PDO, also all in string

Ie always get a string, can I put some kind of module? I put everything from standard repositories and default settings. Timeweb hosting

  • 2
    Do you like PHP , why do you need specific types?) PS string - from mysql - this is normal behavior. - Manitikyl
  • It is more convenient to work with the necessary data types directly from the database, the same data then goes to JS, and there the string does not multiply by a string - Denis Zakharenko
  • one
    As far as I know, the cause of the problem is the <=> DB driver exchange format used: it is text, numbers, strings, dates are in plain text without specifying the type. This means additional conversion is needed, either explicitly or implicitly. - Total Pusher
  • I understood it this way, I cannot find the necessary information, how to fix it - Denis Zakharenko

1 answer 1

String does not always come, cheat! if it is NULL , then it will return the normal php-shny null . The rest is all you can bring and handles.

You can get the desired types. Easy (lying, hard and dreary):

 /** * Костыле-функция для получения PDO типа по native_type * @param string $native_type * @return int */ function getPDOType($native_type) { switch ($native_type) { case 'LONG': return PDO::PARAM_INT; // ... case 'VAR_STRING': default: return PDO::PARAM_STR; } } $pdo = db::connect(); $stmt = $pdo->prepare('SELECT `id`, `name` FROM `users` WHERE `id` = 600;'); $stmt->execute(); $col_cnt = $stmt->columnCount(); $row = []; for ($i = 0; $i < $col_cnt; $i++) { $meta = $stmt->getColumnMeta($i); $stmt->bindColumn( $i + 1, $row[$meta['name']], getPDOType($meta['native_type']) ); } $stmt->fetch(PDO::FETCH_BOUND); var_dump($row); //array(2) { // ["id"] => &int(600) // ["tema"] => &string(9) "Чебурашка" //} 

See also the example in the documentation . Description of most of the parameters PDO :: FETCH_ * is here .

But I would not use this method. On the other hand, if you write him some kind of wrapper function that will “do everything for me”, it would probably be useful for something.

  • I take some kind of hosting on timeweb, I run the same script there that resulted above and all data types come correctly. And the fact that you are proposing this is not at all the right approach - Denis Zakharenko
  • 2
    @DenisZakharenko, I apologize, but I personally consider perversion instead of analyzing the problem and finding a solution - try to permanently fasten some module. But I do not write to you about it. You asked a specific question - is it possible to get the data of the desired types. I gave a specific answer - “can” and gave an example of how to do it. But now for some reason you write "and all data types come correctly" - do you get the types you need at the output or not? Why then was your initial question? - Alexander Belinsky
  • My question was about how to make getting the data type correct from the database, as is the case with all hosting sites. In your example, I could just do $ string = (int) $ user-> id; and get int at the output - Denis Zakharenko
  • @DenisZaharenko, what is this such and such! You keep saying "how it is for everyone, how it is for everyone" - let me give you an example - where is it? I added my answer - now the type is taken by the native_type from the PDO function. I don’t think up how to improve further, I just don’t have any information (I revised the known information on PDO, and I’m going to write my own PHP module to interact with the database only in extreme old age). - Alexander Belinsky
  • I wrote above that on TimeWeb hosting, I also tried reg.ru - Denis Zakharenko