Jump to content

[SOLVED] Need help using variables in query.


pocobueno1388

Recommended Posts

Hello :)

 

I am trying to use variables in my query, but I'm not having any luck.

 

Here is the query:

<?php

    $getEntries = mysql_query("SELECT 
                                  (h.$stat1 + h.$stat2 + h.$stat3) AS total, s.showID, s.horseID
                               FROM 
                                  horses h, show_entries s
                               WHERE
                                 s.showID = '{$show['showID']}' AND
                                 s.horseID = h.horseID")or die(mysql_error());
?>

 

The part I am having trouble with is:

(h.$stat1 + h.$stat2 + h.$stat3)

 

It is returning "24" for each row, and that isn't what it should be returning, I have no idea where it is getting that number from.

 

I'm thinking that when I put

h.$stat1

it is thinking I am wanting to concate, when I don't. I actually want it to display the dot as that is how to define which table I am selecting that value from.

 

How can I do this and get the right results?

 

Any help is greatly appreciated, thanks xD

Nope, that gave me a syntax error.

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''conformation' + h.'intelligence' + h.'strength') AS total, s.showID, s.horseID ' at line 2

so ur variables are interpreted properly , and why don't u try a left join

 


SELECT horseid,(h.$stat1 + h.$stat2 + h.$stat3) AS total from horses as h left join show_entries as s on h.horseid=s.horseid where  s.showID = '{$show['showID']}' 

 

 

 

 

That is giving me the same result as I had before, it is returning "24" as total from all the rows...maybe this isn't a problem with the query, and I'm just making a mistake. Let me go check it out again to make sure.

 

and one more thing u have used h.horseID in where condition which is not there in selected columns so it might be returning Cartesian product no of records.

 

I added h.horseID to the select part and still got the same result.

Ahhhh! The query has been working perfectly the entire time. I was adding EVERY row in the DB instead of just the 3 I selected. Also, the 3 that I selected just happened to all add up to 24 on each row...that was just a major coincidence.

 

Thanks for your help xD

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

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.