-1

I just started learning a little about PHP and sessions (using XAMPP to run PHP scripts).

I was watching a few vidoes and reading a few books before I started.

The thing I have here is from a video that I saw about PHP and sessions to get me going. I have a little problem here that the person in the video did not have.

This is my login.php code:

<?php
session_start();      // Starting Session
$error = '';          // Variable To Store Error Message

if (isset($_POST['submit'])) {
    if (empty($_POST['username']) || empty($_POST['password'])) {
        $error = "Brukernavn eller passord er ugyldig!";
    } 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
        $connection = mysql_connect("localhost", "root", "");

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

        // Selecting Database
        $db = mysql_select_db("logintest", $connection);

        // SQL query to fetch information of registerd users and finds user match.
        $query = mysql_query("select * from users where password='$password' AND username='$username'", $connection);

        $rows = mysql_num_rows($query);
        if ($rows == 1) {
            $_SESSION['login_user']=$username; // Initializing Session
            header("location: profile.php"); // Redirecting To Other Page
        } else {
            $error = "Brukernavn eller passord er ugyldig!";
        }
        mysql_close($connection); // Closing Connection
    }
}
?>

When i now try to log in and with the right username i get this error:

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in
C:\xampp\htdocs\gauldaldesign\login.php:14 Stack trace: #0
C:\xampp\htdocs\gauldaldesign\index.php(2): include() #1 {main} thrown in
C:\xampp\htdocs\gauldaldesign\login.php on line 14

Anyone that can explain what this means and maybe what the problem can be? Maybe not the answer, but at least point me in the right direction so that I can learn from my mistakes.

Cœur
  • 34,719
  • 24
  • 185
  • 251
  • Whatever you do, **do not use the mysql** functions as they all pose a known security risk to your app. Use an alternative such as `mysqli` (note the **i** or PDO). If you take a look at the official documentation you'll see the same warning: http://php.net/manual/en/function.mysql-connect.php - I would also suggest using a more recent version of PHP if you're starting out, e.g. 5.6 onwards or 7+. – Optimae Apr 14 '17 at 17:48
  • The mysql API is no longer available in PHP 7. You should be using mysqli or PDO anyway. – John Conde Apr 14 '17 at 17:50
  • Actually, which version of PHP are you using @learningwebdev? If your version is higher than `5.5.0` then that will explain why you can't use `mysql_connect()` (which you shouldn't use even if you could). – Optimae Apr 14 '17 at 17:50
  • http://stackoverflow.com/questions/13825108/undefined-function-mysql-connect – Andrew Apr 14 '17 at 17:50

1 Answers1

1

You're using php 7 , & in the videos you watched php 5 was used.

Switch to PHP 5 in XAMPP , or better use mysqli_* functions , or even better, use PDO & prepared statements.

See: http://php.net/manual/en/mysqlinfo.api.choosing.php

Hossam
  • 1,105
  • 8
  • 19