duanbing6955 2013-08-05 15:14
浏览 33
已采纳

将mysql更改为pdo并将结果存储在变量中

how can i change mysql code to pdo and store results in variabel

i use this code in index.php content:

$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'; , ..... ?>

and use this variables:

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

and pdo connection include content:

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

i want change my query to pdo and store query result to variables and use that in php codes

  • 写回答

2条回答 默认 最新

  • doupo1865 2013-08-06 10:02
    关注

    Try this:

    <?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';
        }
    }
    ?>
    

    USE ARRAY VALUES EXAMPLE:

    <?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>';
    ?>
    

    WITH YOUR EXAMPLE:

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

    PDO:

    A couple of nice tutorials to learn some basics about PDO

    NetTuts

    phpro

    EDIT:

    You could extract your array values to variables. Example:

    <?php
    
    $items = array("color" => "blue", "size"  => "medium", "shape" => "sphere");
    
    extract($items);
    
    echo $color;
    
    ?>
    

    With your example you just need

    extract($game_get);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题