MySQL
Παρακάτω θα προχωρήσουμε με την δημιουργία μίας απλής βάσης δεδομένων,
η οποία θα χρησιμοποιηθεί ως μία agenda, ο πίνακας στον οποίο θα αποθηκεύσουμε
τις πληροφορίες μας είναι ο users και η δομή του είναι η εξής:
CREATE TABLE IF NOT EXISTS `friends` ( `id` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(25) NOT NULL, `lsname` VARCHAR(50) NOT NULL, `tel` CHAR(10) NOT NULL, PRIMARY KEY (`id`) ) ; INSERT INTO `agenda`.`friends` VALUES (NULL , 'vag', 'balaskas', '6930000000'); INSERT INTO `agenda`.`friends` VALUES (NULL , 'bal', 'vaggelis', '6970000000');
SELECT
select.php
<?php $host = 'localhost' ; $port = 3306; $user = 'root' ; $pass = ''; $db = 'agenda'; $tb = 'friends'; $link = mysql_connect( $host.":".$port, $user, $pass); if ( $link != 0 ) { mysql_select_db($db) or die('Could not select database'); // Performing SQL query $query = 'SELECT * FROM '.$tb; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); // Printing results in HTML print("<p align='center'>Results : </p>"); echo "<p align='center'><table cellpadding='5' cellpadding='0' border='1'>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table></p>\n"; // Free resultset mysql_free_result($result); // Closing connection mysql_close($link); }else{ echo "failed"; } ?>
INSERT
insert.php
<FORM method="POST"> <table> <TR> <TD>Give Name</TD> <TD><INPUT type="text" name="name"></TD> </TR> <TR> <TD>Give Last Name</TD> <TD><INPUT type="text" name="sname"></TD> </TR> <TR> <TD>Give Tel</TD> <TD><INPUT type="text" name="tel"></TD> </TR> <tr><TD colspan="2"><INPUT type="reset" value="Clear"> <input type="submit" value="Insert"></TD></tr> </table> </FORM> <?php $host = 'localhost' ; $port = 3306; $user = 'root' ; $pass = ''; $db = 'agenda'; $tb = 'friends'; $link = mysql_connect( $host.":".$port, $user, $pass); if ( $link != 0 ) { mysql_select_db($db) or die('Could not select database'); // Performing SQL query if ( ( !empty ( $_POST['name'] ) ) && ( !empty ( $_POST['sname'] ) ) && ( !empty ( $_POST['tel'] ) ) ) { $query = "INSERT INTO ".$tb." VALUES (NULL, '".$_POST['name']."', '".$_POST['sname']."', '".$_POST['tel']."');"; mysql_query($query) or die('Query failed: ' . mysql_error()); } $query = 'SELECT * FROM '.$tb; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); // Printing results in HTML echo "<table border=1>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; // Free resultset mysql_free_result($result); // Closing connection mysql_close($link); }else{ echo "failed"; } ?>
SEARCH
<FORM method="POST"> <table> <TR> <TD>Give Name</TD> <TD><INPUT type="text" name="name"></TD> </TR> <tr><TD colspan="2"><INPUT type="reset" value="Clear"> <input type="submit" value="search"></TD></tr> </table> </FORM> <?php $host = 'localhost' ; $port = 3306; $user = 'root' ; $pass = ''; $db = 'agenda'; $tb = 'friends'; $link = mysql_connect( $host.":".$port, $user, $pass); if ( $link != 0 ) { mysql_select_db($db) or die('Could not select database'); // Performing SQL query if ( ( !empty ( $_POST['name'] ) ) ) $query = "SELECT * FROM ".$tb . " WHERE name LIKE '%" . $_POST['name'] . "%'; "; else $query = 'SELECT * FROM '.$tb; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); // Printing results in HTML echo "<table border=1>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; // Free resultset mysql_free_result($result); // Closing connection mysql_close($link); }else{ echo "failed"; } ?>