0

im creating a small webservice and i need set some restfull api.

The problem is that i set the connection with db with a method inside Database class like this:

        public function connect() {
            echo "Starting connection\n";
            // Create connection
             $conn = new mysqli($this->db_name, $this->username, $this->password);

            // Check connection
            if (!$conn) {
                die("Database connection failed: " . $conn->mysqli_connect_error());
            }
             echo "Connected successfully";
             return $conn;
        }

On my api method for city model i do this:

    $database = new Database();
    $db = $database->connect();
    $result = $city->get();

    // If db get connected
    if($db) {
      $city = new City($db);
      $result = $city->get();

      // If query go well
      if($result) {
       // lot of code is done here
      else {
         echo connection failed with DB";
       }

On broswer i get this messages:

Creating DB handler (Constructor of Database object)

Starting connection (Connect method called in API)

Connected successfully (Connection success)

Query not executable (in City models query failed for some reason do not know why)

No connection made with DB! (Connection now fail when in API i check it again before shown data)

I didn't post all file, if it's needed i can do it

Dak28
  • 259
  • 1
  • 8
  • 1
    It is a very bad idea to use `die(mysqli_connect_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) On top of that `$conn->mysqli_connect_error()` is an error as such method doesn't exist. – Dharman Oct 27 '19 at 00:18
  • actually i could just retrive false there, because if connection failed i just need that the conn propriety is false to prevent any api call. but i don't think is that the problem why my query failed and connection also. I don't know if can help but i try remove the settings parameters from Database class and still connection come successfully, how is that even possible? – Dak28 Oct 27 '19 at 00:22
  • It will never be false. Your if statement to check for successful connection will never work. You need to enable error reporting for mysqli. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Oct 27 '19 at 00:23
  • Ok now errors are actually helps me a lot understand all things messed up, now i think i just need fix this: ```No database selected in``` The query is like this: ```$query = "SELECT * FROM city"; $result = mysqli_query($db, $query) or trigger_error(mysqli_error($db));``` – Dak28 Oct 27 '19 at 00:41
  • While some improvement, I would still recommend to use automatic error reporting instead. Just add `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` before `$conn = new mysqli` – Dharman Oct 27 '19 at 00:42
  • To solve your problem with unselected DB, just pass the name of the DB as the fourth parameter to `new mysqli` – Dharman Oct 27 '19 at 00:43
  • i try before but i was putting them inside class propriety instead of method, not it works, error is pretty readable now: ```mysqli_query(Object(mysqli), 'SELECT * FROM C...')``` – Dak28 Oct 27 '19 at 00:44
  • 1
    im already send it i guess ``` $conn = new mysqli($this->db_name, $this->username, $this->password);``` BUT I Delete the host.... ``` $conn = new mysqli($this->host, $this->username, $this->password, $this->db_name);``` Now works, thanks so much! also the order was wrong, was like never working without make the errors print something more readable – Dak28 Oct 27 '19 at 00:49
  • 1
    Possible duplicate of [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Oct 27 '19 at 00:56

0 Answers0