0

I have this php code (my server is at 5.3), it crashes on creating of multi-query.

I already tried deleting other queries, works when there is only one, crashes on the second. Also tried changing atributes in SELECT statement when I left first and second query, still didn't work.


$time = time();

$forum_update = $forum_db->prepare("SELECT @idpm := max(`id_pm`) 
                               FROM `smf_personal_messages`; 

SELECT @realname := `member_name`, @pmcount := `unread_messages` 
            FROM `smf_members` WHERE `id_member` = ? ; 

INSERT INTO `smf_personal_messages` 
(`id_pm_head`, `id_member_from`, `msgtime`, `from_name`, `subject`, `body`) 
VALUES (@idpm+1, ?, ?, @realname, '?', '?'); 

INSERT INTO `smf_pm_recipients` (id_pm, id_member, is_new) 
VALUES (LAST_INSERT_ID(), ?, 1); 

UPDATE `smf_members` SET `unread_messages` = (@pmcount + 1), `new_pm` = 1 
WHERE `id_member` = ?") 
or die("Could not create query");

$forum_update->bind_param('dddssdd', $fprofile, $fprofile, $time, $subject,
$body, $fprofile, $fprofile);
davor300
  • 11
  • 3
  • 1
    You can't execute many queries as one. You need to execute them separately in order. – Alex Apr 09 '19 at 23:55
  • Also subject to race conditions, use auto_increment columns rather than `max(`id_pm`)`. Use [INSERT ... SELECT](https://dev.mysql.com/doc/refman/8.0/en/insert-select.html) rather than the second select and following insert. Ensure your two insert statements are wrapped in a transaction, possibly with the UPDATE too. The update be `UPDATE ... unread_message = unread_messages + 1 ....` to be transactional and simplifies the insert. – danblack Apr 10 '19 at 01:22
  • Use `NOW()` instead of `$time`, unless for some reason the DB time is different from php time, or $time is needed in following code. – danblack Apr 10 '19 at 01:24

0 Answers0