Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
1  
If you dump your variable before trying to put it in the query, what do you get ? –  Bartdude May 21 '13 at 15:49
3  
But this is not your INSERT query, how could we help you? –  codingbiz May 21 '13 at 15:53
    
Is SHPNUM a numeric or string type? If string, you are missing some single quotes before and after the value. –  George Cummins May 21 '13 at 16:03
2  
@codingbiz The OP is trying to insert PHP values into the query, not into the database. –  George Cummins May 21 '13 at 16:03
    
We really can't help you without much more code. –  maythesource.com May 21 '13 at 16:04

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer
$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";

Added quotes(') in the where condtion

share|improve this answer
    
OP as he described is trying to put the value into the statement he is building, not an issue of quotes. –  cerd May 21 '13 at 16:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.