0

I have a simple php script which is ran from cli. I have tried many different variations of mysql code, but regardless, the line isnt inserted in the database even though php says it is....

<?php
$link = mysql_connect('localhost', '***', '***');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('***');

mysql_query("INSERT INTO email_log (to, from, subject, headers, message, source) VALUES ('1','2','3','4','5','6')");
printf("Last inserted record has id %d\n", mysql_insert_id());
?>

php test.php returns Last inserted record has id 0 and the same scripts work flawlessly through a web browser.

Things I have tried:

  • I have changed file ownership to user that may effect it.
  • 4-5 different INSERT codes
Ben
  • 23
  • 4
  • 1
    Add some more information to your question. For example the output of DESCRIBE email_log Also fetch any errors mysql_query could raise, see example #1 in https://www.php.net/manual/en/function.mysql-query.php – vautee Jan 17 '22 at 08:47
  • 1
    PHP5 has been [end of life since 2018](https://www.php.net/eol.php). Use a supported version of PHP. – Gerald Schneider Jan 17 '22 at 08:56
  • Hey Gerald, thanks for your reply... I have switched to another installed ver 7.0. Same issue... Nothing reported in error.log. Did echo to email the line to be inserted and emailed ok... A conflict between cli and mysql?? I have run yest scripts, it seams to connect, It just completely ignores the actual insert. – Ben Jan 17 '22 at 09:46

2 Answers2

0

WOW... how unusual...No error messages, but I did a lot more digging and trouble shooting to find out, that "TO", "FROM" and "SOURCE" row names are on a MYSQL restricted/reserved list.

https://dev.mysql.com/doc/refman/8.0/en/keywords.html

Ben
  • 23
  • 4
  • The lesson? Don't expect mysql_query to work. Verify that it works by checking if it returns an error. Assumptions is the mother of all fuckups. – vidarlo Jan 17 '22 at 12:26
0
  • Do not use the mysql_* interface in PHP. Do use mysqli_* instead. (Or use PDO.)

  • Do get the error message and display it.

Rick James
  • 2,463
  • 1
  • 6
  • 13