0
votes

Good day. Im trying to make a login form using php and xampp to run the code. I already have a database in phpMyAdmin and I get this error

Connected!

Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\login\connection.php:34 Stack trace: #0 {main} thrown in C:\xampp\htdocs\login\connection.php on line 34

everytime I execute this code

<?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");

}


?>

I tried to search on youtube on how to fix the error but they're only telling that the version of my php is outdated/updated. I'm confused.

1
Don't call mysql_ functions when you use mysqli - Scuzzy
Possible duplicate of Can I mix MySQL APIs in PHP? - Scuzzy
It is outdated. mysqli replaced mysql. For your purposes, you can just do a search and replace. - Sablefoste
Ultimately your PHP installation hasn't been built with mysql extension, it is possible to have both available at the same time, in this instance, you don't. - Scuzzy
And by the way, please never, ever store a plain text password in a database (as shown in your code above). This may be a test program, but as a new programmer, this is a very big deal. - Sablefoste

1 Answers

0
votes

If you have set the correct connection to your database....

$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");

    }