Jump to content

One variable works, same value fetched doesn't


jackam1

Recommended Posts

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

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

@Guru You are an absolute geniusI 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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.