標籤:
exec 在一個單獨的函數調用中執行一條 SQL 陳述式,返回受此語句影響的行數。
exec 不會處理select語句
<?phptry { $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();}
<?phpheader(‘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();}
PHP PDO學習(二) exec執行SQL