1

I have an error in executing a while loop in php. actually in php code I tried to fetch records from database for that i establish connection at the beginning of code.In code my outer loop executed well but inner loop executed only one time and terminated.Then i tried to establish mysql connection once again within while loop then it executed.If i comment this it not work well.Tell me to run while loop there is need to establish connection once again or there is another way to solve problem.

Here is my code:

$con    = mysqli_connect("localhost", "root", "root", "scheduler");
$sql    = "select * from jobschedule";
$result = mysqli_query($con,$sql);
$sql2   = "SET GLOBAL event_scheduler = 1";

mysqli_query($con,$sql2);
while($row = mysqli_fetch_array($result))
{
    // $con = mysqli_connect("localhost", "root", "root", "scheduler");

    $dbname  = $row['DBName'];
    $jobname = $row['JobName'];

    $sql1    = "call $dbname.$jobname";
    $result1 = mysqli_query($con,$sql1);

    while($row1 = mysqli_fetch_array($result1))
    {
        echo '<tr>';
           . '<td>' . $row1['Name'] . '</td>';
           . '<td>' . $row1['CountryCode'] . '</td>'
           . '<td>' . $row1['District'] . '</td>'
           . '<td>' . $row1['Population'] . '</td>'
           . '</tr>';
        }    
    }
1
  • 1
    If the inner loop terminates after one time, it just means that the query only returned one row. Are you sure it's supposed to return more? Commented Sep 12, 2013 at 8:10

2 Answers 2

1

Try this

//Connection
$con = mysqli_connect("localhost", "root", "root", "dbname") or die("Error " . mysqli_error($con));

//Query 

$query = "SELECT * FROM tablename" ;

//execute the query.

$result = $con->query($query) or die("Error in the Query.." . mysqli_error($con));

//display information:

while($row = mysqli_fetch_array($result)) {
  echo $row["field_name"] . "<br>";
} 
Sign up to request clarification or add additional context in comments.

2 Comments

I like that OR die(...) - that's a great way to see where exactly the error occurs. Even better would be try { ... } catch(Exception $e) { throw new Exception() }.
In the question he said the loop is executing one time. That means the query was successful, so adding this error checking, while a good idea, probably won't solve the problem.
0

You are trying to acces normal array's values with keys

    $dbname = $row['DBName'];
    $jobname = $row['JobName'];

To do this you must have associative arrays. Try changing

$row = mysqli_fetch_array

to

$row = mysqli_fetch_assoc

2 Comments

By default, mysqli_fetch_array() returns an array with both numeric and named keys, so his array accesses should work.
I tried both solution for my problem but it not work. it giving me error like: "Commands out of sync; you can't run this command now" when inner while loop run

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.