🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
pdo select data ``` <?php echo "<table style='border: solid 1px black;'>"; echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>"; class TableRows extends RecursiveIteratorIterator { function __construct($it) { parent::__construct($it, self::LEAVES_ONLY); } function current() { return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>"; } function beginChildren() { echo "<tr>"; } function endChildren() { echo "</tr>" . "\n"; } } $servername = "localhost"; $username = "root"; $password = "123456"; $dbname = "test"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests"); $stmt->execute(); // set the resulting array to associative $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { echo $v; } } catch(PDOException $e) { echo "Error: " . $e->getMessage(); } $conn = null; echo "</table>"; ``` ``` $query = " SELECT id, firstname, lastname FROM MyGuests WHERE firstname = :firstname AND lastname = :lastname "; $stmt = $conn->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)); $stmt->execute(array(':firstname' => 'John', ':lastname' => 'Doe')); ``` ``` $query = " SELECT id, firstname, lastname FROM MyGuests WHERE firstname LIKE :firstname "; $stmt = $conn->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)); $stmt->execute(array(':firstname' => '%John1%')); ``` ``` $query = " SELECT * FROM tb_uctradecode WHERE _NAMEC_ LIKE :_NAMEC_ OR _NAMEE_ LIKE :_NAMEE_ OR _FHSCODE_ LIKE :_FHSCODE_ "; $stmt = $conn->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)); $stmt->execute(array( ':_NAMEC_' => '%'.$keywork.'%', ':_NAMEE_' => '% '.$keywork.'%', ':_FHSCODE_' => '%'.$code.'%' )); ```