PHP中的OO和mysqli

时间:2015-02-21 11:27:28

标签: php mysql oop mysqli

我已经定义了我的连接文件

文件名= connection.php

<?php
$host = "localhost";
$database = "projectp_peas";
$usr= "projectp_admin";
$pass = "9613341694";
$conn = mysqli_connect($host, $usr, $pass) or die (mysqli_error($conn));
?>

我的联系还可以。 ()

现在我的头等舱: 文件名product.php

<?php
class Product{

public $PRODUCT;
public $SHORT_DESC;
public $LONG_DESC;

public function showProduct($id){
include_once("conection.php");

mysqli_select_db($conn, $database);
$products = "SELECT * FROM PRODUCTS WHERE ID = '$id' ";
$rs_products = mysqli_query($conn, $products) or die (mysqli_error($conn));
$product = mysqli_fetch_assoc($rs_products);

echo "<h3 style=\"font-size:3em; color:#BBBBBB;\"> ";
echo $product['PRODUCT'] ."</h3>"; 
echo "<h5> ";
echo $product['SHORT_DESC'] ."</h5>"; 

echo $product['LONG_DESC']; 
}

}
?>

现在我的index.php档案

<div class ="px_70">
<?php
 require_once('../script/product.php');
 $product = new Product;
 $product->showProduct(1);
?>
</div>

我的查询到目前为止运行良好。

但我的问题始于另一堂课。另一类是这样的:

文件名:clients.php

<?php
class User{
public function TopMenu(){
include_once("conection.php");
session_start();

if(isset($_SESSION['EMAIL'])){
$MAIL = $_SESSION['EMAIL'];

mysqli_select_db($conn, $database);
$username= "SELECT * FROM CLIENTS WHERE EMAIL = '$MAIL' ";
$USERNAME_RS = mysqli_query($conn, $username) or die(mysqli_error($conn));
$USR = mysqli_fetch_assoc($USERNAME_RS);

if(mysqli_num_rows($USERNAME_RS)>0){
echo $USR['C_NAME'] . " | <a href=\"../global/logout.php\">Log out </a> | <a href=\"UserPapers.php\"> Papers &nbsp; &nbsp;</a>" ;
}

else{
echo " <a href=\"../login.php\">Login</a> | <a href=\"../Register.php\">Register &nbsp; &nbsp;</a>" ;
}
}

else{
echo " <a href=\"../login.php\">Login</a> | <a href=\"../Register.php\">Register &nbsp; &nbsp;</a>" ;
}
}
}
?>

再次在index.php档案中:

<div class="tm">
<?php
include_once('../script/clients.php');
$top = new User;
$top->TopMenu();
?>
</div>

为什么我会收到以下错误?

Warning: mysqli_select_db() expects parameter 1 to be mysqli, null given in /home/projectp/public_html/script/product.php on line 11

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/projectp/public_html/script/product.php on line 13

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in /home/projectp/public_html/script/product.php on line 13

文件中的TopMenu()函数不应导致任何错误。因为当我删除Product类时,此函数正常工作。 谁能告诉我,我在哪里犯错误?我是php OO和mysqli的新手。

2 个答案:

答案 0 :(得分:2)

您没有在函数中定义$conn。这就是全部。

您可以将$conn定义为您班级的属性。

class Product {
    private $connection;
    public function __construct($connection) {
        $this->connection = $connection;
    } 
    public function showProduct($id) {
        $database = ... ;
        mysqli_select_db($this->connection, $database);

答案 1 :(得分:0)

我认为您应该将include_once更改为include

class Product{

public $PRODUCT;
public $SHORT_DESC;
public $LONG_DESC;

public function showProduct($id){
include("conection.php");

在这里:

class User{
public function TopMenu(){
include("conection.php");