如何在另一个页面中导入数据库连接

时间:2016-04-07 19:27:08

标签: php mysqli

我是php新手,我想将Db.php中的数据库连接详细信息导入到其他页面,但它无效。

如果代码需要每页设置数据库连接

$bd=mysqli_connect("localhost","root","","dbgadget");

请帮助我或举例说明如何访问其他页面中的db-connnection详细信息。

db.php中

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "dbgadget";


$bd = mysqli_connect($mysql_hostname, $mysql_user,   
$mysql_password,$mysql_database) ;


if (mysqli_connect_errno())
{
    echo "Failed to connect to database: " . mysqli_connect_error();
}

mysqli_close($bd);
?>

Productdetail.php

<?php
include('db.php');

$prodID = $_GET['prodid'];

if(!empty($prodID)){
    /// if i uncoment this line then code work fine 
    $bd=mysqli_connect("localhost","root","","dbgadget");

    $sqlSelectSpecProd = mysqli_query($bd,"select * from products where id = 
     '$prodID'");
    $getProdInfo = mysqli_fetch_array($sqlSelectSpecProd);
    $prodname= $getProdInfo["Product"];
    $prodcat = $getProdInfo["Category"];
    $prodprice = $getProdInfo["Price"];
    $proddesc = $getProdInfo["Description"];
    $prodimage = $getProdInfo["imgUrl"];
}
?>

1 个答案:

答案 0 :(得分:0)

您似乎正在关闭db.php文件中的数据库连接。尝试删除此行,然后在productdetail.php

代码的最后重新添加它