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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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']}' 

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.