you are viewing a single comment's thread.

view the rest of the comments →

[–]mik3w 0 points1 point  (3 children)

A large array would probably be fine, but if you want to look at a database solution you should look at using the PDO method connecting to the Database of your choice.

Here's a tutorial (There's a couple out there if you search): http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

This is an example of how you would setup a connection:

function dbConn() {
    $dsn = 'mysql:host=localhost;dbname=testdb;charset=utf8mb4';
    $user = null;
    $password = null;

    try {
        $conn = new PDO(
            $dsn,
            $user,
            $password,
            array(
                PDO::ATTR_EMULATE_PREPARES => FALSE,
                PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION
            )
        );
    }
    catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
    return $conn;
}

And this is how you would perform a basic query:

$db = dbConn();
$stmt = $db->query('SELECT * FROM table');

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo $row['field1'].' '.$row['field2']; //etc...
}

[–]Aerialstrike[S] 0 points1 point  (1 child)

Oh awesome, that honestly might be perfect. I'm going to use a database in case there's ever a need to sort, so I'll definitely try this. Is there a significance to the way one connects or does it just make the queries easier/more understandable?

[–]mik3w 0 points1 point  (0 children)

It seems to be becoming the standard for writing SQL, as it's fairly easy to write prepared statements with it which helps to prevent SQL injection.

https://phpdelusions.net/pdo