티스토리 뷰

Programming/Apache, PHP

PHP - MYSQL 조회

파란크리스마스 2014. 7. 21. 00:09
728x90

출처 : Mysql_fetch_row 와 Mysql_fetch_array 차이

나누기 몫, 나머지

$num = floor(10 / 3); // 소수점 이하는 버리고 몫만 = 3
$num_1 = 10 % 3; // 나머지 = 1

echo "num = $num / num_1 = $num_1";

mysql_fetch_array : 필드이름으로 데이터 조회

<? 
$connect = mysql_connect('localhost', 'user id', 'password'); 
mysql_select_db("dbname", $connect); 

$query = "select userid, name, tel from user"; 
$result = mysql_query($query, $connect); 
$row = mysql_fetch_array($result); 

echo "$row[userid], $row[name], $row[tel]"; 
?>

DBarray : 함수로 제작

// mysql_fetch_array function
function DBarray($qry) {
	$result = @mysql_query($qry, $connect) or die("Qry Err. : $qry");
	if (mysql_num_rows($result)) { 
	 return mysql_fetch_array($result);
	} else {
	 return 0;
	}
}

PHP Inline IF

$page_max = floor($recode_count / $page_count) + ( ($recode_count % $page_count) > 0 ? 1 : 2);
$current_page = ($page == NULL ? 1 : $page);

Database 레코드 개수 만큼 조회

$result_row = DBquery("select * from gcm_table");

while($row = mysql_fetch_array($result_row)) {
	$data = $row['userid'];
    echo $data."<br />";
}

PHP - DB조회 레코드 개수 만큼 JSON 배열에 담기

$offset = ($current_page - 1) * $page_count;
$result_row = DBquery("select * from gcm_table limit 15 offset $offset");

$data_array = array();
while($gmc_row = mysql_fetch_array($result_row)) {
	$arr = array(
	  'id' => $gmc_row[id], 
	  'reg_id' => $gmc_row[reg_id], 
	  'phone_num' => $gmc_row[phone_num], 
	  'user_idx' => $gmc_row[user_idx], 
	  'userid' => $gmc_row[userid], 
	  'writeday' => $gmc_row[writeday], 
	  'use' => $gmc_row['use'] 
	);	
	array_push($data_array, $arr);
}
echo json_encode($data_array);
댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함