MYSQL SELECT max(ID)WHERE Onderdeel_ID = $ Onderdeel_ID

时间:2012-12-03 09:43:14

标签: mysql select

  

$ result = mysql_query(“SELECT max(Cardex_ID)FROM cardex WHERE Onderdeel_ID = $ Onderdeel_ID”);           if(!$ result){               死('无法查询:'。mysql_error());           }

    $Cardex_ID = mysql_result($result, 0, 'Cardex_ID');

    echo $Cardex_ID;

    mysql_query("UPDATE cardex SET PADate=$PAdate, PAhours=$PAhours, PAcycles=$PAcycles, PAlanding=$PAlanding WHERE Cardex_ID=$Cardex_ID");

1 个答案:

答案 0 :(得分:1)

我建议您使用PDO连接到数据库而不是使用该通用连接。 此外,如果$ Onderdeel_ID是一个数字,您不应该需要""

   $db = new PDO ('mysql:host=localhost;dbname=DB_NAME','DB_USER','DB_PASS');
    $sql = 'SELECT max(Cardex_ID) FROM cardex WHERE Onderdeel_ID='. $Onderdeel_ID;
    $result = $db->query($sql)->fetch(PDO::FETCH_ASSOC);
    echo 'SELECT';
    if (!$result) {
      echo 'RESULT';
    }

注意:同样有效且首选使用语句prepare来阻止SQL注入:

    $db = new PDO ('mysql:host=localhost;dbname=DB_NAME','DB_USER','DB_PASS');
    $db->prepare ( 'SELECT max(Cardex_ID) FROM cardex WHERE Onderdeel_ID=:Onderdeel_ID');
    $result = $db->execute( array( ':Onderdeel_ID' => $Onderdeel_ID ) );

此外,如果您遇到问题,请检查您的$Onderdeel_ID是否已通过。一个简单的var_dump($Onderdeel_ID);可以解决问题