Answer the question
In order to leave comments, you need to log in
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]);
Answer the question
In order to leave comments, you need to log in
Pass at least this as the 4th parameter to the PDO constructor:
$pdo = new PDO($dsn, $user, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
What MAMP gives in the admin panel:
MySQL returned an empty result (i.e. zero rows). (The request took 0.0005 seconds.)
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 questionAsk a Question
731 491 924 answers to any question