Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read variable from mysql_database

Status
Not open for further replies.

AcidReignX

Programmer
Feb 28, 2005
32
US
Okay, this might be a little unusual.... well, actually it's probably highly irregular for all I know, but I'm trying to get this to work a certain way and I'm lost.

I want to read information from a mysql query and return it as an echo, but the problem is that the variables aren't showing up. They show up with the $ sign, which... is not what I had expected.

I have no problem calling the field, and the field contains something like this:
$query = "SELECT foo FROM table WHERE x=1";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_BOTH);

// so now $row['foo'] == "<td>$hello</td>";

// I do this:

echo $row['foo'];

// And it returns: "$hello" inside of the table, even though $hello is set to something like "How are you".

... so... I'm lost. How do I get it to return "How are you" like it normally would if I hadn't called it from a database? I've tried a couple formatting techniques but none have worked for me. :/ Slashes don't bother it... htmlchars doesn't seem to change anything.. entities... nothing. I'm stuck.

Help me oh wise php guys...
 
What value is actually being returned from the database? Verify, don't assume. Verify by taking your query and running it from your preferred MySQL management tool and seeing what is returned.

It could be that the script is working perfectly, but that the data is bad.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Uh oh... how might I accomplish that? I don't really use mysql tools, except PhpMyAdmin, which I'm not sure if that even counts for much.
 
Oh, and I should probably mention that I inserted the information manually through phpmyadmin. So it should be exactly as I inserted it, which is more or less the example I had provided: <td>$hello</td>

As if I had done something exactly like that (but with different variables). But when I return it, the echo doesn't work...
 
To confirm: You're intending to store a variable $hello in a string in the database?

[cheers]
Cheers!
Laura
 
PHPMyAdmin should allow you to run an arbitrary query and see what is returned. It is imperative to make sure that your data is clean before you start trying to dissect your code. Otherwise, you can't relay on your code debugging.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
You have this problem because the string really does contain
Code:
<td>$hello</td>

This is like assigning a string in PHP with single quotes vs double quotes.
Code:
$hello = 'How are you';
$x = '<td>$hello</td>';
$y = "<td>$hello</td>";
echo $x.'<br>';
echo $y;
Would result in a cell containing $hello and a cell containing How are you.

How to solve your problem requires knowledge of what you're trying to accomplish.

Ken
 
To confirm: You're intending to store a variable $hello in a string in the database?"

Not exactly. It's more like this:

$hello = "Hi, whats up?";

$query = "SELECT * FROM foo WHERE bar = 1";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_BOTH);

// $row['hellotest'] is now equal to "<table><tr><td>$hello</td></tr></table>" without the quotes.

echo $row['hellotest'];

// This, unfortunately, returns "<table><tr><td>$hello</td></tr></table>" instead of "<table><tr><td>Hi, whats up?</td></tr></table>" (minus the html tabs, because those are in proper formatting. The $hello appears in a table as it should.

Sleipnir, I'm in the process of looking for that query info, so I'll post back soon to try and confirm it if I can find the right operation.
 
Ack. Ken, once you posted that I realized I had overlooked that aspect, but even changing the quote types doesn't seem to help me.

Also, Sleipner, as far as I can tell, the information is being held exactly like that. <td>$hello</td>
 
Now I got it:

You stored this sring-value in the database:
"<table><tr><td>$hello</td></tr></table>"

and now you want to echo your result and this $hello should contain your "Hi whats up" ...

That sounds quite tricky ...
I'm not sure if it is possible then.

I would use a workaround like storing an alias in the sting like
"<table><tr><td>SAYHELLO</td></tr></table>"
and then do some replace-function (replace "SAYHELLO" by $hello) before echoing out the value

HTH
 
Code:
$hello = "hi there blah";
$mydata = "<td>".$hello."</td>";
 if ($row['foo'] == $mydata)
 echo $mydata;
Difficult to grasp this but is that what you are trying to do ??

Binary Intelligence, true or false?
 
You can also try to use variable variables, which are available in php. Typically you can do something like this:

Code:
$my_var = hello;
$hello = "This is a test"

echo $my_var;             // returns "hello"
echo $$my_var;            // returns "This is a test"
I'm not sure that this would work exactly in the format that you are trying but hopefully it gives you some direction.

I'm also a little confused about what data is being stored in the table. If it is just the $hello variable, then you should easily be able to make the above example work. If you are storing the table data too, why not just have the script generate the table and row tags and leave only the information in the database.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top