0

I'm trying to connect to MySQL through PHP.

$dbc = mysqli_connect([ string $host = ini_get("mysqli.default_host")
                        [, string $username = ini_get("mysqli.default_user")
                         [, string $passwd = ini_get("mysqli.default_pw")
                          [, string $dbname = "site"
                           [, int $port = ini_get("mysqli.default_port")
                            [, string $socket = ini_get("mysqli.default_socket") ]]]]]]) OR die('Could not connect to MySQL: ' . mysqli_connect_error() );

I am showing an error for the first line of the code.

5
  • 2
    What's with all the square brackets? Commented Apr 12, 2013 at 19:20
  • 1
    You seem to be trying to run verbatim code from the documentation site. It doesn't work that way. Go through a PHP tutorial first. Commented Apr 12, 2013 at 19:23
  • from what i know, you cannot define datatypes in php. and for sure not in a function call. Commented Apr 12, 2013 at 19:24
  • @Bart Friederichs, Aren't universities great? This is what I have been taught. Are you screaming yet? Commented Apr 12, 2013 at 19:29
  • @dgPehrson I don't scream easily. Saw too much bad code ;). If you have been taught this, give your teacher a PHP tutorial. Commented Apr 12, 2013 at 19:30

4 Answers 4

3

Put variables in, not declarations in square brackets:

$dbc = mysqli_connect(ini_get("mysqli.default_host"),
                      ini_get("mysqli.default_user"),
                      ini_get("mysqli.default_pw"),
                      "site",
                      ini_get("mysqli.default_port"),
                      ini_get("mysqli.default_socket")) 
       or die('Could not connect to MySQL: ' . mysqli_connect_error() )

Also, using or die() makes for very not-robust code. Fetch the error and handle it correctly.

Sign up to request clarification or add additional context in comments.

2 Comments

Really, you can skip all the ini_get() stuff as those are the default values per the function definition.
@Crontab I know. But I have no idea what OP tries to do. So I didn't touch his code too much.
1

You seem to be misunderstanding how to read the manual. The manual states the following:

mysqli::__construct() ([ string $host = ini_get("mysqli.default_host") [, string $username = ini_get("mysqli.default_user") [, string $passwd = ini_get("mysqli.default_pw") [, string $dbname = "" [, int $port = ini_get("mysqli.default_port") [, string $socket = ini_get("mysqli.default_socket") ]]]]]] )

Which is pretty much what you've put in your code. The manual describes what the function expects to be given, it is not what you should copy into your code. What you need to type is simply:

$dbc = mysqli_connect("yourHostName", "yourUserName", "yourPassword", "yourDatabase") OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );

Let's quickly go over how to read the manual. After __construct(), there's a pair of brackets containing a whole load of things (parameters) that you're expected to give it that it requires to run. A string to represent the host, a string to represent the username, and so on.

Parameters wrapped in square brackets are optional - they do not have to be provided, and __construct() will instead use a default value. For example, we can see string $host = ini_get("mysqli.default_host") in the parameter list. If we called mysqli_connect without proving a host ( $dbc = mysqli_connect() ), it would use the default - in this case ini_get("mysqli.default_host") - which would try to get the host name from your php.ini file

Note that if you choose to omit one optional argument, you must omit all those that follow it - you couldn't, for example, try to give it the host and password, but omit the username. We can however (as I've done above) provide the host, username, password and database (must be provided in that order!) and then omit the port and socket.

2 Comments

Thank you Kai. This was very clear for the connection. It did solve my problem only to open a door for more problems but that is the fun of this project. :) Thank you again!
If I can recommend a book, Head First PHP (O'Reilly) I think would really help you out here.
1
$dbc = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

Comments

0

Well at least they are not teaching you mysql_ extensions.. that's a first. Not to throw you to much of a loop and you may not be able to do this in your class, but I would use PDO like so:

<?php #db_connect.inc.php

try {
    $dbh = new PDO('mysql:host=localhost;dbname=dbname', 'user', 'pass');
} catch (PDOException $e) {
    echo $e->getMessage(); //or return $e->getMessage();
}

?>

2 Comments

Yeah, that throws me through a loop. :)
yeah I figured it might.. it did me at first too. Once you get the hang of it, you will never turn back.. lol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.