Last active
October 17, 2017 17:31
-
-
Save samuel-fonseca/bc1510ebdf5e09fe48d21f0ce2e2dee7 to your computer and use it in GitHub Desktop.
This is the select function, from my php_database function (https://github.com/brazucaz/php_database/blob/master/src/class.database.php)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get content from the database | |
* | |
* @param $fields an array with what columns to pull | |
* @param $table which table to query | |
* | |
* @return $content with the table content | |
* @return $message with either an error or success message | |
*/ | |
public function select( array $fields, string $table, $extra = '' ) | |
{ | |
$message = array(); | |
if( !is_array( $fields ) ) | |
{ | |
$message = array( | |
'is_error' => 'warning', | |
'message' => 'Invalid type given for $fields' | |
); | |
return $message; | |
exit(); | |
} | |
foreach($fields as $value) | |
{ | |
$val[] = $value; | |
} | |
$rows = implode(', ', $val); | |
$sql = "SELECT " . $rows . " FROM " . $table . " " . $extra; | |
if ($result = $this->_connection->query( $sql )) | |
{ | |
if ($result->num_rows > 0) | |
{ | |
try | |
{ | |
while ($row = $result->fetch_assoc()) | |
{ | |
$content[] = $row; | |
} | |
return $content; | |
} | |
catch (Exception $e) | |
{ | |
trigger_error("Error: " , $e->getMessages()); | |
exit(); | |
} | |
} | |
else | |
{ | |
$message = array( | |
'is_error' => 'warning', | |
'message' => 'There are no records in the `' . $table . '` table.' | |
); | |
return $message; | |
exit(); | |
} | |
} | |
else | |
{ | |
$message = array( | |
'is_error' => 'danger', | |
'message' => 'Query Error, please revise the information.' | |
); | |
return $message; | |
exit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment