Setting a complicated variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Setting a complicated variable

Post by kdidymus »

Okay. So this is probably only complicated because I'm a PHP numpty!!

What I WANT to do is something similar to the following:

Code: Select all

$refresh = "$query = "SELECT * FROM tree WHERE urn='$mainurn'";
            $result = mysql_query($query)
            or die ("Couldn't execute query");"
Now I'm guessing that the presence of quotations within quotations is causing my problem but I don't know how to
set this up so that it WILL work!!

In case I've lost anybody, I want to set $refresh to run the above MySQL query as the query is used lots of times throughout my page and I want to save time!

If you want to see what I'm playing with, you'll find it at http://www.didymus.org.uk/new/tree/disp ... ?urn=11164

Thanks in advance.

KD.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Setting a complicated variable

Post by mattpointblank »

You're assigning a string to a string. $query is a variable containing a query string, which you then execute using mysql_query($query). All you need to do is define $query at the top of your script, then you can refer to it again and again throughout the rest.
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: Setting a complicated variable

Post by kdidymus »

So if I just use the code:

Code: Select all

$query $result
in the appropriate places, it will work?

KD.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Setting a complicated variable

Post by mattpointblank »

Almost - you need to just do

$result = mysql_query($query);

which will run that query.
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: Setting a complicated variable

Post by kdidymus »

You're a star. Now I understand. Can't believe I didn't notice that $query was already specified as a string!

Thank you.

KD.
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: Setting a complicated variable

Post by kdidymus »

Okay.

Perhaps I'm trying to run before I can walk here but let's take this one stage further if you would.

I've now got;

Code: Select all

$mainquery = "SELECT * FROM tree WHERE urn='$mainurn'";
at the top of my page and I use;

Code: Select all

$result = mysql_query($mainquery)
          or die ("Couldn't execute query");
At multiple places throughout the page as you suggested.

But there's another query I run throughout but each time there is a changing variable.

The query is;

Code: Select all

$query = "SELECT urn,surname,forename,middlenames,yearofbirth,bloodline FROM tree WHERE urn='[color=#FF0000]$sibling1urn[/color]'";
The bit in red changes each time the query is run.

Any idea how I can save valuable bytes by specifying a majority of the above query as a string and just changing the bit in red each time?

Promise I'm getting the hang of this now!
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: Setting a complicated variable

Post by watson516 »

kdidymus wrote:Okay.

Perhaps I'm trying to run before I can walk here but let's take this one stage further if you would.

I've now got;

Code: Select all

$mainquery = "SELECT * FROM tree WHERE urn='$mainurn'";
at the top of my page and I use;

Code: Select all

$result = mysql_query($mainquery)
          or die ("Couldn't execute query");
At multiple places throughout the page as you suggested.
You are aware you don't need to run the query multiple times if it doesn't change. The query is assigned to the variable so you just use that
kdidymus wrote:
But there's another query I run throughout but each time there is a changing variable.

The query is;

Code: Select all

$query = "SELECT urn,surname,forename,middlenames,yearofbirth,bloodline FROM tree WHERE urn='[color=#FF0000]$sibling1urn[/color]'";
The bit in red changes each time the query is run.

Any idea how I can save valuable bytes by specifying a majority of the above query as a string and just changing the bit in red each time?

Promise I'm getting the hang of this now!
You could save some typing by using:

Code: Select all

SELECT * FROM tree WHERE urn = '$sibling1urn'
That will select everything from the table.
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: Setting a complicated variable

Post by kdidymus »

Thank you Watson516.

I can't use the wildcard (*) for this query as it will overwrite data that I need elsewhere.

However, after some experimentation I have (yet again) answered my own question.

First I specified:

Code: Select all

$fields = "urn,surname,forename,middlenames,yearofbirth,bloodline"; // Specify common fields to extract
And then I use:

Code: Select all

$query = "SELECT $fields FROM tree WHERE urn='$motherurn'";
$result = mysql_query($query)
          or die ("Couldn't execute query");
This saves about 3 kilobytes overall which doesn't sound like a lot but every little helps!

Thank you all once again for your help. If there is a better way, I'd love to hear it.

KD.
Post Reply