This is about the simplest store and retrieve example I could come up with. See also [the Python counterpart](/db/mysql/python/basics1). # Stor1 ## insert.php ```php connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully\n"; function random_words(int $n) { global $words; $result = []; for($i=0;$i<$n;$i++) { $j = rand()%$n; array_push($result,$words[$j]); } return $result; } function camel($xs) { $ys = array_map("camel1",$xs); return implode("",$ys); } function camel1($xs) { if( $xs === "" ) return ""; return strtoupper($xs[0]).strtolower(substr($xs,1)); } function makekey() { return camel(random_words(4)); } function makeval() { $n = 16+(rand()%16); return implode(" ",random_words($n)); } $key = makekey(); $val = makeval(); $now = (new DateTime('now'))->format('Y-m-d H:i:s'); echo "Inserting $key => $val @ $now\n"; /* create a prepared statement */ $stmt = $conn->prepare("INSERT INTO stor1_01 (name, data, date) VALUES (?, ?, ?)"); /* bind parameters for markers */ $stmt->bind_param("sss", $key, $val, $now); if (!$stmt->execute()) { printf("Error message: %s\n", $conn->error); } /* close connection */ $conn->close(); ``` ## list.php ```php connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully\n"; $query = 'SELECT name, data, date FROM stor1_01 LIMIT 20'; $result = $conn->execute_query($query); foreach ($result as $row) { printf("

%s %s %s

\n", $row["name"], $row["date"], $row["data"]); } /* close connection */ $conn->close(); ``` ## get.php ```php connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully\n"; $qs = $_SERVER["QUERY_STRING"]; $query = 'SELECT name, data, date FROM stor1_01 WHERE name=? LIMIT 10'; $result = $conn->execute_query($query,[$qs]); foreach ($result as $row) { printf("

%s %s %s

\n", $row["name"], $row["date"], $row["data"]); } /* close connection */ $conn->close(); ```