Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some of the safest ways to connect to a database with PHP? [duplicate]

Tags:

syntax

php

mysql

I'm still new to PHP and MYSQL and I'm trying to learn both with modern coding techniques. All the stuff I find online seems to be outdated.

Can anybody suggest anything for me? I am also curious if the below code is outdated? If it is indeed outdated, can you suggest newer and safer methods?

<?php
    $connection = mysql_connect("localhost", "root", "");
    if (!$connection) {
        die("Oops, error happened: " . mysql_error());
    }
?>
like image 458
user1707535 Avatar asked Apr 04 '13 08:04

user1707535


People also ask

What are the two ways to connect to a database in PHP?

There are two popular ways to connect to a MySQL database using PHP: With PHP's MySQLi Extension. With PHP Data Objects (PDO)


1 Answers

Use PDO functions.

Database Connection Using PDO:

$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
like image 125
Sumit Bijvani Avatar answered Oct 01 '22 03:10

Sumit Bijvani