While changing mysql to mysqli I went from a completely working php file, to one thats not! I followed the instructions for every change, could you help? Here is my code before(mysql) and after (mysqli):
Before(mysql):
<?php
function db_open() {
$dtbhost=":/something/home/something2/mysql/run/mysql.sock";
$dtbuser="root";
$dtbpass="mypass";
$dtbname="mydb";
$connection = mysql_connect("$dtbhost", "$dtbuser", "$dtbpass")
or die('00: No Connection');
@ mysql_select_db("$dtbname") or die('01: No Database');
}
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('You did not complete all of the required fields')
window.location.href='index.html'
</SCRIPT>");
}
else
{
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
db_open();
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$query = mysql_query("SELECT * FROM login_users WHERE username = '$username' AND password = '$password'")
or die(mysql_error());
$rows = mysql_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$username; // Initializing Session
header("location: profile.html"); // Redirecting To Other Page
} else {
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Wrong username password combination. Please re-enter.')
window.location.href='index.html'
</SCRIPT>");
}
mysql_close($connection); // Closing Connection
}
}
?>
After(mysqli):
<?php
function db_open() {
$dtbhost=":/something/home/something2/mysql/run/mysql.sock";
$dtbuser="root";
$dtbpass="mypass";
$dtbname="mydb";
$con = mysqli_connect("$dtbhost","$dtbuser","$dtbpass","$dtbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
}
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('You did not complete all of the required fields')
window.location.href='index.html'
</SCRIPT>");
}
else
{
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
db_open();
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($con, $username);
$password = mysqli_real_escape_string($con, $password);
$query = mysqli_query($con, "SELECT * FROM login_users WHERE username = '$username' AND password = '$password'")
or die(mysqli_error());
$rows = mysqli_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$username; // Initializing Session
header("location: profile.html"); // Redirecting To Other Page
} else {
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Wrong username password combination. Please re-enter.')
window.location.href='index.html'
</SCRIPT>");
}
mysqli_close($con); // Closing Connection
}
}
?>
The errors I get are:
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: hostname nor servname provided, or not known in /something/home/something2/public_html/login.php on line 8
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: hostname nor servname provided, or not known in /something/home/something2/public_html/login.php on line 8 Failed to connect to MySQL: php_network_getaddresses: getaddrinfo failed: hostname nor servname provided, or not known
( line 8 is: $con = mysqli_connect("$dtbhost","$dtbuser","$dtbpass","$dtbname");
)
Which then trigger more errors because $con is empty. Any help would be appreciated, thanks!