Clean up PHP & MYSQL code (Picked up by Swift as blog reader iOS app) New to PHP and was just wondering if any improvements can be made, maybe increase efficiency or just clean up code?

I know I'm using an old MYSQL maybe we can update it. Just want to make sure the connection can be as best to its ability. Also, if you can, can you explain on how to improve security?

The JSON gets picked up by Swift iOS app.

&lt;?php

$dbhost = "website";
$dbuser = "user";
$dbpass = "password";
$dbname = "database";

$connection = mysql_connect($dbhost,$dbuser,$dbpass);

// Checking if connection can be established
if(!$connection){

    die("Connection Failed");

} else {

// Selecting Database
$dbconnect = mysql_select_db($dbname, $connection);

// Checking if it can connect to Database
if(!$dbconnect){

    die("Unable to connect to Database");

} else {

    $query = "SELECT * FROM blogs";
    $resultset = mysql_query($query, $connection);
    $records= array();

    // Loop and check all records and add to array
    while($r = mysql_fetch_assoc($resultset)) {

        $records[] = $r;
    }

    // Output data as JSON
    echo json_encode($records);
 }
}

?&gt;
Consider this:

&lt;?php
    $dbhost     = "website";
    $dbuser     = "user";
    $dbpass     = "password";
    $dbname     = "database";
    $connection = mysql_connect($dbhost, $dbuser, $dbpass);
    //Kill page if connection could not be established
    //I also added mysql_error() for it to display an error if there was an issue, so you can troubleshoot.
    if(!$connection) { 
        die("Connection Failed:" . mysql_error()); 
    }

    $dbconnect = mysql_select_db($dbname, $connection);
    //Same as before, kill page if there was an issue, and display an error.
    //No need for the IF-ELSE statements, if the `die()` function is hit, the page stops loading anyways.
    if(!$dbconnect) { 
        die("Unable to connect to Database: " . mysql_error()); 
    }

    $query     = "SELECT * FROM blogs";
    $resultset = mysql_query($query, $connection);
    $records   = array();
    // Loop and check all records and add to array
    while($r = mysql_fetch_assoc($resultset)) {
        $records[] = $r;
    }
    // Output data as JSON
    echo json_encode($records);
?&gt;


But I highly recommend switching to mysqli_* or PDO statements:

Please, don't use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process, and mysql_* functions have been officially removed in PHP 7. Instead you should learn about prepared statements and use either PDO or mysqli_*. If you can't decide, this article will help to choose your best option.



Here is a refactor to mysqli_*

&lt;?php
    $dbhost     = "website";
    $dbuser     = "user";
    $dbpass     = "password";
    $dbname     = "database";

    $records   = array();

    $connection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    if($connection-&gt;connect_errno &gt; 0){
        die('Unable to connect to database [' . $connection-&gt;connect_error . ']');
    }
    $query     = "SELECT * FROM blogs";
    if(!$result = $connection-&gt;query($query)){
        die('There was an error running the query [' . $connection-&gt;error . ']');
    }
    while($row = $result-&gt;fetch_assoc()){
        $records[] = $row;
    }
    echo json_encode($records);
?&gt;


There are a couple of differences here, the first connection checks for an actual connection to the database AND to the table in 1 statement.

Another difference is it shows an error specifically for the query if there was a problem, and not for a table connection error in the 2nd error check.



Here is a PDO refactor:


  NOTE: I don't actively develop with PDO, I may have missed something here.


&lt;?php
    $dbhost     = "website";
    $dbuser     = "user";
    $dbpass     = "password";
    $dbname     = "database";
    $charset = 'utf8';

    $records   = array();

    $dsn = "mysql:host=$dbhost;dbname=$dbname;charset=$charset";
    $opt = [
        PDO::ATTR_ERRMODE            =&gt; PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE =&gt; PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   =&gt; false,
    ];
    $pdo = new PDO($dsn, $dbuser, $dbpass, $opt);

    $query     = "SELECT * FROM blogs";
    $stmt = $pdo-&gt;query($query);
    while ($row = $stmt-&gt;fetch()) {
        $records[] = $row;
    }
?&gt;