简体   繁体   中英

MySQL connection only works once in a while loop?

Given this code:

<?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++;
            }
        }
    }
}
?>

The first loops works and it gives me data but afterwards .... nothing much happens!!

here is the output results:

Start searching for rooms at property 1 str from 1 pass (1) 1 n 1 str from 1 pass (1) 1 n 2 str from 1 pass (1) 1 n 3 pass (2) double str from 1 pass (1) 1 n 4 str from 1 pass (1) 1 n 5 str from 1 pass (1) 1 n 6 str from 2 str from 2 str from 2 str from 2 str from 3 str from 3 str from 3 str from 3 str from 4 str from 4 str from 4 str from 4 str from 7 str from 7 str from 7 str from 7 str from 7 str from 7 str from 8 str from 8 str from 8 str from 8 str from 8 str from 8 str from 8

Property no 1 No of beds = 6 No of S beds = 0 No of D beds = 1 Start searching for rooms at property 3

Property no 3 No of beds = 0 No of S beds = 0 No of D beds = 0 Start searching for rooms at property 4

Property no 4 No of beds = 0 No of S beds = 0 No of D beds = 0 Start searching for rooms at property 7

Property no 7 No of beds = 0 No of S beds = 0 No of D beds = 0 Start searching for rooms at property 8

Property no 8 No of beds = 0 No of S beds = 0 No of D beds = 0

i dont know why only the first loops works... can some one help me please Thanks

Is perhaps the connection reset after the first loop? You might have to reopen it for the second query.

Coult you please post your dbconnect.php (obfuscated of course)

Edit: What happens when you add another mysqli_select_db statement like this?

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");

why do you do:

mysqli_select_db($connection, 'dbname');

try this:

$connection = mysqli_connect('localhost','root','password','dbname');

if you password is nothing then you have to write NULL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM