-1

Im trying to connect to the database with a php script but its not working im geting this error: I tried changing the mysql to mysqli but didnt work either im not too sure what to do

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /storage/ssd3/564/6656564/public_html/login.php:8 Stack trace: #0 {main} thrown in /storage/ssd3/564/6656564/public_html/login.php on line 8

Here is my php code

<?php
    // Database Things =========================================================
//These values can be found in the email.
    $host = "localhost"; //<--put your host here
    $user = "******"; //<-- put username here
    $password = "R*****"; //<--put your password here
    $dbname = "id6*****_users"; //<-- put your database name here
    mysql_connect($host, $user, $password) or die("Cant connect into database");
    mysql_select_db($dbname)or die("Cant connect into database");   
    // =============================================================================
    $Act = $_GET["Act"];// what is action, Login or Register?
    $nick = $_GET["User"];
    $pass = $_GET["Pass"];   
    $Email = $_GET["Email"];    
    if($Act == "Login"){
    if(!$nick || !$pass) {
        echo "Login or password cant be empty.";
        } else {

                 $SQL = "SELECT * FROM Users WHERE Username = '" . $nick . "'";
                $result_id = @mysql_query($SQL) or die("DB ERROR");
                $total = mysql_num_rows($result_id);
                    if($total) {
                        $datas = @mysql_fetch_array($result_id);
                            if(!strcmp($pass, $datas["Password"])) {
                                echo "Correct";
                            } else {
                                echo "Wrong";
                            }
                    } else {
                        echo "No User";
                }

    }
    }
   if($Act == "Register"){

        $checkuser = mysql_query("SELECT Username FROM Users WHERE Username='$nick'"); 
        $username_exist = mysql_num_rows($checkuser);
        if($username_exist > 0)
        {
              echo "TAKEN";// Username is taken

              unset($nick);
              exit();
        }else{
            $query = "INSERT INTO Users (Username, Password,Email) VALUES('$nick', '$pass', '$Email')";
            mysql_query($query) or die("ERROR");
            mysql_close();
            echo "Registered";
        }
    }
    // Close mySQL Connection
    mysql_close();
    ?>

If someone could please help me that would be awesome

eisbehr
  • 11,819
  • 7
  • 35
  • 61
  • what is your php version? – Peter Aug 01 '18 at 08:03
  • 2
    Reading the documentation would be a good starting point: http://php.net/manual/en/function.mysql-connect.php Pay special attention to the red things. – KIKO Software Aug 01 '18 at 08:05
  • 3
    "_I tried changing the mysql to mysqli_" That's a great idea since `mysql_*` functions are deprecated – brombeer Aug 01 '18 at 08:06
  • 1
    @kerbholz Not if you just insert an "i" everywhere. – KIKO Software Aug 01 '18 at 08:07
  • @KIKOSoftware Agreed – brombeer Aug 01 '18 at 08:08
  • 1
    @Dalton use PDO because mysql_* methods are deprecated. – Peter Aug 01 '18 at 08:10
  • as mentioned you can't simply swap the mysql_ commands for mysqli_ commands using find/replace. Some of the function arguments are different, some of the function names are different, some of the behaviour is different. Plan a proper project to upgrade your code. What you're using now is obsolete and was removed when PHP7 was released. If you need a temporary solution while you work on this conversion, switch back to using PHP5 or use a shim such as https://github.com/dshafik/php7-mysql-shim (not recommended, still vulnerable to injections etc, but ok as a temporary hack) – ADyson Aug 01 '18 at 08:14

1 Answers1

-1

If You want connect to local database use this variables

$host     = "localhost"; //<--put your host here 
$user     = ""; //<--put your db username here
$password = ""; //<--put your db password here
$dbname   = put your database name here in string
mysql_connect($host, $user, $password) or die("Cant connect into database");
mysql_select_db($dbname)or die("Cant connect into database"); 

if you want connect to your site with sever database then use this

  1. Login to your Cpanel
  2. After login Go to Mysql&Databses
  3. Create Your database ( databse_name)
  4. next create a user ( enter your db_user, db_password )
  5. Connect a user to your database ( select from dropdown )

    $host = "localhost"; //<--put your host here $user = $db_user; $password = $db_password ;
    $dbname = $databse_name; mysql_connect($host, $user, $password) or die("Cant connect into database"); mysql_select_db($dbname)or die("Cant connect into database");

  • `Fatal error: Uncaught Error: Call to undefined function mysql_connect()` btw. – kry Aug 01 '18 at 10:07
  • There isn't even a `$user` in your code. And seriously, read the question before posting an answer. – kry Aug 01 '18 at 10:08
  • how is any of this going to help when the function doesn't even exist, as the reported error message makes very clear? Also, even in the case where it was available please don't recommend people to use obsolete, insecure code such as this :-( . Better alternatives have been available for many many years now, and this code no longer exists at all in the latest versions of PHP. I really hope you are not still using anything like this yourself in your applications. – ADyson Aug 01 '18 at 13:40