doushao8399 2016-08-16 06:47
浏览 22
已采纳

PHP / MySQL到PDO

I want to change MySQL to PDO:

$mapa = mysql_fetch_array(mysql_query("select * from mapa where id = ".$postac['mapa']." limit 1"));
$mapa_d = mysql_query("select * from mapa_d where mapa = ".$mapa['id']." ");

PHP:

 $_SESSION['postac'] = $_POST['postac'];

try like this so far:

$stmt = $pdo->prepare("SELECT * FROM mapa WHERE id=:mapa");  
$stmt->bindValue(':mapa', $postac, PDO::PARAM_STR);  
$stmt->EXECUTE();  
$postac = $stmt->fetchAll(PDO::FETCH_ASSOC); 

mysql update:

mysql_query("update postac set logged = 1 where id = ".$_SESSION['postac']." limit 1");

PDO:

$stmt = $pdo->prepare("update postac set logged = 1 where id:postac");  
$stmt->bindValue(':postac', $_SESSION, PDO::PARAM_STR);  
$stmt->EXECUTE();  
$_SESSION = $stmt->fetchAll(PDO::FETCH_ASSOC); 

Does not work.

  • 写回答

1条回答 默认 最新

  • douzhi1879 2016-08-16 10:43
    关注

    Pre-Answer Note:

    I assume you have already set up a PDO connection construct ($pdo) before trying to run your PDO queries.

    $mapa = mysql_fetch_array(
              mysql_query("select * from mapa WHERE id = ".$postac['mapa']." limit 1"));
    $mapa_d = mysql_query("select * from mapa_d WHERE mapa = ".$mapa['id']." ");
    

    PHP:

     $_SESSION['postac'] = $_POST['postac'];
    

    try like this so far:

    $stmt = $pdo->prepare("SELECT * FROM mapa WHERE id=:mapa");  
    $stmt->bindValue(':mapa', $postac, PDO::PARAM_STR);  
    $stmt->EXECUTE();  
    $postac = $stmt->fetchAll(PDO::FETCH_ASSOC);
    

    PART 1:

    Be Consistent

    Your original statement uses a value $postac['mapa'] as an id reference in the MySQL_ query, but then your PDO statement you are passing the whole array as a value into the PDO query.

    First, MySQL: id ==> $postac['mapa']

    Second, PDO: id ==> $postac

    So this is causing an immediate issue as you're passing a whole array in to PDO which is somehow expected to extract one value from this array. This array is being classed as a string with your PDO::PARAM_STR declaration so this is preventing the query from using this value, as it doesn't fit what it's told to expect.

    Therefore this returns a NULL query.

    So to fix it,

     $stmt = $pdo->prepare("SELECT * FROM mapa WHERE id=:mapa");  
     $stmt->bindValue(':mapa', $postac['mapa'], PDO::PARAM_STR);  
     $stmt->execute();  
     $postac = $stmt->fetchAll(PDO::FETCH_ASSOC);
    

    Part 2:

    Syntax

    $stmt = $pdo->prepare("update postac set logged = 1 where id:postac");  
    $stmt->bindValue(':postac', $_SESSION, PDO::PARAM_STR);  
    $stmt->EXECUTE();  
    $_SESSION = $stmt->fetchAll(PDO::FETCH_ASSOC);
    

    As above, you're passing the whole $_SESSION array as a PARAM_STR value, so it's returning VOID /NULL. You also have a syntax fault that you're using WHERE id:postac, but you really mean WHERE id = :postac be careful of missing out syntax such as = !!.

    PART 3:

    Error Checking

    It is well worth exploring and learning how to get useful error feedback on PHP PDO, as it will save you posting to StackOverfow X times a day (hopefully!)! There is a good answer here about how to setup PDO to output errors. It is also well worth browsing the PHP Manual for PDO error checking details.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效