Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Connect to the database
- *
- * @param string $username
- * @param string $password
- * @param string $dbname
- *
- * @return mysqli the connection to the database
- */
- function connect($username, $password, $dbname) {
- $conn = new mysqli("localhost", USERNAME, PASSWORD, DB_NAME);
- if ($conn->connect_errno) {
- echo "Failed to connect to MySQL: (" . $conn->connect_errno . ")" . $conn->connect_error;
- }
- echo 'Connected succesfully to: ' . $conn->host_info . "\n\n";
- return $conn;
- }
- /**
- * Show the columns and rows of a table
- *
- * @param mysqli $conn a connection to the database
- * @param string $tablename
- *
- * @return void // void significa que esta función no retorna nada
- */
- function show_table_data($conn, $tablename) {
- $stmt = $conn->prepare("SELECT * FROM " . TABLENAME);
- if (!$stmt) {
- echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
- }
- if (!$stmt->execute()) {
- echo "Execute failed: (" . $stmt->errno . ")" . $stmt->error;
- }
- $result = $stmt->get_result();
- while ($item = $result->fetch_object()) { // por cada fila de la tabla
- foreach ($item as $key => $attr) { // por cada clave (columna) y atributo (valor) de cada item de la fila
- echo "$key: $attr\n"; // muestro la clave y el atributo
- }
- echo "\n"; //
- }
- }
- const USERNAME = '';
- const PASSWORD = '';
- const DB_NAME = '';
- const TABLENAME = '';
- $conn = connect(USERNAME, PASSWORD, DB_NAME);
- show_table_data($conn, TABLENAME);
Add Comment
Please, Sign In to add comment