2017-05-11 1 views
0

내가 당신의 기능 db_connect 및 db_query()를 사용,하지만 난 질문이 mysqli_insert_id과 : 어떻게 mysqli_insert_id()와 mysqli_affected_rows()를 사용, 값을 반환하기 때문에 0반환 0 mysqli_affected_rows이

function db_connect(){ 
    static $connection; 
     require_once __DIR__ . '/cfconfig.php'; 
    $connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE); 
    $connection->set_charset('utf8'); 

    if (mysqli_connect_errno()) { 
     printf("Connect failed: %s\n", mysqli_connect_error()); 
     exit(); 
    } 
    return $connection; 
} 

function db_query($query) { 
    $connection = db_connect(); 
    $result = mysqli_query($connection,$query); 
    return $result; 
} 

예 : 그 후

db_query("INSERT INTO .... 

:

$afectrows = mysqli_affected_rows(db_connect()); 
$insertid = mysqli_insert_id(db_connect()); 

값 0을 반환

+0

레코드가 성공적으로 삽입하여 삽입 ID를받을 수 있나요? 자동 증가 ID가 포함 되었습니까? – David

답변

2

일부 카고 틱 정적 변수가 걸려 있음에도 불구하고 호출 할 때마다 새 연결을 작성하는 것은 사용자의 기능 때문입니다.

당신이 연결이 실제 정적 확인해야 작동하게하려면 지금

function db_connect(){ 
    static $connection; 
    if (!$connection) { 
     require_once __DIR__ . '/cfconfig.php'; 
     mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 
     $connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE); 
     $connection->set_charset('utf8'); 
    } 
    return $connection; 
} 

모든 권리

$id = db_connect()->insert_id;