简体   繁体   中英

Do these functions check if there's a connection to the database and display the data?

Can someone explain what these two functions do? I think they check if you're connected to a database and if not display an error but I'm not completely sure. I also have no idea what's going on line by line. Can someone please explain?

<?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;
}
}
?>
<?php    
private function getData($sqlQuery) {    //creates a function named 'getData' with a required parameter
  $result = mysqli_query($this->dbConnect, $sqlQuery); //uses mysqli to query against the "$dbConnect" resource using the passed parameter $sqlQuery
  if(!$result){ //If the result doesn't exist die
    die('Error in query: '. mysqli_error());//die and show error
  } //peforms a  query against database to check it is connected
  $data= array();//create an empty array
  while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {//fetch a row using association (aka an array mapped by keys with no numbered index)
    $data[]=$row;//array push the returned row
  } 
 return $data;//return an array of arrays that you've gotten from MySqlI
}
private function getNumRows($sqlQuery) {//create a function
    $result = mysqli_query($this->dbConnect, $sqlQuery);//query
    if(!$result){//if no result die
        die('Error in query: '. mysqli_error());//die and show error
    }
    $numRows = mysqli_num_rows($result);//get the number of rows in the query
    return $numRows;//return the number of rows in the query
}
}
?>

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM