如何使用require_once()多个并连接到同一页面中的不同数据库?

时间:2015-10-16 09:50:43

标签: php mysqli

我尝试连接到同一页面中的不同数据库并选择两次。但它似乎不起作用,我得到一个数据库,并选择工作!这是我的代码

<?php 
require_once ('../connect_upevent.php');
require_once ('../connect_events.php');


$select_upevent="SELECT * FROM upevent";
$result_upevent=mysqli_query($con,$select_upevent);

$select_events="SELECT img_name , title , LEFT(`content` , 540)  as `content`,date, id FROM events";
$result_events=mysqli_query($con,$select_events);

mysqli_close($con);
?>

my while()

                        <?php while ($row_upevent = mysqli_fetch_array($result_upevent)) {   ?>                     
                    <li> 
                        <h4><?php echo $row_upevent['title'] ?></h4> 
                        <div class="dt-sc-two-third column first event-index-float">
                            <img src="images/event/<?php echo $row_upevent['img_name'] ?>" alt="" title="">
                            <p><?php echo $row_upevent['content'] ?></p>
                        </div>
                        <div class="dt-sc-one-third column event-index-float">
                            <div class="venue">
                                <h5>location</h5>
                                <p><?php echo $row_upevent['place'] ?></p>
                            </div>
                            <div class="details">
                                <h5>time</h5>
                                <p><?php echo $row_upevent['date'] ?></p>
                            </div>
                        </div>
                    </li>
                    <?php } ?>

my while()#2

                        <?php while ($row_events = mysqli_fetch_array($result_events)) {   ?>                     
                    <!--blog starts-->
                    <div class="dt-sc-one-half column first animate" data-animation="fadeInLeft" data-delay="300">
                        <article class="blog-entry">
                            <div class="entry-thumb">
                                <a href="event-detail.php?post=<?php echo $row_events['id'] ?>"><img src="images/event/<?php echo $row_events['img_name'] ?>" alt="" title=""></a>
                            </div>
                            <div class="entry-details">
                                <div class="entry-title">
                                    <h2><a href="event-detail.php?post=<?php echo $row_events['id'] ?>"><?php echo $row_events['title'] ?></a></h2>
                                </div>
                                <div class="entry-body">
                                    <p><?php echo $row_events['content'].'&nbsp;[ ... ]' ?></p>
                                </div>
                                <div class="entry-footer">
                                    <a href="event-detail.php?post=<?php echo $row_events['id'] ?>" class="read-more">read more<span class="fa fa-caret-left"></span> </a>
                                </div>
                            </div>
                        </article>
                    </div>
                    <!--blog ends-->
                    <?php } ?>

所以我怎样才能让它发挥作用!?

3 个答案:

答案 0 :(得分:1)

您连续连接并且两个数据库实例具有相同的名称。你应该做出不同的连接对象。只是用不同的名字命名。

#include "source.h"          

void source::do_cycle() {          
    valid_out = '0';         
    while(true) {         
        wait(clk.posedge_event());       
    //do different kind of stuff         
    }   
}       

或连接到一个,进行查询,连接到第二个并进行查询

<?php 
require_once ('../connect_upevent.php'); // $con_upevent
require_once ('../connect_events.php'); // $con_events


$select_upevent="SELECT * FROM upevent";
$result_upevent=mysqli_query($con_upevent, $select_upevent);

$select_events="SELECT img_name , title , LEFT(`content` , 540)  as `content`,date, id FROM events";
$result_events=mysqli_query($con_events, $select_events);

mysqli_close($con_upevent);
mysqli_close($con_events);
?>

答案 1 :(得分:1)

我需要查看所包含文件的内容:

  • connect_upevent.php
  • connect_events.php

但我猜,问题如下: 您使用相同的实例$con

因此,第一次在文件“connect_upevent.php”中声明$con,然后在“connect_events.php”文件中覆盖它

您应该在包含的文件中创建两个实例。像:

$con_upevent$con_events

否则,$con变量被“connect_events.php”文件覆盖

答案 2 :(得分:1)

使用不同的对象

创建两个连接
  

connect_upevent.php

$servername = "localhost";
$username = "username";
$password = "password";

// Create connection 1
$con1 = new mysqli($servername, $username, $password);
  

connect_events.php

$servername = "localhost";
$username = "username";
$password = "password";

// Create connection 2
$con2 = new mysqli($servername, $username, $password);
  

并使用这样的连接

<?php 
require_once ('../connect_upevent.php');
require_once ('../connect_events.php');


$select_upevent="SELECT * FROM upevent";
$result_upevent=mysqli_query($con1,$select_upevent);

$select_events="SELECT img_name , title , LEFT(`content` , 540)  as `content`,date, id FROM events";
$result_events=mysqli_query($con2,$select_events);

mysqli_close($con1);
mysqli_close($con2);
?>