복붙노트

PHP 데이터베이스 row 갯수 세기

PHP

PHP 데이터베이스 row 갯수 세기

mysqli

mysqli_num_rows

$query_result =  mysqli_query($con, "쿼리문");  
$query_count = mysqli_num_rows($query_result);  

쿼리 결과값의 갯수를 셉니다.

결과 row 갯수가 딱 하나일 때 함수 만들어보기

데이터베이스에서 결과값을 딱 하나만 가지고 와야 할 일이 있습니다.
그럴때는 아래와 같이 함수를 만들어 쓰면 됩니다.

function check_mysqli_exist($query){  
$query_result =  mysqli_query($con, $query);  
if ($query_result === false){  
return false;  
}  

$query_count = mysqli_num_rows($query_result);  

if ($query_count === 1){  
$row = mysqli_fetch_array($query_count, MYSQLI_BOTH);  
return $row;  
}  

return false;  
}  

mysqli prepare statement

PHP에서 SQL injection 방지하기
에서 이야기했던 대로 mysqli 에서 prepare statent 를 이용할 때는 이렇게 쓰세요.

$query = "SELECT `email` FROM `tblUser` WHERE email=?";  

if ($stmt = $dbl->prepare($query)){  

$stmt->bind_param("s", $email);  

if($stmt->execute()){  
$stmt->store_result();  

$email_check= "";  
$stmt->bind_result($email_check);  
$stmt->fetch();  

if ($stmt->num_rows == 1){  

echo "That Email already exists.";  
exit;  

}  
}  
}  

소스 출처 : https://stackoverflow.com/questions/22252904/check-if-row-exists-with-mysql

mysqli prepare statement 함수화

function check_mysqli_ps_exist($query, $params){  
$dbl = new mysqli("DB호스트", "DB유저이름", "DB비번", "DB이름");  

// 커넥션 체크  
if (mysqli_connect_errno()) {  
return false;  
}  

// 실제 접속  
if ($stmt = $dbl->prepare($query)){  
// 파라미터 바인딩.  
// 예제에서는 모든 파라미터가 '문자열' 이라고 가정합니다.  
foreach($params as $key=>$val){  
$stmt->bind_param("s", $val);  
}  

if($stmt->execute()){  
$stmt->store_result();  
$bind_check= "";  
$stmt->bind_result($bind_check);  
$stmt->fetch();  

if ($stmt->num_rows == 1){  
return $bind_check;  
}  
}  

return false;  
}  
}  

PDO 사용

$mysql_hostname = 'xxx';  
$mysql_username = 'xxx';  
$mysql_password = 'xxx';  
$mysql_dbname = 'xxx';  

try {  
$conn= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);  
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
} catch (PDOException $e) {  
exit( $e->getMessage() );  
}  

// assuming a named submit button  

try {  
$stmt = $conn->prepare('SELECT `email` FROM `tblUser` WHERE email = ?');  
$stmt->bindParam(1, $_POST['email']);  
$stmt->execute();  
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {  
// 여기서 뭔가 작업을 합니다.  
}  
}  
catch(PDOException $e) {  
echo 'ERROR: ' . $e->getMessage();  
}  

if($stmt->rowCount() > 0){  
echo "The record exists!";  
} else {  
echo "The record is non-existant.";  
}  

PDO 함수화

function check_pdo_exist($query, $params){  

$mysql_hostname = 'xxx';  
$mysql_username = 'xxx';  
$mysql_password = 'xxx';  
$mysql_dbname = 'xxx';  

try {  
$conn= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);  
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
} catch (PDOException $e) {  
exit( $e->getMessage() );  
}  

try {  
$stmt = $conn->prepare($query);  

// 파라미터 바인딩  
foreach($params as $index,$val){  
$stmt->bindParam($index + 1, $val);  
}  

$stmt->execute();  
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {  
return $row;  
}  
}  
catch(PDOException $e) {  
echo 'ERROR: ' . $e->getMessage();  
return false;  
}  
}  


'PHP' 카테고리의 다른 글

PHP 접속자 IP 알아내기  (0) 2017.11.20
PHP 값을 자바스크립트에 쓰기  (0) 2017.11.19
PHP 문자열 이어붙이기  (0) 2017.11.18
PHP에서 메일 보내기  (0) 2017.11.18
php 업로드 용량 설정 변경  (0) 2017.11.17