Quakenet/#php Tutorial

Note: If you opened this page from an external URL pay attention that all chapters are linked together. Be sure you also read all prior chapters of this tutorial, otherwise you will miss relevant content explained before.

MySQL / Output values

  1. Output a value
  2. Indent the SELECT query

1. Output a value

As we filled the tables with rows we want to read them out. This can be done with the SELECT command. It got a huge list of parameters. Howevery, whatever the SELECT query is calculating and doing the result is always a table, also called result set. This result set got columns and rows like the tables have and can have even 0 rows. Each column got a name to access the specific column.

As an simple example we outputs simple values without using any tables. We use the SELECT keyword to start the query. It is followed by the values we want to read, seperated with commas. These can be static values but also function calls.

SELECT NOW(), "Foobar", 6;

This query returns a result set with 3 columns and 1 row.

Result set of the query
NOW() Foobar 6
2008-08-17 14:31:10 Foobar 6

As you see we have the columns NOW(), Foobar and 6. You also see we got only one row and the value of NOW() is the current time and date.

The column names are the same as the expressions in the SELECT query. To use other names use the AS ... keyword.

SELECT NOW() AS current_time, "Foobar" AS username, 6 AS level;
Ergebnistabelle vom SQL-Query
current_time username level
2008-08-17 14:37:22 Foobar 6

This is important if you want to read from two tables at the same time but got columns with the same name in both tables (like the id columns).

2. Indent the SELECT query

As SELECT queries are very long its common to use a proper indent for them. The values for a mysql keyword are indented by one level.

SELECT NOW() AS current_time, "Foobar" AS username, 6 AS level;

This will become the following query.

SELECT
    NOW() AS current_time,
    "Foobar" AS username,
    6 AS level;

This way you can easily see how many columns the result set will have.

Questions about the chapter

No questions

Back to Next to
Copyright © to the OPs of #php/QuakeNet Valid XHTML 1.0 Strict Valid CSS!