Score:0

mysql not inserting through php cli

ru flag
Ben

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
vautee avatar
kr flag
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
in flag
PHP5 has been [end of life since 2018](https://www.php.net/eol.php). Use a supported version of PHP.
Ben avatar
ru flag
Ben
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.
Score:0
ru flag
Ben

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

vidarlo avatar
ar flag
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.
Score:0
ua flag
  • Do not use the mysql_* interface in PHP. Do use mysqli_* instead. (Or use PDO.)

  • Do get the error message and display it.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.