Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

mysql - Using MySQLi from another class in PHP

I really hope someone can help me figure out what I am missing. I have upgraded my installation from PHP 5.6 to 7.0 and this has forced me to update from Mysql to Mysqli which for some reason has broken my setup.

I researched and followed this guide "Using MySQLi in other classes": Using MySQLi in other classes

I am writing as a last resort and have looked at other sites as well but it seems like the problem comes some where else from.

First I have a database class:

    private $serverName = "localhost";
    private $userName = "DBUserName";
    private $pass = "UserPassword";
    private $database = "SelectedDB";

    public $conn;


    public function __construct(){

             $this->conn = new mysqli($this->serverName, $this->userName,$this->pass,$this->database);


            }

Then I have an API class where I want to access this connection which looks like

require_once 'Database.php';
class MyAPI{
private $db;
public function __construct($request_uri, $postData, $origin) {



    $this->db = new Database();


}

and lastly i try to call it from a function:

$getUserResult = mysqli_query( $this->db->conn, $getUserQry);

When ever I call $this->db->conn I get an internal server error 500

If I create the database connection in the MyAPI class there is no issues which seems odd to me.

I hope someone can point me in a direction.

UPDATE: I corrected a spelling error in the script and now I get 200 but the value still continues to be null from the query mysqli_query.

If i create the $dbtest = new database(); and use that instead it works fine. Is there someway to get it to work inside the constructor with the reference to $db?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are several bad practices that led you to this error.

Clearly, extending User from a Database is a wrong move. Also, the whole Database class is rather useless as it doesn't do anything useful.

Hence I would suggest to

  • get rid of the useless Database class.
  • create a single $db instance from vanilla mysqli.
  • pass it as a constructor parameter into every class that needs a database connection

database.php:

<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$db = new mysqli("localhost", "DBUserName", "UserPassword", "SelectedDB");
$db->set_charset('utf8mb4');

myapi.php

<?php
class MyAPI
{
    protected $db;

    public function __construct($db, $request_uri, $postData, $origin)
    {
        $this->db = $db;
    }

    public function getUser($id)
    {
        $sql = "SELECT * FROM users where id=?";
        $stmt = $this->db->prepate($sql);
        $stmt->bind_param("s", $id);
        $stmt->execute();
        $result = $stmt->get_result();
        return $result->fetch_assoc();
    }
}

app.php

<?php
# require_once 'Database.php';
# require_once 'myapi.php';
require 'vendor/autoload.php'; // autoloading is a must

$api = new MyAPI($db, $request_uri, $postData, $origin);
$user = $api->getUser($_POST['id']);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...