怎么做mysql_query到pdo

时间:2014-01-25 17:04:40

标签: php mysql pdo

我需要帮助mysql_query到pdo

$sql = mysql_query("SELECT * FROM songs");

while($row = mysql_fetch_array($sql))
{
    $result[] = $row;
}

1 个答案:

答案 0 :(得分:2)

PDO基本查询(阅读Migrate from the MySQL Extension to PDO

// Step 1: Establish a connection
$db = new PDO("mysql:host=localhost;dbname=YourDB", "YourUser", "YourPassword");

// Step 2: Construct a query
$query = "SELECT * FROM songs";

// Step 3: Send the query
$qresult = $db->query($query);

// Step 4: Iterate over the results
while($row = $qresult->fetch(PDO::FETCH_ASSOC)) {
    $result[] = $row;
}

// Step 5: Free used resources
$qresult->closeCursor();
$db = null;