2012-09-12 2 views
3

코드가 있는데 올바른지 구조가 가능한지 확실하지 않습니다.테이블에있는 행의 값으로 배열을 채우는 중

$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 채울 수있는 방법입니다 : 다음 코드는?

답변

0

내가 배열을 반환하는 것이 좋습니다, 나는 꽤 ... 정말 기능이 무엇을하고 있는지 알 수없는 함수의 사용자로 참조 시스템을 좋아하지 않아

function get_results() 
{ 
    $array1 = array(); 

    $sql="SELECT names FROM tblnamelist"; 
    $result=mysql_query($sql); 

    while($row=mysql_fetch_array($result)) 
    { 
     $array1[] = $row; 
    } 
    return $array1; 
} 
$array = get_results(); 
+0

$ array1 = $ 행 부분의 $ array1 []에 ctr ?? like $ array1 [ctr] = $ row [ 'names']; CTRL +? –

+0

아니요,'$ array1 [] = $ row'는 $ row를 배열의 끝에 추가합니다. 그것은 자동적으로 성장할 것입니다 :). –

0
function example1(&$array1) { 
    //is this allowed?? -- yes, but you have to do it by reference see & in the definition 
    $array1 = array(); 
0

당신은 연관 배열 반환이 기능을 사용하여 결과를 검색하기 위해 추가로 배열을 만들 필요가 없습니다 : 경우 당신에

while($row=mysql_fetch_array($result){ 

echo $row['field_name']; 
} 

을 :

$sql="SELECT names FROM tblnamelist"; 
    $result=mysql_query($sql); 
    while($row=mysql_fetch_array($result)){ 
    echo $row['field_name']; 

} 

하는 경우를 while 루프가 필요하지 않은 것보다 결과에 단일 행이 있습니다.

0

사용이

if ($count!=0) 
    { 
    while($row=mysql_fetch_array($result)) 
    { 
     array_push($array1,$row['names']); 
    } 
    } 
print_r($array1); 
0

귀하는이처럼 보이게하기 위해 while 루프를 다시 작성할 수 있습니다. 아래 코드는 결과가 더 이상 남아 있지 않을 때까지 $result에서 새로운 $row을 얻습니다. 당신이 값으로하고있는 모든 화면에 인쇄 할 경우, 당신은뿐만 아니라 Harshal의 솔루션을 사용할 수 있습니다, 물론

$array1 = array(); 
while($row = mysql_fetch_array($result)) { 
    $array1[] = $row['names']; // Insert the value of $row['names'] to the end of the array 
} 

// return your array, or use Jakub's method. 
return $array1; 

(당신은 그 $count 변수를 필요가 없습니다). 혹시 함수가 배열을 반환해야 할 것 , 함수가 될 수있다 :

function getNamesArray() { 
    $sql="SELECT names FROM tblnamelist"; 
    $result=mysql_query($sql); 

    // this is the result array that this function will return 
    $array1 = array(); 

    // loop while there are rows in the mysql result 
    while($row = mysql_fetch_array($result)) { 
     // Insert the value of $row['names'] to the end of the array 
     $array1[] = $row['names']; 
    } 
    return $array1; 
} 

// test the function: 
$test = getNamesArray(); 
var_dump($test); 

당신은 비록 prepared statements을 사용하는 것이 좋습니다.및 MySQLi을 살펴보십시오. mysql_ 함수의 사용은 discouraged입니다.

0

횟수 필요가 없습니다 그동안 어떤 값을 반복하므로 당신이 배열에 논리적 인덱스를 제공 할 수 있습니다, 아래이 여러 번 사용하는 경우 행이

$result=mysql_query($sql); 
while($row=mysql_fetch_array($result)) { 
    $array1[]=$row; 
} 

를 배열 1에 할당;

while($row=mysql_fetch_array($result)) { 
    $array1[$row['myUniqueRowID']]=$row; 
} 
관련 문제