douzhaolu4839 2013-05-21 15:47
浏览 26
已采纳

PHP不将变量插入mySQL查询

I am having trouble getting one of my variable to be successfully inserted into my mySQL query. Here is the query string:

$strShipMethodInfo = "SELECT BAMMDD, BAYY, BAXXX, ORDNUM, SHPNUM, \"SHIPMENT METHOD\", \"SHIPMENT STATUS\" FROM SHPPMTHD WHERE BAMMDD = " . $BAMMDD . " AND BAYY = " . $BAYY . " AND BAXXX = " . $BAXXX . " AND SHPNUM = " . $arrShippingInfo[$x]['SHPNUM'] . " AND ORDNUM = '" . $ORDNUM . "' ORDER BY SHPNUM";

I am using $arrShippingInfo[$x]['SHPNUM'] in another query that is very similar, and it is putting the variable in that one. However, when I do it with this query, it comes back as blank. All the other values (BAMMDD, BAYY, BAXXX, etc.) are successfully put in, but the variable for SHPNUM does not get put in.

I have tried everything I can think of, thinking that it might be a quote in the wrong place, but I have been unsuccessful. Could anyone please help me figure this out? Thanks.

EDIT: I did a print_r on the string, and it printed twice... once with the SHPNUM correctly inserted and once with it blank. Turns out it was a logic error in the for loop I was using (I needed to run the query for each shipment in the order). < somehow got changed to <= so once I changed that it worked. Thank you everyone for your responses.

  • 写回答

2条回答 默认 最新

  • dongyikong6207 2013-05-21 16:42
    关注

    I would say there is something wrong with how you are accessing that array value, either with the $x variable, or in how you are passing in the raw array value to the string (hard to say without the rest of your code: try assigning it to $SHPNUM and putting $SHPNUM into the statement?).

    Within your code try running an:

    echo $x; echo $arrShippingInfo[$x]['SHPNUM'];
    var_dump($arrShippingInfo);
    

    and you should be able to find the issue.

    Using dummy data with your statement and running that code through on my end as:

    <?php
    
    $BAMMDD = 'bamddd';
    $BAYY = 'BAYY';
    $BAXXX = 'BAXXX';
    $arrShippingInfo[0]['SHPNUM'] = '54';
    $ORDNUM = 555;
    
    $x = 0;
    $strShipMethodInfo = "SELECT BAMMDD, BAYY, BAXXX, ORDNUM, SHPNUM, \"SHIPMENT METHOD\", \"SHIPMENT STATUS\" FROM SHPPMTHD WHERE BAMMDD = " . $BAMMDD . " AND BAYY = " . $BAYY . " AND BAXXX = " . $BAXXX . " AND SHPNUM = " . $arrShippingInfo[$x]['SHPNUM'] . " AND ORDNUM = '" . $ORDNUM . "' ORDER BY SHPNUM";
    
    echo "<pre>$strShipMethodInfo</pre>";
    ?>
    

    Produces:

    SELECT BAMMDD, BAYY, BAXXX, ORDNUM, SHPNUM, "SHIPMENT METHOD", "SHIPMENT STATUS" FROM SHPPMTHD WHERE BAMMDD = bamddd AND BAYY = BAYY AND BAXXX = BAXXX AND SHPNUM = 54 AND ORDNUM = '555' ORDER BY SHPNUM

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘