1
votes

I am trying to do a connection with XAMPP and sql server. but when ever 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\Maxipak\process.php:3 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Maxipak\process.php on line 3

My code on line 3 looks like this:

$conn = mysql_connect ("localhost","root","");
4
mysql_* functions are deprecated use mysqli_* instead so then you will have no problems..! - Umair Shah Yousafzai
And removed from PHP7. - Jonnix
Ahaa..then if you are using XAMPP with PHP7 so that's why you are getting the undefined function error then..! - Umair Shah Yousafzai

4 Answers

6
votes

Here is a proper way to do it :

Note : As mysql_* functions are deprecated and being removed since PHP 7 so if you are using XAMPP with PHP 7 so then that's why you are getting the undefined function error and instead you should use mysqli_* as it is very easy to use.

HOW TO CREATE A DATABASE CONNECTION USING MYSQLi :

<?php
/* Database config */
$db_host        = '';
$db_user        = '';
$db_pass        = '';
$db_database    = ''; 
/* End config */
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_database);
/* check connection */
if (mysqli_connect_errno()) {printf("Connect failed: %s\n", mysqli_connect_error());}
?>

Note : Just insert your database details and you will be good to go..!

1
votes

You do not have lib mysql activated in your php.ini. But you shouldn't use mysql_ because it is deprecated. You should use mysqli_ or PDO

1
votes

As suggested in a comment, if you're using XAMPP with PHP7, mysql_* functions have been removed.

Here are recommended alternatives to connect to a database:

MYSQLI

mysqli_connect("dbhost", "dbuser", "dbpassword", "db");

PDO

$stmt = new PDO('mysql:host=dbhost;dbname=dbname', 'dbuser', 'dbpass');
0
votes

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs

This problem is not to supported php version on your coding. if you want fix. First and fast change your xampp version. don't change your setting and etc.

example: xampp Version 5.6... (Best Choice)