1

i want to open a connection to mysql database using php with following code:

$connection= mysql_connect("localhost","m****","******");

but I've got undefined function mysql_connect()

i checked out my phpinfo() and got this: phpinfo()

so i tried to turn on mysqli.allow_local_infile so i do following in php.ini file: mysqli.allow_local_infile = On

and tried to restart apache2 and mysql services but does not effect.

what should i do? thanks in advance,

I'm using Ubuntu 18.4 with php 7.2.24 and mysql Ver 14.14 Distrib 5.7.28

Mostafa Jamareh
  • 1,389
  • 4
  • 22
  • 54
  • 1
    Some answers imply that the function was renamed, which is totally misleading. The whole extension was abandoned and it was eventually removed **several years ago**. You need to rewrite your app to use a currently available extension such as PDO or mysqli. – Álvaro González Jan 01 '20 at 11:50

4 Answers4

2

You need to change your function name from mysql_connect to mysqli_connect as mysql_ is not supported in PHP7.x

further, you need to use all functions having mysqli_ instead of mysql_

For more guidance: https://www.php.net/manual/en/book.mysqli.php

Amit Joshi
  • 456
  • 1
  • 8
  • 21
Naveed Ramzan
  • 3,565
  • 3
  • 25
  • 30
1

mysql not supported in PHP 7 so you can use mysqli.

https://www.php.net/manual/en/function.mysqli-connect.php

kelvin kantaria
  • 1,438
  • 11
  • 15
1

use mysqli_connect instead of mysqli_connect

$connection= mysqli_connect("localhost","m****","******");
VIKAS KATARIYA
  • 5,867
  • 3
  • 17
  • 34
0

If you're working with legacy code, it's possible to install the MySQL extension in PHP 7 if you really need it, but it's deprecated and removed in PHP 7, but still possible to compile PHP to include it, although a pain to achieve. Either way, best to rewrite code to use either MySQLi or PDO instead.

If you don't have the time to do refactoring, and don't have the time to get the deprecated and removed MySQL extension into PHP, you can also try using code that emulates the MySQL extension using the MySQLi extension:
https://github.com/e-sites/php-mysql-mysqli-wrapper
https://github.com/kijin/mysql-compat
I've used such code before in the past in conjunction with auto_prepend_file to get a whole legacy codebase onto PHP 7 without spending much time refactoring code lacking tests, etc. The idea behind doing it with auto_prepend_file is in case the legacy code has multiple entry points to save you from editing several files in the codebase so it's always available. One drawback of such approaches is that code which explicitly checks for the MySQL extension to be loaded will fail to detect the extension, such has been my experience with certain Wordpress versions.

All that said, if you lack the time, try to convince whoever you're working for, or yourself, that it's worthwhile swapping over to either MySQLi or PDO and taking the time to refactor code, etc.

Ultimater
  • 4,647
  • 2
  • 29
  • 43