Last active
September 14, 2022 21:15
-
-
Save stemar/ccf16f20d9b5cae3e59e62888a5c685e to your computer and use it in GitHub Desktop.
Show MySQL types vs PHP types (MySQL formats) from a SELECT statement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Connect | |
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); | |
$mysqli = mysqli_connect('localhost', 'root', ''); | |
printf("Connected: %s\n", mysqli_get_host_info($mysqli)); | |
// Create database | |
$database = 'mysqli_example'; | |
$sql = sprintf("CREATE DATABASE IF NOT EXISTS %s", $database); | |
if (mysqli_query($mysqli, $sql)) { | |
printf("Created database: '%s'\n", $database); | |
} else { | |
printf("Error creating database: '%s'\n", mysqli_error($mysqli)); | |
} | |
// Use database | |
mysqli_select_db($mysqli, $database); | |
// Create table | |
$table = 'users'; | |
$sql = sprintf("CREATE TABLE IF NOT EXISTS %s ( | |
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY, | |
firstname VARCHAR(30) NOT NULL, | |
lastname VARCHAR(30) NOT NULL, | |
email VARCHAR(50) DEFAULT NULL, | |
archive TINYINT(1) NOT NULL DEFAULT '0', | |
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | |
)", $table); | |
if (mysqli_query($mysqli, $sql)) { | |
printf("Created table: '%s'\n\n", $table); | |
} else { | |
printf("Error creating table: '%s'\n\n", mysqli_error($mysqli)); | |
} | |
// Show Create Table | |
$sql = sprintf("SHOW CREATE TABLE %s", $table); | |
$result = mysqli_query($mysqli, $sql); | |
if ($result && mysqli_num_rows($result) > 0) { | |
while ($row = mysqli_fetch_assoc($result)) { | |
echo $row['Create Table'], PHP_EOL, PHP_EOL; | |
} | |
} else { | |
echo "0 rows in table.", PHP_EOL; | |
} | |
// Insert data | |
$sql = sprintf("SELECT * FROM %s", $table); | |
$result = mysqli_query($mysqli, $sql); | |
if ($result && mysqli_num_rows($result) == 0) { | |
$sql = sprintf("INSERT INTO %s (firstname, lastname, email) VALUES ('John', 'Doe', NULL)", $table); | |
if (mysqli_query($mysqli, $sql)) { | |
printf("Inserted new row. Last insert ID: %s\n\n", mysqli_insert_id($mysqli)); | |
} else { | |
printf("Error: %s\n%s\n\n", $sql, mysqli_error($mysqli)); | |
} | |
} | |
// Show columns | |
$sql = sprintf("DESCRIBE %s", $table); | |
$describe = array(); | |
$result = mysqli_query($mysqli, $sql); | |
if ($result && mysqli_num_rows($result) > 0) { | |
while ($row = mysqli_fetch_assoc($result)) { | |
$describe[$row['Field']] = $row['Type']; | |
} | |
} else { | |
echo "0 rows in table.", PHP_EOL; | |
} | |
// Select data | |
$sql = sprintf("SELECT * FROM `%s`", $table); | |
echo $sql, PHP_EOL; | |
$output = array(); | |
$result = mysqli_query($mysqli, $sql); | |
if ($result && mysqli_num_rows($result) > 0) { | |
while ($row = mysqli_fetch_assoc($result)) { | |
foreach ($row as $key => $value) { | |
$output[$key]= array( | |
'Value' => $value, | |
'MySQL type' => $describe[$key], | |
'PHP type (MySQL format)' => gettype($value), | |
); | |
} | |
echo preg_replace("/=>[\s]+array \(/", '=> array(', var_export($output, TRUE)), PHP_EOL; | |
ob_start(); var_dump($row); $ob = ob_get_clean(); | |
echo 'var_dump ', str_replace("=>\n", '=>', $ob), PHP_EOL; | |
} | |
} else { | |
echo "0 rows selected.", PHP_EOL; | |
} | |
// Drop database | |
$sql = sprintf("DROP DATABASE IF EXISTS %s", $database); | |
if (mysqli_query($mysqli, $sql)) { | |
printf("Dropped database: '%s'\n", $database); | |
} else { | |
printf("Error dropping database: '%s'\n", mysqli_error($mysqli)); | |
} | |
// Disconnect | |
mysqli_close($mysqli); | |
echo "Disconnected.", PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result in
php -a
console: