0

I got this problem when I run php code to connect with database in mysql.. Can anybody help me? I'm stuck.

Error:

fatal error call to undefined function mysql_connect() in php

Php code:

?php
    mysql_connect("localhost","root","") or die("Could not connect to database");
    mysql_select_db("dadadsdb") or die("Could not select database");
?>
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
aniys
  • 1
  • 1
  • 1
  • 4
  • 2
    The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MySQLi or PDO_MySQL extensions. http://php.net/manual/en/book.mysqli.php – Dhairya Lakhera Nov 12 '17 at 17:10
  • Don't use `mysql*` – Rotimi Nov 12 '17 at 17:10
  • Does this answer your question? [Why shouldn't I use mysql\_\* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Dharman Mar 29 '20 at 19:34

2 Answers2

0

Use mysqli instead of mysql.

$con = mysqli_query("host","username","password","database");

Mysql has been deprecated in PHP 5.5 for various reason and it can be replaced with MySQLi OOP(Object Oriented Programming) or the normal procedural style. You can also use PDO that is more secure than MySQL.

Ivan
  • 5
  • 5
  • I already change mysql_* to mysqli_* just like what you have write. But still not working. It turns out to be error : mysqli_select_db() expects parameter exactly 2 parameters – aniys Nov 12 '17 at 17:33
  • @aniys Do not use the mysqli_select_db function if you have already put the database name in mysqli_connect. – Ivan Nov 12 '17 at 17:39
  • okay as you said. but still got error. Is it because i'm using php version 7? – aniys Nov 12 '17 at 17:47
0

mysql_* functions are deprecated in newer PHP versions and in PHP 7 they are just gone. If you are using PHP 5.x you could install these modules by sudo apt-get install php-mysql (sometimes it is sudo apt-get install php5-mysql but I recommend you to use mysqli or PDO.

MilMike
  • 12,571
  • 15
  • 65
  • 82