0

I'm a php beginner. I saw many videos in youtube, where they are using mysql instead of mysqli; My editor is NetBeans and Netbeans warns me the mysql is deprecated.

$conn = new mysqli($servername, $username, $password);

My doubt:
Should i learn mysql format or just skip that?

Qirel
  • 25,449
  • 7
  • 45
  • 62
kgs
  • 31
  • 5
  • Skip it. Learn MySQLi, and learn how to use prepared statements with that. Don't bother with the deprecated `mysql_` library unless you are going to work on legacy application (in which case, upgrade to a prepared query with MySQLi/PDO). I recommend you look both into PDO and MySQLi prepared statements, learn both and see which one you like most. Its mostly a preference thing, both are equally valid. – Qirel Sep 19 '17 at 06:45
  • 1
    Removing `mysql_query` is one of the best things the core PHP team has done in decades. You may not realize it, but that warning saved you from a whole world of hurt and misery. – tadman Sep 19 '17 at 07:09

2 Answers2

1

Dont learn the mysql_* functions. Even if you stumble accross them in legacy code its not hard to understand them.

As you already said by yourself, mysql_* functions are deprecated (as of PHP 5.5) and even removed (as of PHP 7).

Also, MySQLi is much more secure because it supports prepared statements. If you dont want to use MySQLi, you can also use PDO instead.


PDO: http://php.net/manual/en/book.pdo.php

Prepared Statement: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

Manuel Mannhardt
  • 2,191
  • 1
  • 17
  • 23
  • I'd even say skip `mysqli`, the thing is *barely* better than `mysql_query`. PDO is not just MySQL specific, what you learn will be more useful in the future, and it has a number of conveniences like named placeholders, easier binding, and a less verbose interface. Even better, look at ORMs to abstract your database calls like [Doctrine](http://www.doctrine-project.org/), [Propel](http://propelorm.org/) or [Eloquent](https://laravel.com/docs/master/eloquent). – tadman Sep 19 '17 at 07:08
  • You are right sir, but he clearly asked for it :) Personally im using my own querybuilder or Eloquent if im using Laravel. – Manuel Mannhardt Sep 19 '17 at 07:10
0

Definitely skip it. Using depreciated technologies is never a good idea especially in a production environment. mysqli and pdo are better and much safer solutions. PDO is my personal fav ;). Here is a connection to get you started.

<?php

define("DSN", "mysql:host=localhost;dbname=db_name");
define("USERNAME", "root");
define("PASSWORD", "");

$options = array(PDO::ATTR_PERSISTENT => true);

try{
    $conn = new PDO(DSN, USERNAME, PASSWORD, $options);

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    echo "connection successful";

}catch (PDOException $ex){

    echo "A database error occurred ".$ex->getMessage();

}