-1

I recently changed server and now certain mysqli queries are failing without notice of any kind.

$mysqli = new mysqli($h, $u, $p, $db);
if (mysqli_connect_errno()) {
    error_log("Connect failed: %s\n" .  mysqli_connect_error());
}

    if ($stmt = $mysqli->prepare("INSERT INTO users (email, 
                                            firstName,
                                            lastName, 
                                            phone) 
                                            VALUES (?, ?, ?, ?)"))
{
    $stmt->bind_param("sssi", $email, $firstName, $lastName, $phone);
    $stmt->execute();
    $stmt->close();
} else {
    error_log('[INSERT]errno: %d, error: %s', $mysqli->errno, $mysqli->error);
}

error_logs are never called (but other error_log() work fine). mysqli SELECT queries work fine. All INSERT / UPDATE queries all fail with no error.

The only change is possibly a MySQLi version change between servers.

Kez
  • 61
  • 1
  • 7
  • 1
    is it really necessary to put the query in the if condition? – Carl Binalla Mar 09 '17 at 08:32
  • Certain queries or all queries? – Progrock Mar 09 '17 at 08:42
  • For anyone encountering similar problems with behaviour changes between servers, the issue here was that mysql on the new server was set to STRICT mode so wouldn't accept INSERT without default values. – Kez Mar 09 '17 at 11:03
  • Also, although there was a hint at the answer buried in the answer to the supposed duplicate question, this is a different question with a clear concise answer. The 'duplicate' question was giving an error for starters. – Kez Mar 09 '17 at 11:10
  • I marked an answer that answered the question fully and would be concise and helpful to others. You marked this as a duplicate of another question which is mostly irrelevant. It it quite obvious this is not a duplicate. – Kez Mar 10 '17 at 08:56
  • There is not a single word in the marked answer regarding strict mode. A lie is a sin, mind you. – Your Common Sense Mar 10 '17 at 09:01
  • The question is not asking why it failed, but why there was no error reported. The marked answer explains why: because the check needs to be on 'execute', not 'prepare'. You deep seated anger issues don't help others here, keep your emotionally charged comments to yourself. – Kez Mar 11 '17 at 09:16

1 Answers1

1

Replace your query with this instead. I've removed your prepare() method from the if statement, and put the execution of it there instead.

$stmt = $mysqli->prepare("INSERT INTO users (email, firstName, lastName, phone) VALUES (?, ?, ?, ?)");
$stmt->bind_param("sssi", $email, $firstName, $lastName, $phone);

if($stmt->execute()){ // if the query successfully executes
    echo "success";
} else {
    // display error
}

This should fix your problem, and hopefully it helps you out! If you have any problems; leave a comment!

GROVER.
  • 4,071
  • 2
  • 19
  • 66
  • @YourCommonSense huh? – GROVER. Mar 09 '17 at 08:41
  • 1
    Don't post "answers" saying "give it a shot". Wild guess should go in the comments. – Your Common Sense Mar 09 '17 at 08:42
  • 1
    @YourCommonSense it's literally just a saying. Not really a valid reason to down-vote. Down-vote if the code provided is shifty or doesn't work, not if the way it's provided is not worded to your standard (unless of course it's a bad explanation). – GROVER. Mar 09 '17 at 08:47
  • Stack Overflow is not a gambling site where you *bet* an answer and see if it worked. – Your Common Sense Mar 09 '17 at 08:48
  • 1
    @YourCommonSense did I say it was? I simply provided an alternative to his code, I'm not "_betting_" on anything. – GROVER. Mar 09 '17 at 08:50
  • You *did* that. You posted a code without any explanation. – Your Common Sense Mar 09 '17 at 08:52
  • I agree that your approach could help. but technically it cannot be qualified as an answer – Your Common Sense Mar 09 '17 at 08:55
  • @YourCommonSense well then tell me I should provide an explanation, not that I shouldn't "_bet_" on my answers. and even with a down-vote? That's petty as dude. At least suggest to change it before doing so, so that I can at least learn from it. :/ Being an og StackOverflow user, you should know to do that first – GROVER. Mar 09 '17 at 08:56
  • @YourCommonSense we're not all professionals like you, are we? also, what are you basing your assumption off of? And, why are you being so childish – GROVER. Mar 09 '17 at 09:14