jackam1 Posted July 18, 2017 Share Posted July 18, 2017 Hi I have a very strange problem. If I give a variable a value, such as $st=g1-9, the piece of code I have will work perfectly. This variable is then used to complete a command to fetch data from a table. However if I use $st = $_GET['q']; instead of above, to assign SAME VALUE to $st, i.e. "g1-9', then the nothing gets fetched. It sounds extremely weird. Here is the code: <?php $st = $_GET['q']; // $st fetched this way is not working even if it can be verified by line below echo "st is ".$st; //$st="g1-9"; // However, if I use this line instead of above, then everything works fine $stmt2 = $pd->prepare("SELECT * FROM table2 WHERE part_number = :pn" ); $stmt2->execute(array(':pn'=>$st)); //print_r($stm2); $row = $stmt2->fetch(PDO::FETCH_BOTH); //$pd->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); //print_r($row); $c=$row["price_each1"];// price for single item ?> Link to comment Share on other sites More sharing options...
requinix Posted July 18, 2017 Share Posted July 18, 2017 Look in your URL. Do you see a "q=g1-9" in it? Perhaps you mean to use $_POST? Link to comment Share on other sites More sharing options...
jackam1 Posted July 18, 2017 Author Share Posted July 18, 2017 g1-9 is fetched successfully, since I echo it the line below, after it has been fetched. Link to comment Share on other sites More sharing options...
Jacques1 Posted July 18, 2017 Share Posted July 18, 2017 Do a var_dump() instead of a simple echo. Check both the content and the length information. Just because the input looks fine at first sight doesn't mean it actually contains the right characters (could be a hidden space somewhere, a dash instead of hyphen etc.) Doing an actual comparison also helps var_dump( $st === 'g1-9' ); Link to comment Share on other sites More sharing options...
requinix Posted July 18, 2017 Share Posted July 18, 2017 g1-9 is fetched successfully,That is not what I asked about. Link to comment Share on other sites More sharing options...
jackam1 Posted July 19, 2017 Author Share Posted July 19, 2017 @Guru You are an absolute genius. I had this question on other forums with full codes from other PHP files and jQuery and none of them spotted it. Since the file was copied from a another tutorial, the final variable included some extra information that when I ECHO'd it, it only showed the string part of it. I had my head scratching for days. You are a star. The actual data contained 'string(6) " G1-9" '. Many thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.