-3

我如何将mysql代码更改为pdo并将结果存储在variabel中

我在 index.php 内容中使用此代码:

$games_sql = mysql_query("SELECT id,col1,col2,now FROM tblname ORDER BY now ASC LIMIT 9");
$gn=0;
while($game_get=mysql_fetch_array($games_sql))
{
$id = $game_get['id'];
$col1= $game_get['col1'];
$col2= $game_get['col2'];
$now = $game_get['now'];
$gametimeanddate = jdate("l d M y time G:i",$now);
$gamedate = jdate("l d M y",$now);
$gametime = jdate("G:i",$now);
$gn++;
if(($gn%2)==0){
    $class='background-color:#EEE'; , ..... ?>

并使用这个变量:

<?php echo $id;?>&title=<?php echo func1($col1).'-'.func1($col2);?>

和 pdo 连接包括内容:

$conn = new PDO('mysql:host=localhost;dbname

我想将我的查询更改为 pdo 并将查询结果存储到变量并在 php 代码中使用它

4

2 回答 2

-1

All you have to do is fetch, just like mysql and mysqli.

于 2013-08-05T15:19:28.893 回答
-1

尝试这个:

<?php
$games_sql = "SELECT id,col1,col2,now FROM tblname ORDER BY now ASC LIMIT 9";
$sth = $conn->query($games_sql);
$sth->setFetchMode(PDO::FETCH_ASSOC);

$gn=0;
while ($game_get = $sth->fetch()) {

$id = $game_get['id'];
$col1 = $game_get['col1'];
$col2 = $game_get['col2'];
$now = $game_get['now'];

$gametimeanddate = jdate("l d M y time G:i",$now);
$gamedate = jdate("l d M y",$now);
$gametime = jdate("G:i",$now);

//SAVE VALUES IN ARRAY
$items[] = array('id' => $id, 'col1' => $col1, 'col2' => $col2, 'now' => $now, 'gametimeanddate' => $gametimeanddate, 'gamedate' => $gamedate, 'gametime' => $gametime); 

$gn++;

    if(($gn%2)==0){
        $class='background-color:#EEE';
    }
}
?>

使用数组值示例:

<?php
echo '<table>';
    foreach($items as $value)
    {
        echo '<tr>';
        echo '<td>'. $value['id'] .'</td>';
        echo '<td>'. $value['col1'] .'</td>';
        echo '<td>'. $value['col2'] .'</td>';
        echo '<td>'. $value['gametimeanddate'] .'</td>';
        echo '<td>'. $value['gamedate'] .'</td>';
        echo '<td>'. $value['gametime'] .'</td>';
        echo '</tr>';
    }
echo '</table>';
?>

用你的例子:

<?php
foreach($items as $value)
{
    echo $value['id']?>&title=<?php echo func1($value['col1']).'-'.func1($value['col2']);
}
?>

PDO:

几个很好的教程来学习一些关于 PDO 的基础知识

NetTuts

phpro

编辑:

您可以将数组值提取到变量中。例子:

<?php

$items = array("color" => "blue", "size"  => "medium", "shape" => "sphere");

extract($items);

echo $color;

?>

用你的例子,你只需要

extract($game_get);
于 2013-08-06T10:02:37.840 回答