PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
SELECT * FROM tmp;
Copy Clear
Copy Format Clear
<?php $db = $mysqli; // this service provides a predefined $mysqli variable while we are using $db for the connection #Create a temporary table //$db->query("CREATE TABLE tmp(id int auto_increment primary key, name varchar(90), email varchar(90))"); # populate it with sample data $sql = "INSERT INTO tmp (name,email) VALUES (?,?)"; $stmt = prepared_query($db, $sql, ["Sam", "sam@gmail.com"]); echo "Affected rows: $stmt->affected_rows\n"; echo "Last insert id: $db->insert_id\n"; # Getting rows in a loop $sql = "SELECT * FROM tmp WHERE id > ?"; $result = prepared_select($db, $sql, [1]); while ($row = $result->fetch_assoc()) { echo "{$row["id"]}: {$row["name"]} : {$row["email"]}\n"; } function prepared_query($mysqli, $sql, $params, $types = "") { $types = $types ?: str_repeat("s", count($params)); $stmt = $mysqli->prepare($sql); $stmt->bind_param($types, ...$params); $stmt->execute(); return $stmt; } function prepared_select($mysqli, $sql, $params = [], $types = "") { return prepared_query($mysqli, $sql, $params, $types)->get_result(); }
Show:  
Copy Clear