我有代碼,我不確定它是否正確以及結構是否可能。下面是代碼:使用表中的行填充數組
$host="localhost";
$username="sample1";
$password="1234";
$db_name="sampledb";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
function example1(array1) {
//is this allowed??
$array1 = array();
$ctr = 0;
$ctr1=1;
$sql="SELECT names FROM tblnamelist";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$count=mysql_num_rows($result);
//I also want to populate the array1 with all the values that was retrieved in the query then return it as an array
if($count!=0) {
while($ctr1<=$count) {
$array1[$ctr]=$row[$ctr];
}
}
}
基本上我的問題是我怎麼能填充array1
從查詢檢索到的值?
$ array1 = $ row部分中的$ array1 []不應該有一個ctr?像$ array1 [ctr] = $ row ['names']; ctr ++? –
不,'$ array1 [] = $ row'會將$ row添加到數組的末尾。它會自動增長:)。 –