dreamMyDream2014 2014-10-14 17:21
浏览 67
已采纳

PDO预处理语句:如果缺少值,则使用NULL

I have a prepared statement to store in my db some data coming from an API:

$sql = "INSERT INTO `standings` (`league_id`, `season`, `position`, `team_id`, `played`, `playedathome`, `playedaway`, `won`, `draw`, `lost`, `numberofshots`, `yellowcards`, `redcards`, `goals_for`, `goals_against`, `points`)
VALUES (:league_id, :season, :position, :team_id, :played, :playedathome, :playedaway, :won, :draw, :lost, :numberofshots, :yellowcards, :redcards, :goals_for, :goals_against, :points)
ON DUPLICATE KEY UPDATE `league_id`=:league_id, `season`=:season, `position`=:position, `team_id`=:team_id, `played`=:played, `playedathome`=:playedathome, `playedaway`=:playedaway, `won`=:won, `draw`=:draw, `lost`=:lost, `numberofshots`=:numberofshots, `yellowcards`=:yellowcards, `redcards`=:redcards, `goals_for`=:goals_for, `goals_against`=:goals_against, `points`=:points";

$prep = $pdo->prepare($sql);

$prep->bindParam(':league_id', $league_id);
$prep->bindParam(':season', $season);
$prep->bindParam(':position', $position);
$prep->bindParam(':team_id', $team_id);
$prep->bindParam(':played', $played);
$prep->bindParam(':playedathome', $playedathome);
$prep->bindParam(':playedaway', $playedaway);
$prep->bindParam(':won', $won);
$prep->bindParam(':draw', $draw);
$prep->bindParam(':lost', $lost);
$prep->bindParam(':numberofshots', $numberofshots);
$prep->bindParam(':yellowcards', $yellowcards);
$prep->bindParam(':redcards', $redcards);
$prep->bindParam(':goals_for', $goals_for);
$prep->bindParam(':goals_against', $goals_against);
$prep->bindParam(':points', $points);


foreach($result->TeamLeagueStanding as $standing){

    $team_id = $standing->Team_Id;
    $played = $standing->Played;
    $playedathome = $standing->PlayedAtHome;
    $playedaway = $standing->PlayedAway;
    $won = $standing->Won;
    $draw = $standing->Draw;
    $lost = $standing->Lost;
    $numberofshots = $standing->NumberOfShots;
    [etc...]


    $prep->execute() or print("PDO execute error: ".$prep->errorInfo()[2]);

The problem is that some leagues don't provide the NumberOfShots value, so $standing->NumberOfShots is an empty string, causing the error:

PDO execute error: Incorrect integer value: '' for column 'numberofshots' at row 1

And that row doesn't get inserted.

How can I make something like

if($numberofshot == "")
    $numberofshot = NULL;

or tell PDO to use the default value instead of throwing an error, if the value is incorrect?

  • 写回答

2条回答 默认 最新

  • douxun7992 2014-10-14 17:35
    关注

    http://php.net/manual/en/pdo.setattribute.php

    try:

    setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?