2017-12-10 1 views
-1

그래서이 오류가 발생한다.나는 이것을 얻는다. Uncaught Error : 정의되지 않은 함수를 호출한다. mysql_num_rows()

Call to undefined function mysql_num_rows() 

오류 해결 방법은 무엇인가요? 나는 이런 종류의 환경에 익숙하지 않고 더 많은 것을 배우고 싶다.

     $name = $_POST["name"]; 
          $surname = $_POST["surname"]; 
         $address = $_POST["address"]; 

     List item 
     $username = $_POST["username"]; 
       $password = $_POST["password"]; 
    $confirm_password = $_POST["confirm_password"]; 
     if ((empty($username))) { 
     $response = new usr(); 
     $response->success = 0; 
     $response->message = "Kolom username tidak boleh kosong"; 
     die(json_encode($response)); } else if ((empty($password))) { 
     $response = new usr(); 
     $response->success = 0; 
     $response->message = "Kolom password tidak boleh kosong"; 
     die(json_encode($response)); } else if ((empty($confirm_password)) || $password != $confirm_password) { 
     $response = new usr(); 
     $response->success = 0; 
     $response->message = "Konfirmasi password tidak sama"; 
     die(json_encode($response)); 
    } else { 
     if (!empty($username) && $password == $confirm_password){ 
      $num_rows = mysql_num_rows(mysql_query("SELECT * FROM resident WHERE  username='".$username."'")); 

      if ($num_rows == 0){ 
       $query = mysql_query("INSERT INTO resident (id,name, surname, address, contactno, username, password) 
VALUES(0,'".$name."','".$surname."','".$address."','".$contactno."','".$username."','".$password."')"); 

       if ($query){ 
        $response = new usr(); 
        $response->success = 1; 
        $response->message = "Register berhasil, silahkan login."; 
        die(json_encode($response)); 

       } else { 
        $response = new usr(); 
        $response->success = 0; 
        $response->message = "Username sudah ada"; 
        die(json_encode($response)); 
       } 
      } else { 
       $response = new usr(); 
       $response->success = 0; 
       $response->message = "Username sudah ada"; 
       die(json_encode($response)); 
      } 
     } } 
     mysql_close(); ?> 

고마워요!

+0

해결책을 제공 할 수 있습니까? –

답변

-1

PHP 7.x가 모두 mysql_* Funchionsweise를 제거하므로 대신 mysqli을 사용하십시오. 내가 코드를 편집


Source

이 작동합니다 코드 (테스트하지)입니다. 나는 또한 당신을 위해 몇 가지 메모가 있습니다.

$name = $_POST["name"]; 
$surname = $_POST["surname"]; 
$address = $_POST["address"]; 
$username = $_POST["username"]; 
$password = $_POST["password"]; 
$confirm_password = $_POST["confirm_password"]; 

if(empty($username){ 
    $response = new usr(); 
    $response->success = 0; 
    $response->message = "Kolom username tidak boleh kosong"; 
    die(json_encode($response));  
} 
elseif(empty($password)){ 
    $response = new usr(); 
    $response->success = 0; 
    $response->message = "Kolom password tidak boleh kosong"; 
    die(json_encode($response)); 
} 
elseif(empty($confirm_password) || $password != $confirm_password){ 
    $response = new usr(); 
    $response->success = 0; 
    $response->message = "Konfirmasi password tidak sama"; 
    die(json_encode($response)); 
} 
elseif(!empty($username) && $password == $confirm_password){ 
    // create mysqli object 
    $mysqli = new mysqli("my_server", "my_user", "my_password", "my_database"); 

    // check connection 
    if(mysqli_connect_errno()){ 
     sprintf("Connect failed: %s\n", mysqli_connect_error()); 
     exit(); 
    } 

    // escape the user name, have a look at the SQL injection paragraph 
    $username = $mysqli->real_escape_string($username); 

    // speed up the code, it is not necessary to select * because the returned data is not used anyway 
    $query = "SELECT 1 FROM resident WHERE username='".$username."';"; 
    $result = $mysqli->query($query); 

    if($result->num_rows > 0){ 
     // escape all the other values, using the array is not necessary, this is just for (in my opinion) better looking code 
     $values = array(
      'id' => 0, 
      'name' => $mysqli->real_escape_string($name), 
      'surname' => $mysqli->real_escape_string($surname), 
      'address' => $mysqli->real_escape_string($address), 
      'contactno' => $mysqli->real_escape_string($contactno), 
      'password' => $mysqli->real_escape_string($password) 
     ); 

     $query = "INSERT INTO resident (".implode(",", array_keys($values)).") VALUES (".implode(",", $values).");"; 
     $result = $mysqli->query($query); 

     if($result && $result instanceof mysqli_result && $mysqli->affected_rows > 0){ 
      $response = new usr(); 
      $response->success = 1; 
      $response->message = "Register berhasil, silahkan login."; 
      die(json_encode($response)); 
     } 
     else{ 
      $response = new usr(); 
      $response->success = 0; 
      // I think this is a wrong error message, this should say something like "There is an internal error, please contact an administartor" 
      $response->message = "Username sudah ada"; 
      die(json_encode($response)); 
     } 
    } 
    else{ 
     $response = new usr(); 
     $response->success = 0; 
     $response->message = "Username sudah ada"; 
     die(json_encode($response)); 
    } 

    $mysqli->close(); 
} 

노트

  1. SELECT * FROM my_table WHERE username='".$_POST['username'] 그런 짓을하지 마십시오! SQL Injection의 경우 매우 안전하지 않습니다. 데이터베이스 연결을 사용할 수있는 경우 항상 탈출 사용자가 더 나은 설정할 수 있습니다 모든 값 그렇지 않으면 오류를 얻을 것이다, prepared statements

  2. 확인을 사용합니다. 이러한 오류는 데이터베이스가 이동했거나 암호 또는 사용자가 변경 되었기 때문에 서버가 온라인이 아니기 때문에 발생할 수 있습니다 ...

  3. 68 번 줄에는 오류 발생 원인이 같지만 76 번과 같은 오류 메시지가 표시됩니다. 다른. 사용자가해야 할 일을 분명히 해주는 몇 가지 오류 메시지를 사용해야합니다 (줄 바꿈 오류로는 아무 것도 할 수 없음) 오류 위치를 알려줘야합니다.

  4. 나는 한번 작성되어 SQL 함수를 제공하는 Database 클래스를 인쇄합니다. 그렇게하면 매번 연결을 열 필요가 없으므로 데이터베이스를 변경하면 코드의 한 줄 (Database 클래스)이됩니다. 또한 mysql_에서 mysqli_으로 변경하는 것은 매우 쉬워 져서 문제가 매우 빨리 해결 될 것입니다.

+0

어떻게 할 수 있습니까? 내 스크립트 편집을 도와 줄 수 있습니까? –

+0

기다려 줘서 죄송하지만 시간이 좀 걸렸습니다. :) – miile7

관련 문제