PDA

View Full Version : php tech question



billbenson
09-02-2008, 03:30 PM
I've been trying to find the answer on this for a few days. What I'm doing is a lot more complicated than this, but here is a simple example of the problem. I have a page with some variables on it. I select some text that includes a variable names from a mysql db. I need to insert the variable into a variable. Here is an example:

this works:

$var='bob';
$var1="my name is $var";
echo $var1;
result: my name is bob


This doesn't work:

$var='bob';
$var1=SELECT RESULT ( my name is $var) //this is selected from the db
echo $var1
result: my name is $var

I've tried adding quotes to the db entry, concertinaing it ie $var1='my name is '.$var; Anything else I can think of. It treats the variable within a variable as text when pulled from the db, but treats it as a variable when everything is done on the page.

vangogh
09-02-2008, 07:32 PM
You have to do more to get php and mysql talking. Unfortunately it's not a one step process.

Here's a quick article from O'Reilly on using mysql from php (http://www.onlamp.com/pub/a/php/2004/02/19/php_foundations.html).

Here's the basic process from the article (after you've first connected to the DB)


$result = mysql_query("SELECT * FROM books");

if(!$result) die("Query Failed.");

while($row = mysql_fetch_row($result)) {

/*
$row[0] now contains the first column of the current row,
index 1 is the second, etc.
*/

}


Usually you'll then close the DB connection after getting the results.

You also see people first defining a variable called $query and setting it equal to the query they want to use. So

$query = "SELECT * FROM books";
$result = mysql_query($query);

That lets you set the rest as a function that gets passed the query.

vangogh
09-02-2008, 07:34 PM
Having said all that I'm thinking you might know everything I just said in the post above. I might need to see the exact code to offer a solution so feel free to send via PM if you want.

billbenson
09-02-2008, 09:55 PM
I'll send you a pm VG. This is really beyond the scope of this forum.

vangogh
09-03-2008, 01:31 AM
I got your PM and as soon as I can I'll take a look.

I don't mind if you post coding questions here. We may not have a large group of coders among us at the moment, but we have some and more questions might help attract more members with those skills. In time and if we get enough coding discussion going, I'd like to set up a forum devoted to it.