2. Creating a Table
<?php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$query = "CREATE TABLE cats (
id SMALLINT NOT NULL AUTO_INCREMENT,
family VARCHAR(32) NOT NULL,
name VARCHAR(32) NOT NULL,
age TINYINT NOT NULL,
PRIMARY KEY (id)
)";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
?>
3. D
es
cr
ib
in
g
a
Ta
bl
e
<?php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$query = "DESCRIBE cats";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
echo "<table><tr> <th>Column</th> <th>Type</th>
<th>Null</th> <th>Key</th> </tr>";
for ($j = 0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);
echo "<tr>";
for ($k = 0 ; $k < 4 ; ++$k) echo "<td>$row[$k]</td>";
echo "</tr>";
}
echo "</table>";
?>
5. Dropping a Table
<?php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username,
$db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$query = "DROP TABLE cats";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
?>
6. Adding Data
<?php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$query = "INSERT INTO cats VALUES(NULL, 'Lion', 'Leo', 4)";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
?>
You may wish to add a couple more items of data by modifying $query as
follows and
calling up the program in your browser again:
$query = "INSERT INTO cats VALUES(NULL, 'Cougar', 'Growler', 2)";
$query = "INSERT INTO cats VALUES(NULL, 'Cheetah', 'Charly', 3)";
7. Retrieving rows from the cats table
<?php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$query = "SELECT * FROM cats";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
echo "<table><tr> <th>Id</th> <th>Family</th>
<th>Name</th><th>Age</th></tr>";
for ($j = 0 ; $j < $rows ; ++$j) {
$row = mysql_fetch_row($result);
echo "<tr>";
for ($k = 0 ; $k < 4 ; ++$k) echo "<td>$row[$k]</td>";
echo "</tr>";
}
echo "</table>";
?>
8. This code simply issues the MySQL query SELECT * FROM cats and
then displays all the
rows returned. Its output is as follows:
Id Family Name Age
1 Lion Leo 4
2 Cougar Growler 2
3 Cheetah Charly 3
9. Renaming Charly the cheetah to Charlie
<?php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username,
$db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$query = "UPDATE cats SET name='Charlie' WHERE
name='Charly'";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
?>
10. Now outputs the following:
Id Family Name Age
1 Lion Leo 4
2 Cougar Growler 2
3 Cheetah Charlie 3
11. Removing Growler the cougar from the cats table
<?php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username,
$db_password);
if (!$db_server) die("Unable to connect to MySQL: " .
mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$query = "DELETE FROM cats WHERE name='Growler'";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
?>
12. Adding data to the table cats and reporting
the insertion ID
<?php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username,
$db_password);
if (!$db_server) die("Unable to connect to MySQL: " .
mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$query = "INSERT INTO cats VALUES(NULL, 'Lynx', 'Stumpy',
5)";
$result = mysql_query($query);
echo "The Insert ID was: " . mysql_insert_id();
if (!$result) die ("Database access failed: " . mysql_error());
?>
13. What Is Ajax?
Ajax is a web development technique that uses a set of methods
built into JavaScript to transfer data between the browser and a
server in the background.
An excellent example of this technology is Google Maps , in which
new sections of a map are downloaded from the server when
needed, without requiring a page refresh.
14. Using XMLHttpRequest
Due to the differences between browser implementations of
XMLHttpRequest, it’s necessary to create a special function in order to
ensure that your code will work on all
major browsers.
To do this, you must understand the three ways of creating an
XMLHttpRequest object:
• IE 5: request = new ActiveXObject("Microsoft.XMLHTTP")
• IE 6+: request = new ActiveXObject("Msxml2.XMLHTTP")
• All others: request = new XMLHttpRequest()
15. Example : A cross-browser Ajax function
<script>
function ajaxRequest()
{
try // Non-IE browser?
{
var request = new XMLHttpRequest()
}
catch(e1)
{
try // IE 6+?
{
request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch(e2)
{
try // IE 5?
{
request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch(e3) // There is no Ajax support
{
request = false
}
}
}
return request
}
</script>