1

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","");
The Codesee
  • 3,628
  • 3
  • 32
  • 71
Nadine Duke
  • 13
  • 1
  • 2
  • 4

4 Answers4

6

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..!

Umair Shah Yousafzai
  • 2,300
  • 2
  • 24
  • 48
1

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

nospor
  • 4,150
  • 1
  • 14
  • 25
1

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');
The Codesee
  • 3,628
  • 3
  • 32
  • 71
  • It seems like with `PDO` everything changes quite a lot but with `MYSQLi` things changes very little or am I wrong? so from `MYSQL` adopting `MYSQLi` than `PDO` is rather quite much easy or isn't it?? – Umair Shah Yousafzai May 22 '16 at 10:37
  • You're correct, however `PDO` is a better practice for the long run as it can help protect against SQL Injection Attacks. – The Codesee May 22 '16 at 10:39
  • Well...You are also correct but with use of prepared statements in `mysqli` you can pretty much prevent SQL Injection Attacks or isn't it?? – Umair Shah Yousafzai May 22 '16 at 10:40
  • @UmairShahYousafzai That's true, however I believe it's up to personal preference.. I prefer to use `PDO` without `mysqli`. – The Codesee May 22 '16 at 10:42
  • @As you wish friend..I was just saying..! you know that it's far easy to use `mysqli` :D – Umair Shah Yousafzai May 22 '16 at 10:43
0

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)