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.
我在php中执行while循环时出错。实际上在PHP代码我尝试从数据库中获取记录,我在代码的开头建立连接。在代码中我的外部循环执行良好但内部循环只执行一次并终止。然后我尝试再次建立mysql连接循环然后它执行。如果我评论这不能很好。告诉我运行while循环需要再次建立连接或有另一种方法来解决问题。
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>';
}
}
2 个解决方案
#1
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>";
}
#2
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
#1
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>";
}
#2
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