Wednesday, 27 September 2017

php - 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


Answer



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.

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...