简体   繁体   中英

PHP SQL Prepared Statement: Fatal Error Call To Member Function

I've been trying to get prepared statements working - however, I keep running into the following error

<b>Fatal error</b>:  Call to a member function bindParam() on a non-object on line <b>41</b><br />

I have copied exactly many tutorials and even the provided code did not work and threw the same error.

My code is below:

$mysqli = new mysqli(connect, username,pass, datatbase);
$name = 'Tester';
if (mysqli_connect_errno()) {
    echo "Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error();
   }
      $stmt = $mysqli->prepare("INSERT INTO Parks VALUES (null,?,?,?,?,?,?,?,?,?,?,?,?,?,Now(),?,?,?, 0, 0, 0)");
  if ($stmt === FALSE) {
      die ("Mysql Error: " . $mysqli->error);
}
    $stmt->bind_param('ssssssssssssssss', $name, $theme, $size, $mountains, $hills, $river, $lake, $island, $setofislands, $ocean, $waterfalls, $file, $image, $description, $author,$cs);

$stmt->execute();
$stmt->close();
$mysqli->close();

It's the BindParam Line causing the error.

thanks in advance :)

EDIT: Error resolved, however, no data is being inserted into the database. EDIT: Updated query, database contains VARCHARs except for Description which is LONGTEXT. The final 3 are ints/doubles and there is a current date field.

bindParam is the PDO function. You are using mysqli so try bind_param instead. Where you have 'name' should also be the type definition, so you need 's' for string.

Eg:

$stmt->bind_param('s', $name);

Edit: Although saying that, the error doesn't say the function is incorrect. It says the object doesn't exist... Running this could would give you information as to why the prepare is failing.

$stmt = $mysqli->prepare("INSERT INTO 'Parks' VALUES(null, ?");

if ($stmt === FALSE) {
    die ("Mysql Error: " . $mysqli->error);
}

Most likely the prepare is failing as the SQL is incorrect (My guess is the table name 'Parks' should NOT be in qutoes)

Edit 2: My guess for it still not working is:

$stmt->bindParam('name', $name);

Where you have 'name' should actually be the variable type, as in integer, double, string, etc. This is so the database knows what your variable is.

Try replacing that line with:

$stmt->bindParam('s', $name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM