Using tables
Previous  Next

«In SQL databases a table is a set of data elements (values) that is organized using a model of horizontal rows and vertical columns. The columns are identified by name, and the rows are identified by the values appearing in a particular column subset which has been identified as a candidate key. Table is the lay term for relation. A table has a specified number of columns but can have any number of rows.» from Wikipedia

Listing tables

Assuming that we have a myshop database and that it is empty, let's proceed to the next sample script:
<?php
            include 'gladius/gladius.php';

            $G = new Gladius();

            $G->SelectDB('myshop') or die($G->errstr);

            $rs = $G->Query('SHOW TABLES');

            // print the tables
            print_r($rs->GetArray());
?>

The result should be:

Array ( )

Creating tables

Now let's create our first table. We need a table where to store some customers' informations like name, surname and phone contact. We will call this table phonebook.

The success of your database solution depends by how careful your database design has been, don't make choices like the above in real life solutions.

<?php
            include 'gladius/gladius.php';

            $G = new Gladius();

            $G->SelectDB('myshop') or die($G->errstr);

            // write the SQL statement into a variable
            $sql = 'CREATE TABLE phonebook (
                        name                varchar (200),
                        surname           varchar (200),
                        phone               varchar(100)
                        )';

            $G->Query($sql);

            // show the result
            echo $G->errstr;
?>
Loading data into a table

<?php
            include 'gladius/gladius.php';

            $G = new Gladius();

            $G->SelectDB('myshop') or die($G->errstr);

            // write the SQL statement into a variable
            $sql = "INSERT INTO phonebook (name, surname, phone)
                        VALUES(
                        'Giuseppe',
                        'Albertini',
                        '0000-0001'
                        )";

            $G->Query($sql);

            // show the result
            echo $G->errstr;
?>

What did we do? We have added one row in the table phonebook of database myshop with the data of our customer in it.

Let's see another example of the same command, which does not use the explicit columns declaration.

<?php
            include 'gladius/gladius.php';

            $G = new Gladius();

            $G->SelectDB('myshop') or die($G->errstr);

            // write the SQL statement into a variable
            $sql = "INSERT INTO phonebook
                        VALUES(
                        'Gabriele',
                        'D'' Annunzio',
                        '1000-0000'
                        )";

            $G->Query($sql);

            // show the result
            echo $G->errstr;
?>

Did you notice it? We did not specify (name, surname, phone) right after the table name (phonebook) and before VALUES. This syntax is allowed only when you are going to specify the field values in their order of definition.

Another note: since the surname D'Annunzio contains the single quote character we had to escape it using two single quote characters. Read more about SQL escaping.

Proceed to read Retrieving information from tables