-1

Hi Stackoverflow Community, I'm pretty new so yea, this is my first question and post.

I try right now to do a more or less login page on my php site, its not much of a deal with a database. i get this error when i want to visit my site -f /var/log/apache2/error.log

"Error: Call to undefined function mysqli_connect() in /var/www/html/upload.php:7\nStack trace:\n#0 {main}\n thrown in /var/www/html/upload.php on line 7"

PHP Version: PHP 7.2.24-0ubuntu0.18.04.2 (cli) (built: Jan 13 2020 18:39:59) ( NTS )

Full Code (from the PHP part):

<?php
$host="localhost";
$user="root";
$password="";
$db="demo";

$a=mysqli_connect('localhost','root','','demo');
mysqli_select_db($a,$db);

if(isset($_POST['username'])){

$uname=$_POST['username'];
$password=$_POST['password'];

    $sql="select * from demo where User='".$uname."'AND Pass='".$password."' limit 1";
    $result=mysqli_query($a, $sql);

    if(mysqli_num_rows($result)!==0){
        echo " You Have Successfully Logged in";
        exit();
    }
    else{
        echo " You Have Entered Incorrect Password";
        exit();
    }}
?>
Shadow
  • 33,525
  • 10
  • 51
  • 64
kaiwen_
  • 11
  • 3
    Does this answer your question? [Fatal error: Call to undefined function mysqli\_connect()](https://stackoverflow.com/questions/25281467/fatal-error-call-to-undefined-function-mysqli-connect) – Mat Sz Feb 08 '20 at 22:28
  • `sudo apt install php-mysqli` – jeprubio Feb 08 '20 at 22:35
  • @jeprubio thanks for the reply, sadly I installed it but it still doesn't work [link](http://puu.sh/F8zN1/ad7e14f014.png) – kaiwen_ Feb 08 '20 at 22:40
  • @MatSz thanks for the reply, I tried the variety of solutions. The Problem is, I use a Ubuntu Server 1804 LTS, not a Windows Machine with XAMPP! And installing mysqli didn't help me too – kaiwen_ Feb 08 '20 at 22:42
  • 1. Your query is not secure/stable because you are writing user supplied data directly into your query. Use a prepared statement. 2. I sincerely hope you are not storing unencrypted passwords in your database. 3. Why declare your target database twice? 4. Your User column should be unique, so LIMIT should be unnecessary. 5. It is good practice to use ALL CAPS for mysql keywords and functions. – mickmackusa Feb 09 '20 at 10:04
  • **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Feb 09 '20 at 10:39
  • @Dharman thanks for your warning messages, im still more or less new in php and mysql databases, hard to understand what you mean :) This is just a test site for a workshop i need to do in my class so its more or less irrelevant, to go with safety reasons – kaiwen_ Feb 09 '20 at 13:45
  • It's not about safety. It's about what you learn. You should not be learning to do it this way, there is much easier and better way as suggested in the links. Never write SQL like `User='".$uname."'AND Pass='".$password."'`. Use parameter binding with prepared statements. If you are only starting I recommend to learn PDO instead of mysqli. PDO is simpler and mysqli should not be used in new projects. Here is a good website explaining both: https://phpdelusions.net/ – Dharman Feb 09 '20 at 13:57

1 Answers1

-1

first of all, I want to thank anyone who replied or just visited the thread by itself.

I don't exactly know what was the main issue here, but I assume it was due to the fact mysqli wasn't loaded into apache2.

Solution:

sudo apt-get install PHP-mysqli
service apache2 restart

I wish you all a wonderful day/night!

kaiwen_
  • 11