exec 在一个单独的函数调用中执行一条 SQL 语句,返回受此语句影响的行数。
exec 不会处理select语句
<?php try { $pdo = new PDO(‘mysql:host=localhost:3306;dbname=zhy‘, ‘root‘, ‘root‘); $sql = <<<EOF CREATE TABLE IF NOT EXISTS USERS( ID INTEGER AUTO_INCREMENT KEY, USERNAME VARCHAR(20) NOT NULL UNIQUE, PASSWORD CHAR(30) NOT NULL, EMAIL VARCHAR(30) ) EOF; $res = $pdo->exec($sql); var_dump($res); echo ‘<hr/>‘; $sql = ‘INSERT INTO USERS (USERNAME, PASSWORD, EMAIL) VALUES ("ZYN", "‘.md5(‘ZYN‘).‘", "[email protected]");‘; $res = $pdo->exec($sql); echo ‘受影响的记录条数 :‘.$res.‘<br/>‘; echo ‘最后插入的ID号为:‘.$pdo->lastInsertId(); // var_dump($res); echo ‘<hr/>‘; } catch (PDOException $e) { echo $e->getMessage(); }
<?php header(‘content-type:text/html;charset=utf-8‘); try { $pdo = new PDO(‘mysql:host=localhost:3306;dbname=zhy‘, ‘root‘, ‘root‘); $sql = ‘UPDATE USERS SET USERNAME = "ZHY3" WHERE ID=1‘; $res = $pdo->exec($sql); echo $res.‘条记录被影响‘; } catch (PDOException $e) { echo $e->getMessage(); }
时间: 2024-10-11 15:05:43