1

I created a class to access my database. The simplified class is following (I named it dbaccess.php)

class dbaccess {
function read($db) {
    $con = mysqli_connect($db);
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
    $result = mysqli_query($con,"SELECT * FROM equipment");
    while($row = mysqli_fetch_array($result)){
        $print = $print . $row['ID'] . " " . $row['name'] . " " . $row['new_price'] . " " . $row['residual_value'] . "<br>";
        }
    echo $print;
    mysqli_close($con);
    }
}

To access the class, I use this code

include './dbaccess.php';
//define db address
$add = '"localhost","myuser","mypassword","mydbname"';
$db = new dbaccess;
$db->read($add);

This code resulting

Failed to connect to MySQL: Unknown MySQL server host '"localhost","myuser","mypassword","mydbname"'(2)

I don't know how to fix it, can anyone here help me?

3
  • 3
    You need to pass each value in as a separate parameter, not as string of parameters. Commented Aug 5, 2013 at 16:01
  • it is clear but I don't think I can't define all parameter in one string Commented Aug 5, 2013 at 16:04
  • that is correct ... you cannot. you have to have 4 different arguments for your read method, $host, $user, $password, $db, when you call mysqli_connect pass each of these arguments in separately. Currently you are passing a single string argument in so it's trying to connect to a database on "localhost","myuser","mypassword","mydbname" Commented Aug 5, 2013 at 16:06

4 Answers 4

4

You're passing a single string to mysqli_connect. You need to pass "localhost", "myuser",... as separate variables.

http://php.net/manual/en/function.mysqli-connect.php

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

Comments

0

I have something similar with yours, download my files from dropbox and have a look inside DB Connect

1 Comment

Great, you did it three years ago. Thank you.
0
class dbaccess {
    function read($db) {
        $con = mysqli_connect($server, $user, $password, $dbname);
(...)

Then in your code, you should divide the parameters.

include './dbaccess.php';
//define db address
$db = new dbaccess;
$db->read("localhost","myuser","mypassword","mydbname");

REF

Comments

0

It's probably a better idea to store your host, username, password and database in separate variables like this:

$host = "localhost";
$user = "myuser";
$pass = "mypassword";
$data = "mydbname";
$db = new dbaccess(); // <-- It's good practice to use parentheses in the constructor statement.
$db->read($host,$user,$pass,$data);

And then the implementation of your dbaccess class could be more like this:

$con = mysqli_connect($host,$user,$pass,$data);

This is because the mysqli_connect function takes the host, username, etc. as separate parameters, not as a single string.

Comments

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.