SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    uk colchester
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    search query help

    I have a little script that pulls data from just 1 table, I just want to be able to click a link to arrange the data accordingly, all I am try ing to do is click a link and pass a name to the variables in the sol line link this

    Link:
    echo '<p><a href="' . $_SERVER['PHP_SELF'] . '?search=firstname">First Name</a> </p>';

    Sql line:
    $result = @mysql_query('SELECT * FROM members2006 ORDER BY "$search" ASC');

    To check the variable name is being passed I put this above the sql line:
    echo 'search holds ' . $search;

    When I click the link the variable is then output as firstname as required,
    If i enter firstname into the sql line in place of the variable then the data is displayed as per firstname column in the table.
    This is the first time I have tried this with php and cant see where I am going wrong. Can anyone see my error.

    Regards

    Ian

  2. #2
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    please print the err.
    echo the query

    $result = @mysql_query('SELECT * FROM members2006 ORDER BY '.$_GET['search'].' ASC');

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    uk colchester
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply,

    Ok in my original code I had no error but the results were just output in order of the unique id column and by clicking my version of the link nothing changed,
    I still dont understand why my way could not see the info held in the variable however with your input when i click the link it does now output the data in the order wanted which is great.
    Now my only problem is when the page 1st loads there is no info in the variable and I get this error:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\program files\apache group\Apache\htdocs\sudbury\password\tmpmft81bevcp.php on line 490

    Is there a way around this.

    Thanks for your input

    Regards
    Last edited by meanweaver; Jan 5, 2007 at 14:14.

  4. #4
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when there is no value u need to provide atleast some value to query to execute

    if($_GET['search'] =="")
    {
    $result = @mysql_query('SELECT * FROM members2006 ORDER BY firstname ASC');
    }
    else
    {
    $result = @mysql_query('SELECT * FROM members2006 ORDER BY '.$_GET['search'].' ASC');
    }

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    uk colchester
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i have managed to sort out my problem with an if statement like this
    if ($search == ''){
    $result = @mysql_query('SELECT * FROM members2006 ORDER BY id ASC' );
    }else{
    $result = @mysql_query('SELECT * FROM members2006 ORDER BY '.$_GET['search'] .' ASC' );
    }

    However can someone explain why I have to use the $_GET[] function in the sql query instead of being able to just put the variable name in there instead.

    Thanks for the help

    Ian

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    uk colchester
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh no we were both posting at the same time, however I came up with almost the same code which is good news for me. I notice your formatting is easier to read.

    Thanks again

  7. #7
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    u can use $_REQUEST

  8. #8
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The $_GET variable is used to collect values from a form with method="get". Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and it has limits on the amount of information to send

  9. #9
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  10. #10
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    uk colchester
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks on the $_get, I understand using this when information is sent with a form but I looked though my book and cant see why i need it when the variable is sent via the url string like this ?search=firstname

    Sorry if I am a bit slow on this just cant make sence of that one bit

    Thanks again for your help

    Regards

  11. #11
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    coz there are few methods that a form can use to pass the variables from one page to another (or to itself). They care called GET and POST.


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
  •