>  Q&A  >  본문

사용자 정의 함수에서 $mysqli->query를 사용할 수 없는 이유는 무엇입니까?

MCXCI}8`AS6FQ6ZJ7TUW)%H.png

别闹i别闹i2564일 전965

모든 응답(1)나는 대답할 것이다

  • 天蓬老师

    天蓬老师2017-09-15 14:31:20

    사용에 문제가 있습니다~~

    $mysqli->query()를 사용하기 위한 전제 조건은 $mysqli 객체를 생성했다는 것입니다. 이 객체를 dbconfigs.php 파일에 생성하셨나요?

    또한 아래에서 mysqli_fetch_array()를 사용하셨습니다. 이는 일반적인 프로세스 지향 명령문입니다. 데이터를 어떻게 호출하려는지 정말 모르겠습니다.

    객체지향을 사용하고 싶다면 다음과 같이 사용하세요:

    $mysqli = new mysqli($host,$userName,$password,$dbName);
    if ($mysqli->connect_errno){
        die('Error Connected'.$mysqli->connect_error;
    }
    $result = $mysqli->query('SELECT * FROM table_name');
    if ($result && $result->num_rows > 0){
    while($row = $result->fetch_array(MYSQLI_ASSOC)){
       //输出数据
     }
     $result->free_result();
    }
    $mysqli->close();

    프로세스 지향이라면 위 코드를 수정하세요:

    $conn = mysqli_connect($host,$userName,$password,$dbName);
    if (mysqli_connect_errno($conn)){
        die('Error Connected'.mysqli_connect_error($conn);
    }
    $result = mysqli_query('SELECT * FROM table_name');
    if ($result && mysqli_num_rows($conn,$result) > 0){
    while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
       //输出数据
     }
     mysqli_free($result);
    }
    mysqli_close($conn);

    객체지향과 프로세스지향을 섞어 사용하지 마세요.

    PHP 중국어 웹사이트에는 다음 두 부분에 대한 튜토리얼이 있습니다:

    http://www.php.cn/course/653.html (MySQLi 객체 지향)

    http://www.php.cn/course/653 .html(MySQLi는 프로세스 지향적입니다)

    회신하다
    1
  • 别闹i

    $mysqli=new mysqli("localhost","root","root","perform_file") or die("데이터베이스 연결 실패"); $mysqli->query("utf8 이름 설정"); //dbconfigs.php의 구성입니다. mysqli_fetch_array //이것은 배열을 통해 설정된 쿼리 결과를 반복하는 것이 아닌가?

    别闹i · 2017-09-18 09:23:35
    别闹i

    dbconfigs.php의 구성에 문제가 있습니까?

    别闹i · 2017-09-18 09:43:54
    天蓬老师

    mysqli_fetch_array()는 결과 세트를 인덱스 및 연관을 포함하여 하나씩 배열 출력으로 변환합니다.

    天蓬老师 · 2017-09-18 09:29:22
  • 취소회신하다