1

我在 php 中执行 while 循环时出错。实际上在php代码中,我试图从数据库中获取记录,因为我在代码开头建立了连接。在代码中,我的外循环执行得很好,但内循环只执行了一次并终止。然后我尝试在while内再次建立mysql连接循环然后它执行。如果我评论这个它不能正常工作。告诉我运行while循环需要再次建立连接或者有另一种方法来解决问题。

这是我的代码:

$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>';
        }    
    }
4

2 回答 2

1

尝试这个

//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>";
} 
于 2013-09-12T07:52:00.470 回答
0

您正在尝试使用键访问普通数组的值

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

为此,您必须具有关联数组。尝试改变

$row = mysqli_fetch_array

$row = mysqli_fetch_assoc
于 2013-09-12T07:40:56.783 回答