0

I am new to php.I have a php file that tries to connect to an sql database.The database name is student.

index.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Available</title>
    </head>
    <body>
    <?php 
        $host="127.0.0.1";
        $port=3306;
        $socket="";
        $user="root";
        $password="aPassword";
        $dbname="student";

        $con = new mysqli($host, $user, $password, $dbname, $port, $socket)
            or die ('Could not connect to the database server' . mysqli_connect_error());
        if($con){
            echo "Connection to server  working";
        }
        else{
            echo "You ruin my life";
        }
        //$con->close();

     ?>
    </body>
</html>

However if I try to run this file I get the following error:

Fatal error: in index.php on line 17 Line 17 is this:

$con = new mysqli($host, $user, $password, $dbname, $port, $socket)
                or die ('Could not connect to the database server' . mysqli_connect_error());

Any Ideas? I forgot to mention that the code is generated by MySql workbench This is the exact error:

Fatal error: in C:\Users\manos_000\Desktop\���� �������\sql scripts\website\index.php on line 17
Manos Kounelakis
  • 2,848
  • 5
  • 31
  • 55
  • 3
    you can usually leave out $port, $socket –  Feb 26 '17 at 22:10
  • Pls do not add irrelevant tags to your question. Your code and the error has nothing to do with mysql workbench, nor with udf. – Shadow Feb 26 '17 at 22:11
  • Can you explain why downvoting? – Manos Kounelakis Feb 26 '17 at 22:12
  • 1
    Is php_mysqli extension turned on in php.ini ? – Uours Feb 26 '17 at 22:13
  • how can i check that? – Manos Kounelakis Feb 26 '17 at 22:13
  • 1
    Pls also share the exact error message with us, not just that it was fatal. – Shadow Feb 26 '17 at 22:16
  • 1
    I'd just add that your if/else block is redundant. You're already basically doing that with your 'or die' condition. – ThomasEllis Feb 26 '17 at 22:20
  • I don't use mysql, but you will usually not get a false returned on object instantiation so both your 'or die' and your 'if/else' checks are invalid. Try following Example #1 posted on http://php.net/manual/en/mysqli.construct.php for proper instantiation and error checking. – ThomasEllis Feb 26 '17 at 22:25
  • @ThomasEllis I tried that to with no luck – Manos Kounelakis Feb 26 '17 at 22:28
  • I'm with @Uours. Then you likely need to check if the extension is even enabled. Would need to know what stack you're running. WAMP? LAMP? – ThomasEllis Feb 26 '17 at 22:30
  • @ThomasEllis I have just used the `php -S 127.0.0.1:80` command – Manos Kounelakis Feb 26 '17 at 22:31
  • Use phpinfo() in a script like this : `` and look for mysqli . If it shows up then you have the extension . – Uours Feb 26 '17 at 22:32
  • @Uours I can not find mysqli.How can I install it? – Manos Kounelakis Feb 26 '17 at 22:34
  • It depends on which operating system you are using . Usually it is there and you would just turn it on . You would do this by un-commenting the related line in the php.ini file . – Uours Feb 26 '17 at 22:38
  • 1
    Run this in a script `` and look for **Loaded Configuration File** . That would give you the path of the php.ini . Open that file in a text editor and look for mysqli . Always keep a backup of the original file just in case the modified file creates any issues . Furthermore considering you are new to PHP , you could just use the deprecated mysql version to play around . But I would strongly recommend to use MySQLi or PDO after you know how to Install/Turn-ON the required extension . – Uours Feb 26 '17 at 22:44
  • @Uours it says none – Manos Kounelakis Feb 26 '17 at 22:48

0 Answers0