1

I am trying to set up a LAMP server on lubuntu 14.04.5, I erased index.html and replaced it with :

/var/www/html/index.php

Problem is that everything before this <?php tag executes and this seems to crash it. I do not know why, is it because I MUST have index.html?

<?php
$dbhost = "localhost:3306";
$dbuser = "user1";
$dbpass = "pass1";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass);
if(!$connection){
die("connection error " . mysqli_error());
}
echo "connected successfully";
mysqli_close($connection);
?>

So in my php error log I get undefined function mysqli_connect(). How do I make this an available function?

When I do aptitude search php5-mysql an i shows up meaning that it is installed. And there are no other mysql modules except for mysqlnd which I don't think I need since its just native directory and mysqlnd-ms How do I resolve this?

edit: I think I need to add the mysqli extension but having trouble locating it.

5
  • Is this right mysqli_erro(), should it not be mysqli_error()? Aug 23, 2017 at 22:40
  • 1
    Please post the entire file content and also print-screen of the result.
    – pa4080
    Aug 24, 2017 at 11:01
  • Sorry I fixed that mysqli_error(), just a typo Aug 25, 2017 at 20:45
  • @pa4080 entire file is just html stuff like head title body etc. And print-screen is just nothing nill, nada. In view page source all I see is all the code pre the <?php tag. also I have updated the question Aug 25, 2017 at 20:48
  • @DavidFoerster not completely a programming issue since I didn't have php5-mysql installed, but I understand if you think it is. Aug 29, 2017 at 18:34

3 Answers 3

2

Your code has syntax errors right code to use:

<?php
    $dbhost = "localhost:3306";
    $dbuser = "user1";
    $dbpass = "pass1";
    $connection = mysqli_connect($dbhost, $dbuser, $dbpass);
    if(!$connection){
        die("connection error " . mysqli_error());
    }
    echo "connected successfully";
    mysqli_close($connection);
?>

Errors:

  • mysqli_erro() ==> mysqli_error()
  • dbpass ==> $dbpass
6
  • thanks for pointing that out, I hand wrote the code so those errors are only present here. Aug 23, 2017 at 23:04
  • This <?php tag executes and this seems to crash it isn't clear Aug 23, 2017 at 23:06
  • Sorry I mean to say, that my html <title> </title> appears and looking at view page source everything before the <?php tag appears. So I think the php is crashing some how. Aug 24, 2017 at 0:42
  • 1
    Check the error log. This defaults to /var/log/apache2/error.log If php crashes, it will usually note this in the log. Also, for local development, printing warnings and errors may make a lot of sense.
    – vidarlo
    Aug 24, 2017 at 19:09
  • Thank you, so it turns out that it does not know what mysqli_connect is Aug 24, 2017 at 20:30
2

SO what I did was sudo apt-get install php5-mysql Then I was still getting the same webpage so I thought nothing happened, but after checking the /var/log/php5/error.log It turns out the error changed from "undefined command mysqli_connect()" to some code related error like "mysqli_error requires 1 parameter and "object of class mysqli could not be converted to string" and "unknown mysql server 'localhost:3306'". localhost:3306 got changed to just locahost. so after updating my code, everything works fine.

<html>
   <head>
      <title>Connecting MySQL Server</title>
   </head>
   <body>
      <?php
         $dbhost = 'localhost';
         $dbuser = 'user1';
         $dbpass = 'pass1';
         $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
         if(! $conn ) {
            die('Could not connect: ' . mysql_connect_error());
         }
         echo 'Connected successfully';
         mysqli_close($conn);
      ?>
   </body>
</html>
0

You say you did this...

sudo apt-get install php5-mysql

And you never mention doing this, which is also required...

sudo service apache2 restart

Just because you install php5-mysql, doesn't mean Apache has loaded either PHP or the PHP MySQL module.

You can find out for sure what's occurring by creating a phpinfo.php file containing...

<?php
print_r (phpinfo());
?>

Then check + ensure both PHP is loaded + PHP mysql module is loaded.

When in doubt, always check your phpinfo.php file output as your first debugging step.

Many times having this simple output, quickly resolves complex problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.