尝试使用PHP从2个数据库中获取数据

时间:2018-06-19 07:55:10

标签: php html mysql sql database

我必须为学校项目制作一个非常接近的预订系统,但后来出现了这个问题,我无法解决。

所以我希望你们能帮助我:)

这是从数据库获取数据的PHP代码(formulier.php):

<div id="room1" style="margin-left: 10%;"><br><br>
    <img src=”source.php?id=2” width="150" height="150" />&nbsp;
    <?php
    $query = "SELECT * FROM dt_tb WHERE dt BETWEEN '2004-01-01' AND '2005-01-25'";
    /* SELECT * FROM dt_tb WHERE dt BETWEEN BETWEEN '$dt1' AND '$dt2' */
    $result = mysqli_query($connect,$query);

    if (mysqli_num_rows($result)) {
        while($row = $result->fetch_assoc()) {
            echo "HERE COMES THE DESCRIPTION &nbsp;";
            echo "&nbsp; check-in: ". $row["dt"] ." | check-out: ". $row["dt2"] ." ";
        }
    }
    else {
        echo "An error occured, Please choose another date.";
    }
    $connect->close();
    ?>
    <br><br><br>
    <form method="post" action="#">
        <input required type="submit" id="Reserve" name="Reserve" value="Book Now"  style="margin-left: 0.4%;">
    </form>
    <hr style="width: 65%;">
</div>

<div id="room1" style="margin-left: 10%;"><br><br>
    <img src=”source2.php?id=1” width="150" height="150" />&nbsp;
    <?php
    $query2 = "SELECT * FROM dt_tb WHERE dt BETWEEN '2000-01-01' AND '2100-01-25'";
    /* SELECT * FROM dt_tb WHERE dt BETWEEN BETWEEN '$dt1' AND '$dt2' */
    $result2 = mysqli_query($connect,$query2);
    if (mysqli_num_rows($result2)) {
        while($row2 = $result2->fetch_assoc()) {
            echo "HERE COMES THE DESCRIPTION &nbsp;";
            echo "&nbsp; check-in: ". $row2["dt"] ." | check-out: ". $row2["dt2"] ." ";
        }
    }
    else {
        echo "An error occured, Please choose another date.";
    }
    $connect->close();
    ?>
    <br><br><br>
    <form method="post" action="#">
        <input required type="submit" id="Reserve" name="Reserve" value="Book Now"  style="margin-left: 0.4%;">
    </form>
    <hr style="width: 65%;">
</div>

这是用于在数据库中获取图像的PHP代码(source.php):

$query = "select * from images WHERE id = 2";
$stmt = $connect->prepare( $query );
$stmt->bindParam(2, $_GET['id']);
$stmt->execute();
$num = $stmt->rowCount();
if( $num ){
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    header("Content-type: image/png");
    print $row['data'];
    exit;
}else{
}

这是第二张图片(source2.php):

$query = "select * from images WHERE id = 1";
$stmt = $connect->prepare( $query );
$stmt->bindParam(1, $_GET['id']);
$stmt->execute();
$num = $stmt->rowCount();
if( $num ){
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    header("Content-type: image/png");
    print $row['data'];
    exit;
}else{
}

这些是数据库:

TABLE IMAGES
========================================
id | name          | data
========================================
1  | sample image  |[BLOB - 31,9 KiB]
2  | sample image2 |[BLOB - 370,8 KiB]
TABLE DT_TB
========================================
id | dt          | dt2
========================================
1  | 2004-10-26  | 2005-01-25
TABLE DT_TB10
========================================
id | dt          | dt2
========================================
1  | 2006-01-01  | 2007-01-25

这是结果:https://gyazo.com/e3263b030bd2a367e86d39db262551ba

所以我的问题基本上是它不会显示来自数据库的图像 第二次&#34;预订&#34;将显示错误,而不是与第一次&#34;预订&#34;相同的数据。

如果有人可以帮我解决这个问题,我将非常感激。

提前致谢。

1 个答案:

答案 0 :(得分:0)

你可以用这个作为例子,但我已经解决了我的问题:)。

你永远不会在中途关闭$连接。

$connect->close();

使用了2次,但仅在您确实要关闭与数据库的连接时使用。

至于图像,@ Rick Calder有一个很好的解释。

感谢您的帮助。