Jump to content
FreakingOUT

Conversion of text data from $variables issue

Recommended Posts

ARRAY Example scaled down to two (renamed example only) fields

$data = array(
"field1" => "Text within quotes processes OK",
"field2" => "Text within quotes processes OK"
);

The frustating thing has been trying to figure out how to
substitute actual text data contained in these 2 example
variables to use data from a MySQL query:

$text1 = $db_field['sampletext1'];
$text2 = $db_field['sampletext2'];

I have tried:

$data = array(
"field1" => $text1,
"field2" => $text2
);

*** (AND) ***

$data = array(
"field1 => '$text1',
"field2 => '$text2'
);

But neither approach works, even though the text in both
$text1 and $text absolutely works IF I (manually) type the
actual text within double-quotes like this:

$data = array(
"field1 => "text1",
"field2 => "text2"
);

I also tried using strval but that did not work.

So how on earth to get the (actual) text from $text1 and
$text2 into the two array fields as-if the contained text
would  (manually) be typed in double-quotes is the Mystery.

It is probably something simple, so maybe I don't know
exactly what to search for in seeking code snippet examples?

Thanks for any enlightenent.

-FreakingOUT

 

 

Edited by FreakingOUT

Share this post


Link to post
Share on other sites

Instead of asking about your attempted solution to your problem, tell us what the real problem is you are trying to solve. You said you want to " use data from a MySQL query ". That sounds like that would be the real problem. Post the code you have tried to retrieve data from MySQL.

Share this post


Link to post
Share on other sites
Quote

I have tried: ...

the first method should work.

Quote

*** (AND) ***

the second method won't work because php variables contained within a single-quoted string are not parsed/replaced with the variable's contents.

for the first method, there could be a number of reasons that it doesn't appear to work -

1) the variables don't exist at the point where you are referencing them.

2) the variables don't actually contain what you think or did but have been cleared by some of your code.

3) the method or place you are using to view the result or otherwise determine that this doesn't work, is at fault.

4) there could be a variable scope problem.

edit: 5) you could be seeing the result from double page requests (different browsers/client programs do this for various reasons), where the first request does work, but the second doesn't actually find/retrieve the data.

it would take seeing all the relevant code, from the point where you are querying for and retrieving the data through to where you are viewing the incorrect result.

do you have php's error_reporting set to E_ALL and display_errors set to ON so that php would help you by reporting and displaying all the errors it detects.

Edited by mac_gyver

Share this post


Link to post
Share on other sites

Thanks for your replies.

There is no problem with retrieving the data from MySQL, which I also verified echo'ing the $variables via the assignments from the mysqli query.  These printed fine on the screen (inside a <textarea> box).  The actual text content (inside example $text1 and $text2) is clean as a whistle.

I then (manually) typed that same (real) data text inside of double-quotes into the same $data (array) which uploaded everything fine to a "Realtime Processing" destination with verified acceptance.   There are passwords and and a special API involve, but the end result cannot be viewed by here --- because it displays on a 3rd party private password protected web system.  

I've used the same browser involved to successfully make things work WHEN I (manually) put the text content into the 2 fields (other fields in the array are for confidential email address, password & the API key.

FYI, I continually ran all the PHP & MySQL combo code through a Code Checker each time I tried something different, with the result being "No Issues Found".  THAT is what baffles me.

What I will have to do is redact or <snip> all confidential info from the code (on another computer), and use some dummy field and data names/content to maintain Confidentiality.

- FreakingOUT

 

 

 

Share this post


Link to post
Share on other sites

Do you have PHP error checking turned on to see what possible errors there are?  If so how about showing us the code that does the query and the code that extracts the two data values from the results in full context so that we may follow it better.

Share this post


Link to post
Share on other sites
7 hours ago, FreakingOUT said:

What I will have to do is redact or <snip> all confidential info from the code (on another computer), and use some dummy field and data names/content to maintain Confidentiality.

any database, api or web site url/host information and any username/password/key values should be implemented in your code using variables or defined constants that are contained in a separate .php file and required by your main code, so that you don't need to be editing them out of the main code in order to post it for getting help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.