简体   繁体   中英

Fatal error: Uncaught Error: Call to undefined function mysql_connect()

I am a beginner and also a diploma student... please help me solve this error... I tried many online solution but it cant help ... I'm new to php and mysql...

<?php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="slr"; // Database name 
$tbl_name="software"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form 
$soft_name=$_POST['soft_name'];
$installed_date=$_POST['installed_date'];
$expiry_date=$_POST['expiry_date'];
$product_key=$_POST['product_key'];
// Insert data into mysql 
$sql="INSERT INTO $software(soft_name, installed_date, expiry_date, product_key)VALUES('$soft_name', '$installed_date', '$expiry_date', '$product_key')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful". 
if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='CreateData.php'>Back to main page</a>";
} else {
    echo "ERROR";
}
// close connection 
mysql_close();
?>

You should use mysqli_connect instead of mysql_connect which is deprecated since PHP 5.5.0 :

    $link = mysqli_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysqli_select_db($link, $db_name)or die("cannot select DB");

Try This:

Old way :

<?php
    $link = mysql_connect('localhost', 'user', 'pass');
    mysql_select_db('testdb', $link);
    mysql_set_charset('UTF-8', $link);
?>

New way : all you gotta do is create a new PDO object. PDO's constructor takes at most 4 parameters, DSN, username, password, and an array of driver options.

A DSN is basically a string of options that tell PDO which driver to use, and the connection details... You can look up all the options here PDO MYSQL DSN

<?php
    $db=new PDO('mysql:host=localhost;dbname=slr;charset=utf8mb4', 'root', '') or die("Could connect to Database");
?>

According to Here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM