kuki
kuki

Reputation: 65

Type definition string doesn't match number of bind

I have an error in php and I have no idea how to fix this. By the way this is my school lesson example so I don't really know what's happening there. This is supporsed to be master/detail navigation.

<?php

$mysqli = new mysqli("localhost", "root", "", "base");

$stmt = $mysqli->prepare('SELECT * FROM aeromiting WHERE id = ":id"');
echo $mysqli->error;
$stmt->bind_param(':id', $_GET['id']);
var_dump($stmt);

$data = $stmt->execute();

?>

Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in C:\xampp\htdocs\test1\detail.php on line 20 object(mysqli_stmt)#2 (10) { ["affected_rows"]=> int(0) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(0) ["field_count"]=> int(4) ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["sqlstate"]=> string(5) "00000" ["id"]=> int(1) }

Upvotes: 1

Views: 107

Answers (2)

Kuya
Kuya

Reputation: 7310

If you want to do it in PDO, try this...

<?php
$host = 'localhost'; $db = 'base'; $user = 'root'; $pw = '';
$conn = new PDO('mysql:host='.$host.';dbname='.$db.';charset=utf8', $user,     $pw);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

try {
    $id = $_GET['id'];
    $sql = "SELECT * FROM aeromiting WHERE id=:id";
    $query = $conn->prepare($sql);
    $query->bindValue(':id', $id, PDO::PARAM_INT);
    $query->execute();
    $row = $query->fetch(PDO::FETCH_ASSOC);
    $totalRows = $query->rowCount();
} catch (PDOException $e) {
die("Could not get the data: " . $e->getMessage());
}
?>

Have a look at pdo_mysql for more information.

Upvotes: 1

Qirel
Qirel

Reputation: 26450

You're mixing APIs here. MySQLi can't accept strings or the likes as parameters.

$stmt = $mysqli->prepare('SELECT * FROM aeromiting WHERE id = ":id"');
$stmt->bind_param(':id', $_GET['id']);

Should be

$stmt = $mysqli->prepare('SELECT * FROM aeromiting WHERE id = ?');
$stmt->bind_param('i', $_GET['id']);

This is of course assumed that $_GET['id'] is an integer (hence the 'i' in the bind_param. If it's a string, replace the i with s.

You should probably get a bind_result as well, so you actually bind the results from the database to some variables. Take a look at the MySQLi documentation.

Upvotes: 0

Related Questions