首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MySQL连接只在一个循环中工作一次?

MySQL连接只在一个循环中工作一次?
EN

Stack Overflow用户
提问于 2014-08-08 12:35:55
回答 2查看 86关注 0票数 0

给出了以下代码:

代码语言:javascript
运行
复制
<?php 
include 'dbconnect.php';
include 'properties_classes.php';

mysqli_select_db($connection, 'bl1property');
$p_results = mysqli_query($connection, "SELECT * FROM property");
$r_results = mysqli_query($connection, "SELECT * FROM rooms");

function return_bed_results($p_id, $r_results) {
    $total_bed = 0; 
    $available_single_bed = 0;  
    $available_double_bed = 0;
    echo ' Start searching for rooms at property ' . $p_id . '</br>';

    while ($bed_r = mysqli_fetch_array($r_results)) {
        echo 'str from ' . $bed_r['property id'] . '</br>';
        if ($p_id == $bed_r['property id']){
            echo 'pass (1) ' . $bed_r['property id'] . ' n ' . $bed_r['room no'] . '</br>';
            if ($bed_r['occupied'] == 0) {
                echo 'pass (2) ';
                if ($bed_r['room type'] == 'Single') {
                    echo ' single ' . '</br>';
                    $available_single_bed++;
                } else {
                    echo ' double ' . '</br>';
                    $available_double_bed++;
                }
            }
            $total_bed++;
        }
    }
    $return_b = new BedResults;
    $return_b->total_no_beds = $total_bed;
    $return_b->available_single_beds = $available_single_bed;
    $return_b->available_double_beds = $available_double_bed;
    echo '</br>' . 'Property no ' . $p_id . '</br>';
    echo 'No of beds = ' . $return_b->total_no_beds . '</br>';
    echo 'No of S beds = ' . $return_b->available_single_beds . '</br>';
    echo 'No of D beds = ' . $return_b->available_double_beds . '</br>';
    return $return_b;
}
$p_search_index = 0;
$newResults[] = new Results;
while ($row = mysqli_fetch_array($p_results)) {
    if ($accomodation_type == $row['tenant type'] || $accomodation_type == 'any') {
        if ($living_arrangment == $row['arrangement'] || $living_arrangment == 'any') {
            if ($min_sel <= $row['rent_min'] && $max_sel >= $row['rent_max']) {
                $b_total = return_bed_results($row['id'], $r_results);
                $newResults[$p_search_index] = new Results;
                $newResults[$p_search_index]->total_Bed = $b_total->total_no_beds;
                $newResults[$p_search_index]->available_single = $b_total->available_single_beds;
                $newResults[$p_search_index]->available_double = $b_total->available_double_beds;
                $newResults[$p_search_index]->rent_min = $row['rent_min'];
                $newResults[$p_search_index]->rent_max = $row['rent_max'];
                $p_search_index++;
            }
        }
    }
}
?>

第一个循环工作,它给我数据,但后来.什么都没发生!!

这里是输出结果:

开始在属性1str (1) 1n1str从1 pass (1) 1n2 str从1 pass (1) 1n3 pass (2)从1 pass (1) 1n4 str从1 pass (1) 1n5 str从1 pass (1) 1n6 str从2str 3str从3str 3str从3str到4str从7str到7str(1)从7 str从8 str 物业编号1床编号=6 S床编号=0 D床位数目=1开始在物业3搜查房间 属性3,床号=0,S床号=0,D床号=0,开始在属性4处搜索房间 属性编号4床的编号=0 S床的编号=0 D床位=0开始在属性7处搜索房间 属性7,床的编号=0,S床的编号=0,D床的编号=0,开始在属性8处搜索房间 属性8,床号=0,S床号=0,D级床号=0 我不知道为什么只有第一个循环工作..。有人能帮我吗,,谢谢,

EN

回答 2

Stack Overflow用户

发布于 2014-08-08 12:41:21

也许在第一个循环之后连接重置了吗?您可能必须为第二个查询重新打开它。

请贴上你的dbconnect.php (当然是模糊的)

编辑:添加这样的另一个mysqli_select_db语句会发生什么?

代码语言:javascript
运行
复制
mysqli_select_db($connection, 'bl1property');
$p_results = mysqli_query($connection, "SELECT * FROM property");
mysqli_select_db($connection, 'bl1property');
$r_results = mysqli_query($connection, "SELECT * FROM rooms");
票数 0
EN

Stack Overflow用户

发布于 2014-08-08 13:47:08

你为什么要:

代码语言:javascript
运行
复制
mysqli_select_db($connection, 'dbname');

试试这个:

代码语言:javascript
运行
复制
$connection = mysqli_connect('localhost','root','password','dbname');

如果您的密码是空的,那么您必须写空。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25203695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档