美好的一天。我试图使用php和xampp来创建一个登录表单来运行代码。我已经在phpMyAdmin中有一个数据库,我收到此错误
连接!
致命错误:未捕获错误:在C:\ xampp \ htdocs \ login \ connection.php中调用未定义函数mysql_query():34堆栈跟踪:在C:\ xampp \ htdocs \ login \中抛出#0 {main}第34行的connection.php
每次执行此代码
<?php
$host = 'localhost';
$uname = 'root';
$pword = '';
$db = 'login';
$port = '3306';
$link = mysqli_init();
$success = mysqli_real_connect(
$link,
$host,
$uname,
$pword,
$db,
$port
);
if(isset($_POST['user']))
{
$username = $_POST['user'];
}
if(isset($_POST['pass']))
{
$password = $_POST['pass'];
}
$con = new mysqli($host, $uname, $pword, $db) or die("Connection failed". mysqli_error());
echo ("Connected!");
$result = mysql_query($success. "select * from db_login where username = $username and password = $password");
$row = mysql_fetch_array($result);
if($row['username'] == $username && $row['password'] == $password )
{
echo("Welcome!". $row['username']);
}
else
{
echo("Login failed");
}
?>
我尝试在youtube上搜索如何修复错误,但他们只是告诉我的php版本已过时/更新。我很困惑。
答案 0 :(得分:0)
如果您已设置与数据库的正确连接....
$host = 'localhost';
$uname = 'root';
$pword = '';
$db = 'login';
$success = mysqli_connect($host,$uname,$pword,$db);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['user']))
{
$Username = $_POST['user'];
}
if(isset($_POST['pass']))
{
$Password = $_POST['pass'];
}
// removes backslashes
$username = stripslashes($Username);
//escapes special characters in a string
$username = mysqli_real_escape_string($success,$username);
$password = stripslashes($Password);
$password = mysqli_real_escape_string($success,$password);
$result = mysqli_query($success. "select * from db_login where username = $username and password = $password") or die(mysql_error());
$row = mysqli_num_rows($result);
if($rows==1){
echo("Welcome!". $row['username']);
}
else
{
echo("Login failed");
}