$sql=mysql_query("select cost from projectdetails");
while($row=mysql_fetch_assoc($sql))
{
$data=$row['cost'];
}
这是我的PHP代码你能帮我解决这个问题,
$data = array("0" => 898, "1" => 1498, "2" => 1343,"3" => 1345, "4" => 1045, "5" => 1343, "6" => 987);
答案 0 :(得分:1)
只需$data[] = $row['cost'];
即可获得$data
作为数组
答案 1 :(得分:0)
试试这个:
$count = 0;
$sql=mysql_query("select cost from projectdetails");
while($row=mysql_fetch_assoc($sql)) {
$data[$count]=$row['cost'];
$count++;
}
答案 2 :(得分:0)
首先你应该使用PDO,它要好得多,试试这个
//edit for your server/database
$username = "username";
$password = "password";
$hostname = "hostname";
$dbname = "database-name";
//to connect to the database
$db = new PDO('mysql:host='.$hostname.';dbname='.$dbname.';charset=utf8', $username, $password);
然后查询非常简单
$query = "SELECT cost FROM projectdetails;";
$sth = $db->prepare($query);
$sth->execute();
$data = $sth->fetchAll();
现在你将所有的$ data作为数组,你可以遍历它,分配不同的值或任何你想要的。