0

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.

  • Don't call `mysql_` functions when you use `mysqli` – Scuzzy Sep 06 '17 at 03:34
  • Possible duplicate of [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – Scuzzy Sep 06 '17 at 03:35
  • It *is* outdated. `mysqli` replaced `mysql`. For your purposes, you can just do a search and replace. – Sablefoste Sep 06 '17 at 03:35
  • 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 Sep 06 '17 at 03:36
  • 1
    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 Sep 06 '17 at 03:37
  • so what should I use then? mysqli or mysql? – Yancie Atendido Sep 06 '17 at 04:12

1 Answers1

0

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

    }
pedram shabani
  • 1,614
  • 2
  • 19
  • 28