I need to use a prepare statement to get an array of results, and my experience till now is all in procedural way. This is the code where I need a hand:
Before this I have a require statement with a connection established to the db, and with a $_POST method from a form I get two variables value that are $town and $type.
$stmt = mysqli_smt_init($conn);
if (mysqli_stmt_prepare($stmt, "SELECT town, type, code FROM tab_01 WHERE town = ? AND type LIKE ?");
mysqli_stmt_bind_param($stmt, "ss", $town, $type);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$townvalue[] = $row["town"];
$typevalue[] = $row["type"];
}
}
mysqli_stmt_close($stmt);
It doesn't run, of course, since in the "if" and in the "while" statements my syntax comes from non prepared statements and it's surely wrong.
I need to echo the rows of the result (I mean the three columns of the result).
In a non prepared statement I'd write like this:
$sql = "SELECT town, type, code FROM tab_01 WHERE town='$town' AND type LIKE '$type'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo $row["town"];
echo $row["type"];
echo $row["code"];
}
}
mysqli_free_result($result);
mysqli_close($conn);
What's the correct syntax to get the same in a prepared way? Thank you.