0

I'm having trouble getting this php script working with mysqli. I'm using Apache server, PHP 5.5, mysqli. The connection works, and I have other statements working properly (not with bind though).

// NOTE: servername, username, password defined but not shown

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    // database connection error
    echo '1';
    $conn->close();
    exit();
} 
$sqlQuery = "INSERT INTO 'fishingusers' SET 'deviceID' = ?, 'nickname' = ?,     'valid_on' = 'NOW()', 'valid_until' = 'null'";

echo ' '. $sqlQuery . '';

$deviceID = $conn->real_escape_string($_GET['deviceID']);
$nickname = $conn->real_escape_string($_GET['nickname']);

$stmt = $conn->prepare($sqlQuery);
$stmt->bind_param('ss', $deviceIDValue, $nicknameValue);

$deviceIDValue = $deviceID;
$nicknameValue = $nickname;

if($stmt->execute()) {
    // insert success
    echo '0';
}
else {
    // insert failed
    echo '3';
}

$conn->close();

I am receiving this error:

Fatal error: Call to a member function bind_param() on a non-object in XXXX on line 27

Line 27 is the $stmt->bind_param(). Sorry, I'm very new to PHP... this seems pretty simple.

jszobody
  • 28,495
  • 6
  • 61
  • 72
  • Did you select a database? The constructor only has 3 arguments, and there is no `$conn->select_db()`. Also, the reason why the `$stmt` isn't an object, is because you've used single-quotes around your table and column names - which makes the query *fail*. This should be backticks instead, and `NULL`-values and `NOW()` shouldn't be quoted either. – Qirel Jun 17 '16 at 21:59
  • a little [error reporting](http://php.net/manual/en/mysqli-stmt.error.php) will go a long way to helping you find these things `if (!($stmt = $conn->prepare($sqlQuery))) trigger_error("Error: Prepare failed: (".$conn->errno.") ".$conn->error, E_USER_ERROR);` – Jeff Puckett Jun 17 '16 at 22:03
  • @Qirel No I didn't. I'm new to databases in general. I have a table under a schema in MySql but I don't think I named a database during creation? Thanks for the quick reply. – Jason Howerslitz Jun 17 '16 at 22:04
  • Also very helpful: http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – Jeff Puckett Jun 17 '16 at 22:05
  • You should fix the query like I commented (with the backticks instead of single-quotes), and then you need to select a database. Those are just the mistakes I spotted, there might be more. Do that, test it and see if it works. If it doesn't, come back and update your question with the relevant code and we'll have another look ;-) @JasonHowerslitz – Qirel Jun 17 '16 at 22:05
  • Backticks (didn't realize they were different from single quotes) and selecting the database did the trick, thanks! New to stack overflow, so hopefully I'm responding here correctly. – Jason Howerslitz Jun 17 '16 at 22:18
  • Glad it worked out for you! And welcome to SO! – Qirel Jun 17 '16 at 22:23

0 Answers0