PHP and MySQL 30: query select
Tags: php, mysql, mysqli, pdo
Published: 2019-12-23 10:02:02

___

### <center>Happy Coding</center>

---


## `Home` <a name="home"> [`Previous`](https://dev.to/antelove19/php-and-mysql-20-query-insert-d07)

In this post, we will learn about how to use query **SELECT** in PHP for retrieve data from database MySQL, with 3 ways:

  1. [MySQLi procedural](#procedural) 
  2. [MySQLi object-oriented](#object)
  3. [PDO](#pdo)

> For known, how to connect to database, can be learned in [here](https://dev.to/antelove19/php-and-mysql-11-connection-database-2mm)

Insert query is used for insert data or record into database

Syntax:
```php
    SELECT column1, column2, column3 
    FROM table_name;
```
using wilcard (*) for select all column
```php
    SELECT *
    FROM table_name;
```

Example:

```php
    $sql  = "SELECT id, firstname, lastname ";
    $sql .= "FROM users " ;
```
```php
    $sql  = "SELECT * ";
    $sql .= "FROM users " ;
```
___


And, for execute the query, we will use 3 ways:


### 1. MySQLi[^1] procedural <a name="procedural"></a>

[`Back to Home`](#home)

Execute query using **mysqli::query**[^2] and get a **`$result`**:

```php
    $result = mysqli_query( $connection, $sql );
```

Display data using **mysqli_result::fetch_assoc**[^3], alternative using **mysqli_result::fetch_object**[^4]:

```php
    echo "<pre>";

    echo "No \t";
    echo "ID \t";
    echo "Firstname \t\t";
    echo "Lastname \n";

    $no = 1; /* variabel $no */

    /* loop while */
    while ( $row = mysqli_fetch_assoc($result) )
    {

        echo $no . "\t";
        echo $row['id'] . "\t";
        echo $row['firstname'] . "\t\t";
        echo $row['lastname'] . "\n";

        $no++; /* increment */

    }

    echo "</pre>";
```

Close connection using **mysqli::close**[^5]:

```php
    mysqli_close($connection);
```

Source Code:

* [repl.it/antelove19/MySQLi/procedural/select](https://repl.it/@antelove19/MySQLi-procedural-30-select)

___


### 2. MySQLi[^1] object-oriented <a name="object"></a>

[`Back to Home`](#home)

Execute query using **mysqli::query**[^2] and get a **`$result`**:

```php
    $result = $connection->query($sql);
```

Display data using **mysqli_result::fetch_object**[^4]:

```php
    echo "<pre>";

    echo "No \t";
    echo "ID \t";
    echo "Firstname \t";
    echo "Lastname \n";

    $no = 1; /* variabel $no */

    /* loop while */
    while ( $row = $result->fetch_object() )
    {

        echo $no . "\t";
        echo $row->id . "\t";
        echo $row->firstname . "\t\t";
        echo $row->lastname . "\n";

        $no++; /* increment */

    }

    echo "</pre>";
```

Close connection using **mysqli::close**[^5]:

```php
     $connection->close();
```

Source Code:

* [repl.it/antelove19/MySQLi/object-oriented/select](https://repl.it/@antelove19/MySQLi-object-oriented-30-select)

___


### 3. PDO[^7] <a name="pdo"></a>

[`Back to Home`](#home)

Execute query and get a **`$result`**:

```php
    $result = $connection->query($sql);
```

Display Data:

```php
    echo "<pre>";

    echo "No \t";
    echo "ID \t";
    echo "Firstname \t";
    echo "Lastname \n";

    $no = 1; /* variabel $no */

    /* loop foreach */
    foreach ( $result as $row  )
    {

        echo $no . "\t";
        echo $row->id . "\t";
        echo $row->firstname . "\t\t";
        echo $row->lastname . "\n";

        $no++; /* increment */

    } 

    echo "</pre>";
```

Close connection:

```php
    $connection = null;
```

Source Code:

* [repl.it/antelove19/PDO/select](https://repl.it/@antelove19/PDO-30-select)


## [`Back to Home`](#home) | [`Next#`](#)

___

<center>Thank for reading :)</center>

___


[^1]: **php.net**, "MySQL Improved Extension", accessed on date 21 december 2019 and from [_https://www.php.net/manual/en/book.mysqli.php_] (https://www.php.net/manual/en/mysqli.construct.php)

[^2]: **php.net**, "mysqli::query", accessed on date 22 december 2019 and from [_https://www.php.net/manual/en/mysqli.query.php_] (https://www.php.net/manual/en/mysqli.query.php)

[^3]: **php.net**, "mysqli_result::fetch_assoc", accessed on date 23 december 2019 and from [_https://www.php.net/manual/en/mysqli-result.fetch-assoc.php_] (https://www.php.net/manual/en/mysqli-result.fetch-assoc.php)

[^4]: **php.net**, "mysqli_result::fetch_object", accessed on date 23 december 2019 and from [_https://www.php.net/manual/en/mysqli-result.fetch-object.php_] (https://www.php.net/manual/en/mysqli-result.fetch-object.php)

[^5]: **php.net**, "mysqli::close", accessed on date 21 december 2019 and from [_https://www.php.net/manual/en/mysqli.close.php_] (https://www.php.net/manual/en/mysqli.close.php)

[^6]: **php.net**, "PHP Data Objects", accessed on date 21 december 2019 and from [_https://www.php.net/manual/en/book.pdo.php_] (https://www.php.net/manual/en/book.pdo.php) 

[^7]: **php.net**, "PDO::__construct", accessed on date 21 december 2019 and from [_https://www.php.net/manual/en/pdo.construct.php_] (https://www.php.net/manual/en/pdo.construct.php)