0

I started learning PHP and decided to try and make a forum out of what I knew. I never really got to connect to MySQL database through PHP I was just wondering why it is giving me this error:

 Fatal error: Uncaught Error: Call to undefined function mysql_connect() in 
 C:\xampp\htdocs\FORUM\connect.php:9 Stack trace: #0 
 {main} thrown in C:\xampp\htdocs\FORUM\connect.php on line 9

I do recognise that it says that I have an undefined function, but I dont know how to define mysql_connect(). I am hoping that someone can have a look at my code and tell me how to fix my (most likely blatant) error.

<?php
    $database = array();
    $database['host'] = "localhost";
    $database['port'] = '3306';
    $database['name'] = "forum";
    $database['username'] = "root";
    $database['password'] = "";

    $link = mysql_connect($database['host'], $database['username'], $database['password']);

    if ($link) {
        echo "Successfully connected to the database : ".$database['name'];
    }   else {
          echo "Connect to the database : ".$database['name'] . "failed<br/>";
          echo "Error : ".mysql_error();
    }
?>
Dharman
  • 26,923
  • 21
  • 73
  • 125
bob ross
  • 27
  • 1
  • 9
  • 1
    Does this answer your question? [Undefined function mysql\_connect()](https://stackoverflow.com/questions/13825108/undefined-function-mysql-connect) – JeffUK Dec 16 '20 at 22:37
  • It's giving you that error because `mysql_*()` was removed in PHP 7. Use `mysqli_*()`, or [PDO](https://www.php.net/manual/en/book.pdo.php) –  Dec 16 '20 at 22:40
  • Not really, like I've said I haven't really touched the database stuff. I've only got a glimpse of it why doing backend with [adonisJS](https://adonisjs.com/). – bob ross Dec 16 '20 at 22:45

1 Answers1

-1
mysql_connect()

was deprecated earlier and is removed from PHP 7. try mysqli_connect (mind the "i"), or better, use PDO, Doctrine, or any other abstraction.

Zsolt Szilagyi
  • 4,274
  • 4
  • 25
  • 43
  • Thanks a lot Zsolt Szilagyi and @CatchAsCatchCan, I best get further into my research next time, but thanks – bob ross Dec 16 '20 at 22:51