user1846761
user1846761

Reputation: 61

Can you use more than one query with mysqli->prepare on one db connection?

I want to use a single database connection with multiple queries but use prepare and bind_param. How can i do this? I cant find it in the documentation.

Edit: i want two completely different queries.

$db = getConnection();
$query = "INSERT INTO talks(title, body, topic) VALUES(?, ?, ?)";
$stmt = $db->prepare($query);
$stmt->bind_param('sss', $title , $body, $topic);
$stmt->execute();
$stmt->close();

$query = "SELECT * WHERE title=?";
$stmt = $db->prepare($query);
$stmt->bind_param("s", $title);
$stmt->execute();
$stmt->bind_result($i, $t, $b, $to);
$stmt->fetch();
$id = $i;
$stmt->close();

Its telling me that $stmt isnt an object on the second go around

Upvotes: 1

Views: 503

Answers (1)

Madara's Ghost
Madara's Ghost

Reputation: 175017

Just prepare a second query, as you did with the first.

$conn = new mysqli(....);
$stmt = $conn->prepare(....);
//Do stuff with $stmt

$stmt = $conn->prepare(...different...); //$stmt is overridden with the new query.
//Do stuff with the new $stmt.

Upvotes: 2

Related Questions