这些功能是否检查到数据库的连接并显示数据?

有人可以解释这两个功能的作用吗?我认为他们会检查您是否连接到数据库,如果未显示错误,但我不确定。我也不知道是怎么回事。有人可以解释一下吗?


<?php    

private function getData($sqlQuery) {

  $result = mysqli_query($this->dbConnect, $sqlQuery);

  if(!$result){

    die('Error in query: '. mysqli_error());

  } //peforms a  query against database to check it is connected

  $data= array();

  while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

    $data[]=$row;

  } 

 return $data;

}

private function getNumRows($sqlQuery) {

    $result = mysqli_query($this->dbConnect, $sqlQuery);

    if(!$result){

        die('Error in query: '. mysqli_error());

    }

    $numRows = mysqli_num_rows($result);

    return $numRows;

}

}

?>


冉冉说
浏览 150回答 1
1回答

天涯尽头无女友

<?php&nbsp; &nbsp;&nbsp;private function getData($sqlQuery) {&nbsp; &nbsp; //creates a function named 'getData' with a required parameter&nbsp; $result = mysqli_query($this->dbConnect, $sqlQuery); //uses mysqli to query against the "$dbConnect" resource using the passed parameter $sqlQuery&nbsp; if(!$result){ //If the result doesn't exist die&nbsp; &nbsp; die('Error in query: '. mysqli_error());//die and show error&nbsp; } //peforms a&nbsp; query against database to check it is connected&nbsp; $data= array();//create an empty array&nbsp; while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {//fetch a row using association (aka an array mapped by keys with no numbered index)&nbsp; &nbsp; $data[]=$row;//array push the returned row&nbsp; }&nbsp;&nbsp;return $data;//return an array of arrays that you've gotten from MySqlI}private function getNumRows($sqlQuery) {//create a function&nbsp; &nbsp; $result = mysqli_query($this->dbConnect, $sqlQuery);//query&nbsp; &nbsp; if(!$result){//if no result die&nbsp; &nbsp; &nbsp; &nbsp; die('Error in query: '. mysqli_error());//die and show error&nbsp; &nbsp; }&nbsp; &nbsp; $numRows = mysqli_num_rows($result);//get the number of rows in the query&nbsp; &nbsp; return $numRows;//return the number of rows in the query}}?>希望这可以帮助
打开App,查看更多内容
随时随地看视频慕课网APP