Fatal error: Uncaught Error: Call to undefined fun

2018-12-31 14:19发布

I am trying to do a simple connection with XAMPP and MySQL server, but whenever I try to enter data or connect to the database, I get this error.

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

Example of line 22:

$link = mysql_connect($mysql_hostname , $mysql_username);

标签: php mysql xampp
8条回答
孤独总比滥情好
2楼-- · 2018-12-31 15:02

mysql_ functions have been removed from PHP 7. You can now use MySQLi or PDO.

MySQLi example:

mysqli_connect($mysql_hostname, $mysql_username, $mysql_password, $mysql_dbname);

mysqli_connect reference link

查看更多
梦醉为红颜
3楼-- · 2018-12-31 15:07

As other answers suggest... Some guy(for whatever reason) decided that your old code should not work when you upgrade your php because he knows better than you and don't care about what your code does or how simple it is for you to upgrade.

Well if can't upgrade your project overnight you can

downgrade your version of php to whatever version that worked

or...

use a shim (kinda polyfill) such as this https://github.com/dshafik/php7-mysql-shim or https://github.com/dotpointer/mysql-shim then find a place for include_once("choice_shim.php"); somewhere on your code

That will keep your old php code a up and running until you are in a mood to update...

查看更多
妖精总统
4楼-- · 2018-12-31 15:07

Make sure you have not committed a typo as in my case

msyql_fetch_assoc should be mysql

查看更多
泛滥B
5楼-- · 2018-12-31 15:08

mysql_* functions have been removed in PHP 7.

You probably have PHP 7 in XAMPP. You now have two alternatives: MySQLi and PDO.

Additionally, here is a nice wiki page about PDO.

查看更多
公子世无双
6楼-- · 2018-12-31 15:08

You got that error because the mysql_connect function (actually, all mysql_* functions) were removed from PHP 7. You can now use MySQLi or PDO.

Example:

$mysqli = new mysqli($hostname, $username, $password, $database);
查看更多
忆尘夕之涩
7楼-- · 2018-12-31 15:11

mysql_* functions have been removed in PHP 7.

You now have two alternatives: MySQLi and PDO.

The following is a before (-) and after (+) comparison of some common changes to MySQLi, taken straight out of working code:

-if (!$dbLink = mysql_connect($dbHost, $dbUser, $dbPass))
+if (!$dbLink = mysqli_connect($dbHost, $dbUser, $dbPass))

-if (!mysql_select_db($dbName, $dbLink))
+if (!mysqli_select_db($dbLink, $dbName))

-if (!$result = mysql_query($query, $dbLink)) {
+if (!$result = mysqli_query($dbLink, $query)) {

-while ($row = mysql_fetch_array( $result, MYSQL_ASSOC )) {
+while ($row = mysqli_fetch_array( $result, MYSQLI_ASSOC )) {

-mysql_close($dbLink);
+mysqli_close($dbLink);
查看更多
登录 后发表回答