简体   繁体   中英

Switch query from traditional MySQL to PDO Format

I'm in the process of turning all of mysql related stuff in PHP into PDO format. That being said, I need to ask a question.

I had a query that looked like this:

$query = "SELECT COUNT(*) FROM table WHERE home_team = '".$team."' AND home_score > away_score";

With PDO, I have tried:

$query = "SELECT COUNT(*) FROM table WHERE home_team=:team AND home_score>:away_score";
$stmt = $db->prepare($query);
$stmt->bindValue(':team', $team, PDO::PARAM_STR);
$count = $stmt->fetchColumn();

However, when $count is echoed out, nothing appears. I have a strong feeling it's because the error pertains to the query. But, I'm fairly new to PDO and can't an error message the way mysql_query()or die(mysql_error()); would.

Any ideas?

You missing error handling core from your calls. To check for PDO errors:

a. Change PDO error mode to exceptions:

$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

After this, a PDO error will result in an instance of PDOException, which you can catch / check.

OR

b.Check for errors after a query execution / PDO call with the errorInfo function.

$stmt = $dbh->prepare('bogus sql');
if (!$stmt) {
    echo "\nPDO::errorInfo():\n";
    print_r($dbh->errorInfo());
}
$query = "SELECT COUNT(*) FROM table WHERE home_team=:team AND home_score>:away_score";
$stmt = $db->prepare($query);
$stmt->execute(array(':team' => $team, ':away_score' => $away_score)); //- you are missing this line
$count = $stmt->fetchColumn();

Example 1 here shows that query should be prepared and than executed first. After that you will be able to get results using fetchColumn. Also, it looks like you are missing an away_score value. I've passed it in execute function

If away score is a column, do not mark it as a placeholder ( : set before its name):

$query = "SELECT COUNT(*) FROM table WHERE home_team=:team AND home_score > away_score";

and than in execute:

$stmt->execute(array(':team' => $team));

In mysql, we know there are mysql_fetch_row(), mysql_fetch_array(), and mysql_fetch_assoc().

For PDO there's a lot of type of fetch

<?php
// configuration
$dbtype= "mysql";
$dbhost= "localhost";
$dbname= "test";
$dbuser= "root";
$dbpass= "admin";

// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// query
$sql = "SELECT title FROM books ORDER BY title";
$q   = $conn->query($sql);
// fetch
while($r = $q->fetch()){
  print_r($r);
}
// result
//Array ( [title] => book_title [0] => book_title ) 
?>

This is when you don't have to prepare the statement , if you have to do it you should follow this step

<?php
// configuration
$dbtype= "mysql";
$dbhost= "localhost";
$dbname= "test";
$dbuser= "root";
$dbpass= "admin";

// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$title = 'PHP AJAX is Awesome';
// query
$sql = "SELECT * FROM books WHERE title = ?";
$q = $conn->prepare($sql);
$q->execute(array($title));
$q->setFetchMode(PDO::FETCH_BOTH);
// fetch
while($r = $q->fetch()){
  print_r($r);
}
?>

It's better if you use the Wild Card ? for the attribute (Because its similar to an ORM system)

In Your case for example you should do this

$query = "SELECT COUNT(*) FROM table WHERE home_team=? AND home_score > ?";
$stmt = $db->prepare($query);
$stmt->execute(array($team,$HScore));
$stmt->setFetchMode(PDO::FETCH_BOTH);
$numbers = $stmt->fetch(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM