yet more mysql stuff

cat-town
Dan Buch 14 years ago
parent 2d9cd9de75
commit 0807e2ee9d

@ -1,19 +1,32 @@
<?php <?php
$MYSQL_CXN = mysqli_connect( $MYSQLCONN = new mysqli(
"localhost", "localhost",
"learningphp", "learningphp",
"learningphp", "learningphp",
"learningphp" "learningphp"
); );
if (mysqli_connect_errno()) {
printf("Could not connect to database: %s\n", mysqli_connect_error());
exit();
}
$ACTION = "";
if (array_key_exists("action", $_POST)) {
$ACTION = $_POST["action"];
}
require_once("_head.php"); require_once("_head.php");
function create_all_tables($cxn) {
return @mysqli_query($cxn, file_get_contents("../setup.sql"));
};
function show_clear_link() {
?>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>">clear</a>
<?php
}
mkheader("mysql!", '#aff'); mkheader("mysql!", '#aff');
@ -21,33 +34,105 @@ mkheader("mysql!", '#aff');
<body> <body>
<!-- ======================================================= --> <?php
<pre><?php
if ($ACTION == "create-tables") {
?><p>Creating tables...</p><pre><?php
print "\$MYSQL_CXN:\n"; $MYSQLCONN->query("DROP TABLE IF EXISTS customers");
print_r($MYSQL_CXN); $create_result = $MYSQLCONN->query("
CREATE TABLE customers (
customer_id MEDIUMINT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
PRIMARY KEY(customer_id)
) ENGINE=InnoDB
");
?></pre> if (!$create_result) {
printf("Failed to create tables: %s\n", $MYSQLCONN->error);
} else {
printf("%d Row inserted.\n", $MYSQLCONN->affected_rows);
print_r($MYSQLCONN->query("DESCRIBE customers"));
print_r($MYSQLCONN->query("SELECT * FROM customers"));
}
<?php ?></pre><?php
if ($_POST['create'] == "yes") { show_clear_link();
create_all_tables($MYSQL_CXN);
} elseif ($ACTION == "show-conn") {
?> ?>
<!-- ======================================================= -->
<pre><?php <pre><?php
print_r(mysqli_query($MYSQL_CXN, "DESCRIBE customers"));
print_r(mysqli_query($MYSQL_CXN, "SELECT * FROM customers"));
?></pre><a href="<?php echo $_SERVER['PHP_SELF']; ?>">clear</a><?php print "\$MYSQLCONN:\n";
print_r($MYSQLCONN);
?></pre><?php
} elseif ($ACTION == "add-customer") {
$add_customer = $MYSQLCONN->prepare(
"INSERT INTO customers (customer_id, name, email) VALUES (NULL, ?, ?)"
);
$add_customer->bind_param('ss', $_POST["name"], $_POST["email"]);
$result = $add_customer->execute();
if (!$result) {
?><p>Failed to create customer!: <?php echo $MYSQLCONN->error ?></p><?php
} else {
?><p><?php echo $MYSQLCONN->affected_rows; ?> Row inserted.</p><?php
}
show_clear_link();
} else { } else {
?> ?>
<form name="setup" action="" method="POST"> <form name="database_admin" action="" method="POST">
<input type="hidden" name="create" value="yes" /> <fieldset>
<input type="submit" name="submit" value="Create Tables" /> <legend>Database Admin</legend>
<span class="block">
<input type="radio" id="action-create-tables" name="action" value="create-tables" />
<label for="action-create-tables">create tables</label>
</span>
<span class="block">
<input type="radio" id="action-show-conn" name="action" value="show-conn" />
<label for="action-show-conn">show connection info</label>
</span>
<input type="submit" name="submit" value="Do Stuff" />
</fieldset>
</form>
<form name="add_customer" action="" method="POST">
<fieldset><legend>Add Customer</legend>
<input type="hidden" name="action" value="add-customer" />
<label class="block" for="customer-name">Name:</label>
<input type="text" id="customer-name" name="name" />
<label class="block" for="customer-email">Email:</label>
<input type="email" name="email" />
<input class="block" type="submit" name="submit" value="Add Customer" />
</fieldset>
</form> </form>
<pre id="customer-listing"><?php
$results = $MYSQLCONN->query("SELECT * FROM customers");
while ($row = $results->fetch_array(MYSQLI_ASSOC)) {
print_r($row);
}
?></pre>
<?php <?php
} }
@ -56,3 +141,9 @@ mkheader("mysql!", '#aff');
</body> </body>
</html> </html>
<?php
$MYSQLCONN->close();
?>

@ -528,7 +528,7 @@ error_reporting = E_ALL & ~E_DEPRECATED
; Development Value: On ; Development Value: On
; Production Value: Off ; Production Value: Off
; http://php.net/display-errors ; http://php.net/display-errors
display_errors = Off display_errors = On
; The display of errors which occur during PHP's startup sequence are handled ; The display of errors which occur during PHP's startup sequence are handled
; separately from display_errors. PHP's default behavior is to suppress those ; separately from display_errors. PHP's default behavior is to suppress those
@ -539,7 +539,7 @@ display_errors = Off
; Development Value: On ; Development Value: On
; Production Value: Off ; Production Value: Off
; http://php.net/display-startup-errors ; http://php.net/display-startup-errors
display_startup_errors = Off display_startup_errors = On
; Besides displaying errors, PHP can also log errors to locations such as a ; Besides displaying errors, PHP can also log errors to locations such as a
; server-specific log, STDERR, or a location specified by the error_log ; server-specific log, STDERR, or a location specified by the error_log
@ -601,7 +601,7 @@ track_errors = Off
; Development Value: On ; Development Value: On
; Production value: Off ; Production value: Off
; http://php.net/html-errors ; http://php.net/html-errors
html_errors = Off html_errors = On
; If html_errors is set On PHP produces clickable error messages that direct ; If html_errors is set On PHP produces clickable error messages that direct
; to a page describing the error or function causing the error in detail. ; to a page describing the error or function causing the error in detail.

@ -1,6 +1,6 @@
CREATE TABLE IF NOT EXISTS customers ( CREATE TABLE customers (
customer_id INTEGER, customer_id INTEGER,
name VARCHAR(255), name VARCHAR(255) NOT NULL,
email VARCHAR(255), email VARCHAR(255) NOT NULL,
PRIMARY KEY(customer_id) PRIMARY KEY(customer_id)
); ) AUTO_INCREMENT = 1, ENGINE = InnoDB

Loading…
Cancel
Save