D
D
Dinara Chanchanyan2020-04-29 01:27:46
PHP
Dinara Chanchanyan, 2020-04-29 01:27:46

Why did MySQL return an empty result?

I am learning php and trying to add rows to a table. In all variants, all actions are obtained, such as displaying lines on the screen, but actions with entering data or creating a table in the database are never obtained.
Here is a small example of how I am trying to insert a row into a table (already existing in the database):

$user = 'root';
        $password = 'root';
        $db = 'ht'; (имя бд совпадает с реальной)
        $host = 'localhost';

        $dsn = 'mysql:host='.$host.';dbname='.$db;
        $pdo = new PDO($dsn, $user, $password);

      
        $id = 1; (названия переменных совпадают с названиями колонок в таблице)
        $name = 'Dinara';
        $title = 'Кепка';

        $sql = 'INSERT INTO orders(id, name, title) VALUES(:id, :name, :title)';
        $query = $pdo->prepare($sql);
        $query->execute(['id' => $id, 'name' => $name, 'title' => $title]);

What MAMP gives in the admin panel:
MySQL returned an empty result (i.e. zero rows). (The request took 0.0005 sec.)

What could be the problem, please help?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
SagePtr, 2020-04-29
@SagePtr

Pass at least this as the 4th parameter to the PDO constructor:

$pdo = new PDO($dsn, $user, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);

You don't want to play blind kitten, do you?

G
galaxy, 2020-04-29
@galaxy

What MAMP gives in the admin panel:
MySQL returned an empty result (i.e. zero rows). (The request took 0.0005 seconds.)

Where does it show up?
The INSERT query does not return a result, if that.

D
Dinara Chanchanyan, 2020-04-29
@Dinarik_dinarik

Found my mistake!
When creating the table fields, I specified the INT type for the name and title fields. Therefore, when trying to enter something other than numbers, it gave an error that I saw only when I tried to manually enter the fields.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question