Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
CREATE TABLE `test` (`testcol` varchar(20));
INSERT INTO `test` (`testcol`) VALUES ('O\'Neill'), ('Smith');
SELECT * FROM `test`;
<?php
$query = "SELECT * FROM `test` WHERE `testcol` = ?";
$var = "O'Neill";
if ( $stmt = mysqli_prepare( $mysqli, $query ) ) {
// bind the parameters to the query
$bind = mysqli_stmt_bind_param($stmt, "s", $var );
if ( $bind === false ) {
$response['error'] = 'Bind: ' . mysqli_error( $conn );
} else {
// execute the query
if ( mysqli_stmt_execute( $stmt ) === false ) {
$response['error'] = 'Execute: ' . mysqli_stmt_error( $stmt );
}
else
{
$result = $stmt->get_result();
$row = $result->fetch_assoc();
echo $row['testcol'];
}
}
}