2013-02-19 4 views
0

2 차원 배열로 변환하는 것보다 데이터베이스에서 레코드를 얻고 싶습니다.PHP 레코드를 2 차원 배열로 변환합니다

Example: 

$data = array(
     1 => array ('Name', 'Surname','sex','address','web'), 
     array('Schwarz', 'Oliver','M','KP','222.dddd'), 
     array('Test', 'Peter','F','KK','wwww.fsadfs') 
     ); 

위와 같이 데이터베이스의 데이터를 포맷하는 방법은 무엇입니까?

+1

함수를 만드는 당신의 대답은, 여러 배열

public static function getRecords($query){ // Gets multiple records and returns associative array $db = db::open(); $result = $db->query($query); if(!$result){ die('There was an error running the query [' . $db->error . ']'); } if($result->num_rows>0){ $i=0; while ($row = $result->fetch_assoc()){ $recordset[$i] = $row; $i++; } }else{ $recordset = false; } db::close($db); return ($recordset); } 

을 반환이다 '동안 ($의 편곡의 [ ] = $ db-> fetchRow())'? 정확히 그 모든 것이 어렵지는 않습니다. –

+0

친애하는 Marc B, 데이터베이스 전체 코딩을 제공 할 수 있습니까? – user1759419

답변

0

Here은 PDO와 붙여 넣기 내부에서 생성 된 메모리 내장 SQLite 데이터베이스를 사용하는 예입니다. 데이터베이스를 인라인으로 생성해야만 스크립트가 엄격하게 필요한 것보다 약간 자세한 정보를 얻을 수 있습니다. 이 같은

0

시도 뭔가 :

$i = 0; 

foreach($results as $result) 
{ 
    $array[$i]['field1'] = $results['field1']; 
    $array[$i]['field2'] = $results['field2']; 

    $i++; 
} 

이 데이터베이스는 다소 차이가있을 수 있습니다에 액세스하는 데 사용중인 라이브러리에 따라.

+0

안녕하세요, 저는 무슨 뜻인지 이해하지 못합니까? – user1759419

+0

죄송합니다. 예를 들어 ADO를 사용하고 있다면'$ results = $ db-> Execute ($ sql);'을 실행하면 위에서 설명한 바와 같이'$ results'를 연관 배열로 반복 할 수 있습니다. – 80vs90

0

여기에 그냥

$QUERY="select * FROM USERS"; 


AND CREATE class db to open the connection and also add function in this class and your function is ready 



    class db{ 
    public static function open(){ // opens the db connection. Specify different databases for local and live server and it will automatically select the correct one 

     $servers = array('localhost', '127.0.0.1', 'warrior'); 
     if(in_array($_SERVER['HTTP_HOST'], $servers)){ //for localhost 
      $dbuser = 'root'; 
      $dbpwd = ''; 
      $dbname = 'database name'; 
      $dbserver = 'localhost'; 
     } 
     $db = mysqli_connect($dbserver,$dbuser,$dbpwd,$dbname); 
     if ($db->connect_errno > 0){ 
      echo "Failed to connect to MySQL: " . $db->connect_error; 
      } 
     return $db; 
     } 

    public static function close(&$db){ 
     $db->close(); 
    } //also add your Get_record function here 
} 

파일을 포함

같은 쿼리를 전달하고 테스트 감사

관련 문제