2

I had registered my website with a domain and hosting. It consist of a database. I have written the connection PHP file as given below. It doesn't work and is displaying this error:

Call to undefined function mysql_connect().

I create the database in cPanel:

<?php
    mysql_connect('localhost', 'xxx', 'xxx');
    mysql_select_db('xxx');
?>
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Sourabh23
  • 19
  • 2
  • 6
  • 9
    you don't have the mysql_*() functions enabled in php. that's a good thing. don't even THINK of using them. use mysqli or pdo instead. mysql_*() is deprecated and removed in the latest php versions. – Marc B Nov 25 '15 at 21:24
  • You should be using the `mysqli_` range of functions. `mysql_` is now deprecated. – Luke Joshua Park Nov 25 '15 at 21:24
  • `sudo apt-get install php5-mysql1` (debian, ubuntu) – Ryan Nov 25 '15 at 21:24
  • @self Pretty sure there's no`1` at the end... Also, I would recommend using `php5-mysqlnd` instead, which uses the MySQL native driver. – Mike Nov 25 '15 at 21:31
  • Or better yet: http://stackoverflow.com/questions/15346605/php-apache-php-fatal-error-call-to-undefined-function-mysql-connect – Mike Nov 25 '15 at 21:33

1 Answers1

5

As mentioned in comments mysql_ is deprecated and has been removed from PHP 7.

The possible reason it's saying mysql_connect is undefined is because your host must be using latest PHP version (PHP 7) or is blocked by default from using mysql_. If you want, you can enable it (I will not recommend it).

Go to your cPanel -> Select PHP version ->
//There will be several checkboxes. Tick MySQL to enable it.

Enter image description here

This method is for cPanel only. I am not sure about others host or Plesk.

You can change your host (Bigrock) PHP version as mentioned here.

Instead of mysql_ you can use mysqli_:

$conn = mysqli_connect($localhost, $username, $password, $db_name);

You can learn more about mysql_, mysqli_, and PDO at Choosing an API.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Skyyy
  • 1,457
  • 1
  • 22
  • 56
  • as of writing, php7 is not released, only "php7 release candidates", development preview, latest being RC 7 – hanshenrik Nov 26 '15 at 05:31
  • Many hosting services started providing support for php7. Well they do say not use in production – Skyyy Nov 26 '15 at 05:34
  • In that case I have to change my whole document from mysql to mysqli. Isn't that possible to run with mysql code – Sourabh23 Nov 26 '15 at 09:05
  • For now you can run `mysql_` in Current php version. But in few months when every host will upgrade to php7. You script will stop working. So its better to change your script now than later. – Skyyy Nov 26 '15 at 09:08
  • For now if you wan't to make it work check your current php version. If its not set to php 7. Some host block `mysql_` for security purposes you can enable it if you want . You will find settings to enable it where you can choose php versions. This option will also be removed in php7 – Skyyy Nov 26 '15 at 09:10
  • Updated question how you can enable it – Skyyy Nov 26 '15 at 09:14
  • I buy my host from Bigrock. So where I check there php version they use – Sourabh23 Nov 26 '15 at 09:17
  • http://manage.bigrock.in/kb/answer/1592 You can change your php version in blue rock as mentioned here.. (they have some missing images in there documentation) – Skyyy Nov 26 '15 at 09:22
  • run `` to get all the details of your php – Skyyy Nov 26 '15 at 09:25