According to PHP documentation, the following is true about the query(INSERT) statement:

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

However, when I run the following version of the statement:

$insert = CS50::query("INSERT INTO portfolios (users_id, symbol, shares) VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE shares = shares + VALUES(shares)", $_SESSION["id"], strtoupper($stock["symbol"]), $_POST["shares"]);

and then var_dump($insert), I get an int (1 or 2), instead of TRUE or FALSE. This makes error-checking impossible.

Does anyone have any idea as to why that is happenning? Everything works fine, it's just the return value that is weird.


You are not calling PHP mysql_query(). You are calling CS50::query. From the pset specification/instructions:

If query is instead passed a DELETE, INSERT, or UPDATE statement, it will return a non-negative integer that represents the number of rows deleted, inserted, or updated, respectively.......... Note our use of IGNORE, which ensures that this statement will return 0 if username already exists

  • Oh wow. That's incredibly stupid of me. But thanks! – iCode95 Aug 21 '16 at 16:14
  • No such thing as "stupid" when you're in "school"! :) – DinoCoderSaurus Aug 21 '16 at 16:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .