PDO在函数中编写语句[重复]

This question already has an answer here:

I am currently using MySQL with PHP but am looking to start MySQLi or PDO

I have while loops like:

$sql="select from ... ";
$rs=mysql_query($sql);
while($result=mysql_fetch_array($rs))
{
    $sql2="select from table2 where id = $result["tbl1_id"] ";
}

If I put my MySQLi or PDO queries into a function how can I run things like the above? Doing while loops with queries inside the while loops?

Or is if easier to not do the functions at all and just run the prepared statements as normal?

</div>

It's a duplication question like oGeez say, you have to learn how to code PDO in PHP and other before asking question, this is the answer:

$dbh = new PDO("mysql:host=" . HOST . ";dbname=" . BASE, USER, PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $query = 'SELECT * FROM table';
    $stmt = $dbh->query($query);
    $items = $stmt->fetchAll(PDO::FETCH_OBJ);

foreach($items as $item {
 print_r($item);
}

You wouldn't. And to be honest.. Even in the old days you would not do it this way, but like this:

$sql="select from ... ";
$rs=mysql_query($sql);
$ids = array()
while($result=mysql_fetch_array($rs))
  {
  $ids[] = $result["tbl1_id"];
  }

$sql2="select from table2 where id in ".implode(',', $ids) .";

Or even better, you use a join to run the query just once, on all the tables that need to provide info.

In PDO you can do the same thing. Get all the ID's and the execute a query

I usually take the approach of preparing the query and not using a function. Also I am not clear as to what exactly it is that you want. You want to make your queries as quick and efficient as possible so you should not look to run a while look within another while loop.

This is how my PDO queries usually look

My connection:

$host = "localhost";
$db_name = "assignment";
$username = "root";
$password = "";

try {
    $connection = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);

}catch(PDOException $exception){ //to handle connection error
    echo "Connection error: " . $exception->getMessage();
}

MY query:

$query = "SELECT * FROM Table";
               $stmt = $connection->prepare( $query );
               $stmt->execute();

             while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
                   {
                     extract($row);


                     }

the main reason to put it in a function would be if you use the query in multiple files. i have a web app with many queries and i like to keep them in a separate file so that they're easier to track down if i need to make changes. the main thing is that you 1) have to pass your database as a parameter and 2) return the results

function pdoquery($db, $parameter){ $query = "SELECT * FROM table WHERE column=?"; $stmt = $db->prepare($query); $stmt->bindValue(1, $parameter, PDO::PARAM_STR); //or PARAM_INT if (!$stmt->execute()) { echo "Could not get results: (" . $stmt->errorCode . ") " . $stmt->errorInfo; exit; } else $result = $stmt->fetch(); $db = null; return $result; }

but as others have mentioned, if its only used once, there's no need for a function, and looping through the results is best done outside of the function as well. however, it is possible to do it inside the function if you want to.