Press "Enter" to skip to content

[PHP] mysql update 된 row 수 체크하기

<?php
$mysql = new mysqli("localhost","username","password","use_db");

if($mysql->connect_errno)
{
  echo "MySQL 접속 실패 : ".$mysql->connect_error;
  exit();
}

// 업데이트 처리
$mysql->query("UPDATE usertable SET name = 'myName' where id = 100");
echo "적용된 row 수: ".$mysql->affected_rows;

$mysqli->close();
?>

SELECT 일때도 동일하게 사용할 수 있습니다.

다만, query() 의 리턴 변수에서 체크하는 것이 아니라 mysqli 인스턴스로 체크합니다.

One Comment

  1. comago comago

    // 조회시
    $sql = “SELECT * FROM table”;
    $res = mysql_query($sql, $conn_db);

    $tot_rows = mysql_num_rows($res);

    // 업데이트, 인서트, 딜리트 시
    $usql = “update set status = ‘N’ table where id = 10”;
    $ures = mysql_query($usql, $conn_db);
    $totcnt = mysql_affected_rows($conn_db);

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다