Page 1 of 1

Setting a complicated variable

Posted: Thu Feb 19, 2009 9:56 am
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.

Re: Setting a complicated variable

Posted: Thu Feb 19, 2009 10:01 am
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.

Re: Setting a complicated variable

Posted: Thu Feb 19, 2009 11:01 am
by kdidymus
So if I just use the code:

Code: Select all

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

KD.

Re: Setting a complicated variable

Posted: Thu Feb 19, 2009 11:18 am
by mattpointblank
Almost - you need to just do

$result = mysql_query($query);

which will run that query.

Re: Setting a complicated variable

Posted: Thu Feb 19, 2009 11:50 am
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.

Re: Setting a complicated variable

Posted: Thu Feb 19, 2009 1:22 pm
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!

Re: Setting a complicated variable

Posted: Thu Feb 19, 2009 5:04 pm
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.

Re: Setting a complicated variable

Posted: Fri Feb 20, 2009 1:51 am
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.