<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// Create a connection to the database
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
// Check for connection errors
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Check if table exists before creating it
if ($mysqli->query("SHOW TABLES LIKE 'myCity'")->num_rows == 0) {
// If the table doesn't exist, create it by duplicating the structure of the 'City' table
if (!$mysqli->query("CREATE TABLE myCity LIKE City")) {
die("Error creating table: " . $mysqli->error);
}
} else {
echo "Table 'myCity' already exists.\n";
}
// Insert a new record into 'myCity'
$query = "INSERT INTO myCity (ID, Name, CountryCode, District, Population) VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
if ($mysqli->query($query)) {
printf("New record has ID %d.\n", $mysqli->insert_id);
} else {
echo "Error inserting record: " . $mysqli->error;
}
// Close the connection
$mysqli->close();
?>
No hay comentarios:
Publicar un comentario