Weiter Zurück [Inhalt] Online Suche im Handbuch

50. Projekt MySQL und PHP3 für Fortgeschrittene

Dieses Projekt ist speziell für diejenigen gedacht, die mit PHP3 Verbindungen zu anderen Servern im Internet aufbauen möchten. Beispielsweise wird hier detailliert die Abfrage der DENIC Datenbank beschrieben, in der alle Besitzer von Domains mit Name, Adresse, Anschrift, Faxnummer, u.s.w. enthalten sind. In Verbindung mit MySQL lassen sich natürlich so ganz andere Marketingmöglichkeiten realisieren.....

<html>
<body>
<?
    echo("Hello World\n");
?>

Nunja ich denke, daß ein "Hallo Welt !" niemanden mehr hinterm Ofen hervorlocken kann, es ist trotzdem immer ein guter Test, um festzustellen, ob PHP3 implementiert ist ....

shell> mysql -uUSERNAME -pPASSWORD

mysql> create database phptest;
Query OK, 1 row affected (0.13 sec)

mysql> create table TEST
    -> (
    -> ID        int auto_increment primary key,
    -> Name      varchar(32),
    -> Age       int,
    -> Salary    int
    -> );
Query OK, 0 rows affected (0.11 sec)

mysql> insert into TEST values (0,'Billy Bob Barker','87','93000');
Query OK, 1 row affected (0.09 sec)

mysql> insert into TEST values (0,'Sniffling Sam Sloth','23','12000');
Query OK, 1 row affected (0.01 sec)

mysql> insert into TEST values (0,'Roy Bartley','31','87000');
Query OK, 1 row affected (0.01 sec)

mysql> insert into TEST values (0,'Leroy Longrad','45','63000');
Query OK, 1 row affected (0.01 sec)

mysql> insert into TEST values (0,'Amy Antwerp','37','34000');
Query OK, 1 row affected (0.01 sec)

mysql> insert into TEST values (0,'Kim Kruger','57','76000');
Query OK, 1 row affected (0.01 sec)

mysql> select * from TEST;
+----+---------------------+------+--------+
| ID | Name                | Age  | Salary |
+----+---------------------+------+--------+
|  1 | Billy Bob Barker    |   87 |  93000 |
|  2 | Sniffling Sam Sloth |   23 |  12000 |
|  3 | Roy Bartley         |   31 |  87000 |
|  4 | Leroy Longrad       |   45 |  63000 |
|  5 | Amy Antwerp         |   37 |  34000 |
|  6 | Kim Kruger          |   57 |  76000 |
+----+---------------------+------+--------+
6 rows in set (0.16 sec)

Create a new file, "testdb.php3" and type in the following: 

<html>
<body>
<?
    require("util.php3");
    $sql = new MySQL_class;
    $sql->Create("phptest");

    echo("Database connection successful.\n");
?>



Das sind die Routinen in util.php3

<?
/*
 * Utility routines for MySQL.
 */

class MySQL_class {
    var $db, $id, $result, $rows, $data, $a_rows;
    var $user, $pass, $host;

    /* Make sure you change the USERNAME and PASSWORD to your name and
     * password for the DB
     */

    function Setup ($user, $pass) {
        $this->user = $user;
        $this->pass = $pass;
    }

    function Create ($db) {
        if (!$this->user) {
            $this->user = "USERNAME";
        }
        if (!$this->pass) {
            $this->pass = "PASSWORD";
        }
        $this->db = $db;
        $this->id = @mysql_pconnect($this->host, $this->user, $this->pass)
or
            MySQL_ErrorMsg("Unable to connect to MySQL server: $this->host :
'$SERVER_NAME'");
        $this->selectdb($db);
    }

    function SelectDB ($db) {
        @mysql_select_db($db, $this->id) or
            MySQL_ErrorMsg ("Unable to select database: $db");
    }

    # Use this function is the query will return multiple rows.  Use the
Fetch
    # routine to loop through those rows.
    function Query ($query) {
        $this->result = @mysql_query($query, $this->id) or
            MySQL_ErrorMsg ("Unable to perform query: $query");
        $this->rows = @mysql_num_rows($this->result);
        $this->a_rows = @mysql_affected_rows($this->result);
    }

    # Use this function if the query will only return a
    # single data element.
    function QueryItem ($query) {
        $this->result = @mysql_query($query, $this->id) or
            MySQL_ErrorMsg ("Unable to perform query: $query");
        $this->rows = @mysql_num_rows($this->result);
        $this->a_rows = @mysql_affected_rows($this->result);
        $this->data = @mysql_fetch_array($this->result) or
            MySQL_ErrorMsg ("Unable to fetch data from query: $query");
        return($this->data[0]);
    }

    # This function is useful if the query will only return a
    # single row.
    function QueryRow ($query) {
        $this->result = @mysql_query($query, $this->id) or
            MySQL_ErrorMsg ("Unable to perform query: $query");
        $this->rows = @mysql_num_rows($this->result);
        $this->a_rows = @mysql_affected_rows($this->result);
        $this->data = @mysql_fetch_array($this->result) or
            MySQL_ErrorMsg ("Unable to fetch data from query: $query");
        return($this->data);
    }

    function Fetch ($row) {
        @mysql_data_seek($this->result, $row) or
            MySQL_ErrorMsg ("Unable to seek data row: $row");
        $this->data = @mysql_fetch_array($this->result) or
            MySQL_ErrorMsg ("Unable to fetch row: $row");
    }

    function Insert ($query) {
        $this->result = @mysql_query($query, $this->id) or
            MySQL_ErrorMsg ("Unable to perform insert: $query");
        $this->a_rows = @mysql_affected_rows($this->result);
    }

    function Update ($query) {
        $this->result = @mysql_query($query, $this->id) or
            MySQL_ErrorMsg ("Unable to perform update: $query");
        $this->a_rows = @mysql_affected_rows($this->result);
    }

    function Delete ($query) {
        $this->result = @mysql_query($query, $this->id) or
            MySQL_ErrorMsg ("Unable to perform Delete: $query");
        $this->a_rows = @mysql_affected_rows($this->result);
    }
}

/* ********************************************************************
 * MySQL_ErrorMsg
 *
 * Print out an MySQL error message
 *
 */

function MySQL_ErrorMsg ($msg) {
    # Close out a bunch of HTML constructs which might prevent
    # the HTML page from displaying the error text.
    echo("\n");
    echo("\n");

    # Display the error message
    $text  = "<font color=\"#ff0000\" size=+2><p>Error: $msg :";
    $text .= mysql_error();
    $text .= "\n";
    die($text);
}
?>


Weiter Zurück [Inhalt] Online Suche im Handbuch