How to use PHP with MySQL: the complete tutorial (with examples)

65 thoughts on “How to use PHP with MySQL: the complete tutorial (with examples)”

    • First, you select the database record containing the BLOB column with the image. The select query is exactly the same as for any other select operation.

      Then, you can use the result image column as it was a real image.
      For example, you can return the image directly like this:

      echo $row[‘image’];

      In this case, the PHP file acts exactly as it was an image file.
      Or you can use the database data in a src attribute, like this (add opening and closing tag)

      img src=”data:image/png;base64,’ . $row[‘image’] . ‘”

      Reply
    • Store the image file names in your database only, you should NEVER store the image files in the database as the files should be stored in a webspace folder! Store the images in a folder in your webspace. and request the image name from the database. Why? Image name is just a small text while images are way larger, it would be faster if image names are requested from the database only. If a lot of image files are stored in the database it will get slow and buggy, also read the answer from Alex, there is a code example in his answer that might help you out. Good luck and happy coding

      Reply
  1. I also have connection with PDO like this:
    DSN = $DSN;
    $this->user = $user;
    $this->password = $password;
    $this->options = $options;
    $this->conn = $this->openConnection();
    }

    protected function openConnection() {
    //creating an object of mysqli
    //$cnn = new mysqli($this->host, $this->user, $this->password, $this->database);
    try {
    $cnn = new PDO($this->DSN,$this->user,$this->password,$this->options);
    return $cnn;
    }
    catch (PDOException $e) {
    die(“There is some problem in connection: ” .$e->getMessage());
    }

    }

    public function getRow($sql) {
    try {
    $stmt = $this->conn->prepare($sql);
    $stmt->execute();
    }
    catch (PDOException $e) {
    die(“Query failed: ” .$e->getMessage());
    }

    // get one row
    $result = $stmt->fetch();
    return $result;
    }

    public function query($sql) {
    try {
    $stmt = $this->conn->prepare($sql);
    $stmt->execute();
    }
    catch (PDOException $e) {
    die(“Query failed: ” .$e->getMessage());
    }

    // get all rows in array but large array consume mucb RAM space
    //$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    //print_r ($stmt);
    return $stmt;
    }

    public function update($sql) {
    try {
    $stmt = $this->conn->prepare($sql);
    }
    catch (PDOException $e) {
    die(“Updation failed: ” .$e->getMessage());
    }

    // get all rows in array but large array consume mucb RAM space
    //$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    //print_r ($stmt);
    return $stmt;
    }

    public function __destruct() {
    //use “unset(object)”, object = $dbconn
    //for destructing the object of mysqli
    if ($this->conn) {
    $this->conn = null;
    }
    }
    }

    defined(“DB_DSN”) ? NULL : define(“DB_DSN”, “mysql:host=localhost;dbname=imgdb”);
    defined(“DB_USER”) ? NULL : define(“DB_USER”, “root”);
    defined(“DB_PASS”) ? NULL : define(“DB_PASS”, “”);
    defined(“DB_NAME”) ? NULL : define(“DB_OPTIONS”, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,));

    $dbconn = new DB(DB_DSN, DB_USER, DB_PASS, DB_OPTIONS);
    ?>

    Reply
    • Hi, thank you for your comment.

      Unfortunately I don’t have a PDF version, but you can make one yourself by “saving it as PDF” from the print dialog. Let me know if it works for you.

      Reply
  2. Is it unsafe for me to use Oracle DB?
    What would be difference and is it okay to do the same things just with an Oracle Based Database?

    Reply
  3. If you want to access a database using PHP, you have mainly two choices – MySQLi (i for improved) and PDO (PHP Data Objects). Is MySQL replaced with MySQLi and PDO in PHP? So, What do I need to learn? Can I learn PDO directly instead of that?

    Reply
    • The old MySQL extension has been entirely replaced by MySQLi.
      Since they share the same syntax, MySQLi is easier to learn for those familiar with the old MySQL.

      PDO is a more abstract layer that works with many database types (MySQL, PostgreSQL, etc.).
      Both MySQLi and PDO are fine. You don’t need to learn both, so if you already know PDO that’s ok and you can stick with it.

      If you want to choose one to learn first, I would say PDO is more powerful but a bit more complicated. So, go for it if you can invest a little more time and effort in learning, otherwise start with MySQLi.

      Reply
    • mysqli_query() is the MySQLi-based function, and it is the one you want to use.
      mysql_query() is based on the old (and no longer available) MySQL library.

      Reply
    • mysql extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0.
      If you use Laravel framework, certainly it won’t work, as Laravel use PHP 7.
      You should upgrade/transform your code to mysqli or PDO so that applications that use PHP 7 can run on the same PHP server.
      This case happens to me because existing app uses mysql extension that runs on old PHP. Exchanging between old PHP instance and PHP 7 instance on the same PC is tedious and might not work. So I have to work around the issue by using Nginx for my project in order the old app can still run on Apache + old PHP instance.
      But I have suggested the programmer of the old app to migrate to MySQLi extension, so his app won’t cause problem to other app that use PHP 7.

      Reply
  4. Wow!! Alex, that was great. So informative. Never knew of the PDO::SILENT mode. Amazing.

    But, when we echo Error messages, will those messages be displayed in production, or will they be over-ridden by ‘display_error=false’ setting?

    Reply
    • Hey Nedum,

      the error messages are not affected by the “display_error” flag, because they are not PHP core errors.
      You have to edit the code yourself to show or hide them.

      Reply

Leave a Comment