note 127641 added to mysql-xdevapi-table.select

From: Date: Thu, 15 Sep 2022 21:52:15 +0000
Subject: note 127641 added to mysql-xdevapi-table.select
Groups: php.notes 
Request: Send a blank email to php-notes+get-219585@lists.php.net to get a copy of this message
// Some simple script snippets for processing multiple database rows // ---------- Page - multipleRows.php ---------- <?php session_start(); require "./DatabaseClass.php"; ?> // ... other HTML Code <head> <link href="./bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> // ... other HTML Code <table class="table table-striped"> <thead> <tr> <th>Name</th> <th>Email</th> <th>Password</th> </tr> </thead> <tbody> <?php $userId = ''; $userName = ''; $userEmail = ''; $userPassword = ''; $dataAllUsers = DatabaseClass::getAllUsers(); if (empty($dataAllUsers)) { echo "<tr><td colspan='3'> <p class='alert alert-danger align-content-center'>No data found</p></td></tr>"; } foreach ($dataAllUsers as $d) { $userId = preg_replace('()', '', $d["id"]); $userName = $d["name"]; $userEmail = $d["email"]; $password = $d["password"]; echo "<tr>"; echo "<td>" . $userId . "</td>"; echo "<td>" . $userName . "</td>"; echo "<td>" . $userEmail . "</td>"; echo "<td>" . $userPassword . "</td>"; echo "<td>" . "<a href='userdetails.php?id=$userId'>user details</a>" . "</td>"; echo "</tr>"; } ?> </tbody> </table> // ... other HTML Code </div> </body> // ---------- DatabaseClass.php ---------- <?php class DatabaseClass { private $userId = ''; public static function connect() { $dbName = 'MyDatabase'; $dbHost = 'localhost'; $dbUsername = 'root'; $dbUserPassword = ''; try { $conn = new PDO("mysql:host=$dbHost;dbname=$dbName;", $dbUsername, $dbUserPassword); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage(); } return $conn; } public static function getAllUsers() { $db = DatabaseHelper::connect(); $sql = "SELECT * FROM Users ORDER BY Id ASC"; $stmt = $db->prepare($sql); $stmt->execute(); $data = $stmt->fetchAll(PDO::FETCH_ASSOC); $db = null; return $data; } } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.173 (proxied: 185.17.14.59) ---- Manual Page -- https://php.net/manual/en/mysql-xdevapi-table.select.php Edit -- https://main.php.net/note/edit/127641 Del: integrated -- https://main.php.net/note/delete/127641/integrated Del: useless -- https://main.php.net/note/delete/127641/useless Del: bad code -- https://main.php.net/note/delete/127641/bad+code Del: spam -- https://main.php.net/note/delete/127641/spam Del: non-english -- https://main.php.net/note/delete/127641/non-english Del: in docs -- https://main.php.net/note/delete/127641/in+docs Del: other reasons-- https://main.php.net/note/delete/127641 Reject -- https://main.php.net/note/reject/127641 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#219585) next »