Jump to content

One variable works, same value fetched doesn't


jackam1
Go to solution Solved by 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' );
  • Like 1
Link to comment
Share on other sites

  • Solution

@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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.