1

I am trying to connect to my database, but it shows me error in the mysql_connect function.

The error is: Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\Connect.php:12 Stack trace: #0 C:\xampp\htdocs\Test.php(3): require() #1 {main} thrown in C:\xampp\htdocs\Connect.php on line 12

The Connect file:

<?php  

$db_host = "localhost"; 
// Place the username for the MySQL database here 
$db_username = "root";  
// Place the password for the MySQL database here 
$db_pass = "";  
// Place the name for the MySQL database here 
$db_name = "oscar"; 

// Run the connection here  
$con = mysql_connect("db_host","$db_username","$db_pass");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("$db_name", $con);
try 
{
 $conn = new PDO("mysql:host=$db_host;dbname=$db_name", $db_username, $db_pass);
// set the PDO error mode to exception
 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 echo "Connected successfully"; 
}
catch(PDOException $e)
{
 echo "Connection failed: " . $e->getMessage();
}

?>

The text file:

<?php
// Connect to the MySQL database  
require "Connect.php"; 
echo "Success";
?>
TheGrimBoo
  • 199
  • 1
  • 4
  • 14

4 Answers4

3

why are you using mysql_connect and even PDO at the same time ? and mysql is deprecated so and vulnerable to sql injection.

only this code will connect to your database

<?php
$db_host = "localhost";
// Place the username for the MySQL database here
$db_username = "root";
// Place the password for the MySQL database here
$db_pass = "";
// Place the name for the MySQL database here
$db_name = "oscar";


try {
    $conn = new PDO("mysql:host=$db_host;dbname=$db_name", $db_username, $db_pass);
// set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
Exprator
  • 26,992
  • 6
  • 47
  • 59
0

the function mysql_connect is a deprecated function. Instead you should be using mysqli_connect read more about this here

The following code should work:

<?php
/**
 * Created by PhpStorm.
 * User: ...
 * Date: 5-12-2017
 * Time: 09:47
 * Database connection.
 */
?>
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'admin');
define('DB_PASSWORD', 'admin');
define('DB_DATABASE', 'your_database');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Deathstorm
  • 818
  • 11
  • 36
0

mysql function was deprecated in php 5.5.0. I have changed some changes in your code. $ symbol was added in mysqli connect statement.

$con = mysqli_connect("$db_host","$db_username","$db_pass");
if (!$con)
{
  die('Could not connect: ' . mysqli_error());
}
mysqli_select_db("$db_name", $con);
Muralitharan
  • 367
  • 1
  • 2
  • 11
0

Try to use mysqli

<?php
$db_host = "localhost"; 
$db_username = "root"; // Place the username for the MySQL database here 
$db_pass = "";// Place the password for the MySQL database here  
$db_name = "test";// Place the name for the MySQL database here 
$conn = new mysqli($db_host, $db_username, $db_pass,$db_name);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
//echo "Connected successfully";
?>

This works perfectly.

Akila
  • 715
  • 2
  • 8
  • 13