SitePoint Sponsor

User Tag List

Results 1 to 18 of 18

Thread: $item in query

  1. #1
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question $item in query

    I have the following code:

    PHP Code:
        $sql "SELECT *
                FROM atm_comments
                WHERE comment_"
    .$item."id = $".{$item}."_id
                AND comment_auth = 1"

    I want to have the $item variable on the second line to be parsed first to make up the variable name at the end of $ride_id (when $item is set to 'ride' anyway!)

    The code above fails. I can;t see any errors as this code in running in a tab script and for some reason the tab content does not show when it fails. When it does work however then the content shows fine so i do now when it is successful.

    Please help

    Thanks

    Regards,
    Neil

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should try to read some sql tutorials, as you don't seem to grasp what you are doing now (no offenses, really).

    The line
    Code:
    WHERE comment_".$item."id = $".{$item}."_id
    almost certainly not what you want.

    A database is composed of tables, which are composed of columns.
    Each column has a name.
    When you do query the database, on a SELECT statement, you must/should specify which column should have which value.
    In the query up there, you switch the column name on each query (which is either a badly designed db schema result, or an error).
    So, for each item you try to get this query result, you try to get the content of comment_{item_name}_id.
    So, if you have 2'000 items, you should have a table with more than 2'000 columns ?

    No, you should have an table items, and another table comments, and have a query like
    Code:
    SELECT *
    FROM atm_comments c, atm_items i
    WHERE c.comment_auth = 1
    AND c.item_id=i.item_id
    That way, your table holding the comment stay simple, and the table holding your items data too.

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should try to read some sql tutorials, as you don't seem to grasp what you are doing now (no offenses, really).

    The line
    Code:
    WHERE comment_".$item."id = $".{$item}."_id
    almost certainly not what you want.

    A database is composed of tables, which are composed of columns.
    Each column has a name.
    When you do query the database, on a SELECT statement, you must/should specify which column should have which value.
    In the query up there, you switch the column name on each query (which is either a badly designed db schema result, or an error).
    So, for each item you try to get this query result, you try to get the content of comment_{item_name}_id.
    So, if you have 2'000 items, you should have a table with more than 2'000 columns ?

    No, you should have an table items, and another table comments, and have a query like
    Code:
    SELECT *
    FROM atm_comments c, atm_items i
    WHERE c.comment_auth = 1
    AND c.item_id=i.item_id
    That way, your table holding the comment stay simple, and the table holding your items data too.

    But in the end, without seeing your db schema, we cannot offer much help.

  4. #4
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You're asking about variable variables. In this case you'd want

    comment_".$item."id = ${$item}_id

    So if $item='ride' this would turn into something like:

    comment_rideid = ${ride}_id

    and would place the value of $ride in there for ${ride}
    - Robert

  5. #5
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by tripy View Post
    You should try to read some sql tutorials, as you don't seem to grasp what you are doing now (no offenses, really).

    The line
    Code:
    WHERE comment_".$item."id = $".{$item}."_id
    almost certainly not what you want.

    A database is composed of tables, which are composed of columns.
    Each column has a name.
    When you do query the database, on a SELECT statement, you must/should specify which column should have which value.
    In the query up there, you switch the column name on each query (which is either a badly designed db schema result, or an error).
    So, for each item you try to get this query result, you try to get the content of comment_{item_name}_id.
    So, if you have 2'000 items, you should have a table with more than 2'000 columns ?

    No, you should have an table items, and another table comments, and have a query like
    Code:
    SELECT *
    FROM atm_comments c, atm_items i
    WHERE c.comment_auth = 1
    AND c.item_id=i.item_id
    That way, your table holding the comment stay simple, and the table holding your items data too.

    But in the end, without seeing your db schema, we cannot offer much help.

    I do know exactly what I am doing and I know what I am trying to do is correct but the problem is how it is written is incorrect which is what I am asking for help with.


    Quote Originally Posted by QMonkey
    You're asking about variable variables. In this case you'd want

    comment_".$item."id = ${$item}_id

    So if $item='ride' this would turn into something like:

    comment_rideid = ${ride}_id

    and would place the value of $ride in there for ${ride}
    This is the sort of thing I was asking about. But that doesn;t seem to work either.

    Any more ideas?

  6. #6
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I use what QMonkey suggested, the query being run (I echoed it out to see what was being run) was something like this:

    SELECT * FROM atm_comments WHERE comment_rideid = Bouncing Bugs'_id' AND comment_auth = 1

    I have absolutley no idea how it is getting the words 'Bouncing Bugs' as that is stored in a completely different table all together: atm_rides.ride_name. I am only really concerned with atm_comments and the value of $ride_id which should be where 'Bouncing Bugs' is in the query above. I need to use the $item variable as this code is on a page called comments.inc.php which is included on the page about rides, areas, services and so on, so for example on the areas page $item would be equal to area and the query qould swap $item to area and then on the last part of the second lione of the query once $item has been swpped to area it would make up the variable $area_id.

    Does that help you understand it a bit more?

    I just don;t see HOW it is getting the words 'Bouncing Bugs'


    Update: I have just noticed that if I refresh the page then a different ride name is put in where 'Bouncing Bugs' is above.

    Please help me figure this all out.

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Upto here
    PHP Code:
    "comment_".$item."id" 
    this is fine it will be :
    PHP Code:
    comment_abcid 
    if the variable $item has the value 'abc' assigned to it.

    But the value you are tying to compare with this field name what i guess is you are trying to get from a variable name $abc_id if the variable $item holds same 'abc' here.

    Am i right?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  8. #8
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,
    I appreciate your help but really do not understand what you mean.

    This part:
    PHP Code:
    comment_".$item."id 
    is fine!


    The part I am having problems with is:
    PHP Code:
    ".${$item}."'_id' 
    Thanks

    Regards,
    Neil

  9. #9
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok see this example which will help you out I hope:
    PHP Code:
    $item         'abc';
    $abc_id     '1';
    $var         $item "_id";
    $var         = $$var;
    $sql "SELECT * FROM atm_comments WHERE comment_" $item "_id=" $var
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  10. #10
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Managed it with this:

    PHP Code:
        $var $item.'_id';
        
    $var = $$var;
        
    $sql "SELECT *
                FROM atm_comments
                WHERE comment_"
    .$item."id = ".$var."
                AND comment_auth = 1"

    I know it works but don;t know how it works. Please can you explain.

    Thanks

    Regards,
    Neil

  11. #11
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here
    PHP Code:
    $var $item.'_id'
    The variable $var will hold a string 'abc_id' if $item holds 'abc' on it. And if you have known to PHP Variables Variable then the following line will create a variable of the variable with the string 'abc_id' as $abc_id:
    PHP Code:
    $var = $$var
    And now the value stored in the variable $abc_id will be assigned to $var.

    Is it clear now?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  12. #12
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, Thankyou so much

  13. #13
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, As this is of a similar nature I have posted it in here:

    I have this code:

    PHP Code:
            $formvalue_rideid $ride_id;
            
    $formvalue_rideid htmlentities($formvalue_rideid);
            
    $formvalue_rideid mysql_real_escape_string($formvalue_rideid); 
    How can I replace all occurrences of the word 'ride' with the $item variable?

  14. #14
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rctneil View Post
    Right, As this is of a similar nature I have posted it in here:

    I have this code:

    PHP Code:
            $formvalue_rideid $ride_id;
            
    $formvalue_rideid htmlentities($formvalue_rideid);
            
    $formvalue_rideid mysql_real_escape_string($formvalue_rideid); 
    How can I replace all occurrences of the word 'ride' with the $item variable?
    PHP Code:
    $newstring str_replace ('ride'$item$oldstring); 

  15. #15
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    No No No, I need it doing but in the context of what we have previously been talking about in this thread. If I wanted it done that way then I would ahve started a new thread about it as it would be a new topic but it's not in this case!!!!!!!!!!!!!!

    More Explanation: Right, The code below is not right as it is not working but it's the only way I can explain it:

    PHP Code:
    $formvalue_{$item}id = ${$item}_id
    In that line I want both occurrences of $item to be parsed first. In this example $item is set to 'ride'.

    If $item is parsed first then the line would become:

    PHP Code:
    $formvalue_rideid $ride_id
    And then PHP can parse that fully to create a variable called $formvalue_rideid with a number in it.

    Understand? - Basically I need to know how to make PHP parse $item first. I thought it was done by putting curly brackets around what needs to be done first but that doesn't seem to be working in this case.

    Please help

    Thanks

    Regards,
    Neil

  16. #16
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I would set it to another variable first

    PHP Code:
    $varname "formvalue_" $item "_id";
    $varname2 $item "_id";

    $
    $varname = $$varname2 

  17. #17
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, I could do it like that but in this case I wish to do it like I stated above and want to know how I do it please.

    Thanks

    Regards,
    Neil

  18. #18
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well, I'm not sure you can. Which is why I offered you an alternative solution.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •