从 MySQL 更改为 MySQLi,不再工作
Changed from MySQL to MySQLi, not working anymore
在将 mysql 更改为 mysqli 时,我从一个完全可用的 php 文件变成了一个不可用的文件!我按照说明进行了每一次更改,你能帮忙吗?这是我之前(mysql)和之后(mysqli)的代码:
之前(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
}
}
?>
之后(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
}
}
?>
我得到的错误是:
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
(第 8 行是:$con = mysqli_connect("$dtbhost","$dtbuser","$dtbpass","$dtbname");
)
然后触发更多错误,因为 $con 是空的。
任何帮助将不胜感激,谢谢!
$dtbhost 变量应该是IP地址或域名。例如:'localhost'、'127.0.0.1'、'example.com' 等。不是套接字文件。
在将 mysql 更改为 mysqli 时,我从一个完全可用的 php 文件变成了一个不可用的文件!我按照说明进行了每一次更改,你能帮忙吗?这是我之前(mysql)和之后(mysqli)的代码:
之前(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
}
}
?>
之后(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
}
}
?>
我得到的错误是:
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
(第 8 行是:$con = mysqli_connect("$dtbhost","$dtbuser","$dtbpass","$dtbname");
)
然后触发更多错误,因为 $con 是空的。 任何帮助将不胜感激,谢谢!
$dtbhost 变量应该是IP地址或域名。例如:'localhost'、'127.0.0.1'、'example.com' 等。不是套接字文件。