[英]How to fetch all rows of sql query from MySQL database into array in php
我正在編寫一個使用google chart API構建圖表的應用程序。 數據是動態的,並駐留在MySQL數據庫中。
面臨的問題是,包含要顯示的數據源的數組僅包含查詢返回的第一行。 即使循環后。
這是代碼:
include 'connect.php';
$sql="SELECT result,COUNT(result) value
FROM test
WHERE result LIKE 'GY%'
GROUP BY result";
$result=mysqli_query($link,$sql);
$myurl[]="['Option','Value']";
while($row=mysqli_fetch_assoc($result))
{
$result=$row['result'];
$value=$row['value'];
$Title="My Results";
$myurl[]="['".$result."',".$value."]";
}
<script type="text/javascript">
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.arrayToDataTable([
<?php
echo (implode(",",$myurl));?>
]);
var options={
title:'<?php echo($Title);?> '
};
圖表中僅使用返回的第一行,因此該值顯示為100%。 如何確保使用所有行值?
像這樣修改查詢
$sql="SELECT result value
FROM test
WHERE result LIKE 'GY%'
GROUP BY result";
$result=mysqli_query($link,$sql);
以及您可以訪問的結果數,如下所示。
mysqli_num_rows($result)
如果需要關聯數組,請使用以下命令:
$query = "SELECT column FROM `table` WHERE id='" . $id . "'"; / Whatever your query is
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
然后,您可以使用$row['columnname'];
從編號為$id
的行中調用$row
和列名$row['columnname'];
我也很確定您可以使用print_r($row['columnname']);
在WHERE id='" . $id . "'";
不包括WHERE id='" . $id . "'";
而是只具有$query = "SELECT column FROM
table ;
它將打印出該列中的所有數據(每一行)。
另外,如果您只是想將數據繪制到圖表上,則使用google API可能會使事情復雜化,因為您可以使用以下代碼將數據輸出到HTML表格中:
<?php
$query = "SELECT FROM `table`";
$result = mysql_query($query);
echo '<table class="table">';
echo '<tr class="table">';
echo '<th class="table">Column</th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result)
{
echo '<tr class="table">';
echo '<th class="table">' . $row['column'] . '</th>';
echo '</tr>';
}
echo '</table>';
?>
使用PHP中的echo然后添加html代碼,添加您需要的其他任何內容。 您還可以在表中放置按鈕,以及幾乎所有其他功能。 該網站應該提供您需要的有關HTML表格的更多信息: http : //www.w3schools.com/html/html_tables.asp
這是我用php回顯的HTML表單制作的另一個小示例。 它具有按鈕等。
$query = "SELECT * FROM `users` WHERE status='accepted'";
$result = mysql_query($query) or die(mysql_error());
echo '<table class="table">';
echo '<h2>Accounts</h2>';
echo '<tr class="table">';
echo '<th class="table">Name</th>';
echo '<th class="table">Email</th>';
echo '<th class="table">Username</th>';
echo '<th class="table">Date Created</th>';
echo '<th class="table">Decline</th>';
echo '<th class="table">Accept</th>';
echo '<th class="table">Give Admin</th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result))
{
echo '<tr class="table">';
echo '<th class="table">' . $row['name'] . '</th>';
echo '<th class="table">' . $row['email'] . '</th>';
echo '<th class="table">' . $row['username'] . '</th>';
echo '<th class="table">' . $row['trn_date'] . '</th>';
echo '<form method="post">';
echo '<th class="table"><input type="submit" name="decline_' .$row["id"]. '" value="decline" ></input></th>';
echo '<th class="table"><input type="submit" name="accept_' .$row["id"]. '" value="accept" ></input></th>';
echo '<th class="table"><input type="submit" name="giveadmin_' .$row["id"]. '" value="accept/give admin" ></input></th>';
echo '</form>';
echo '</tr>';
}
echo '</table>';
希望能有所幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.