#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Posts
    6
    Rep Power
    0
    Greetings, all.
    I'm new to php and mysql, however, I have gotten my website to dynamically generate pages based on articles/ author data in mysql. This rocks!
    However, I need to hyperlink to a php script that queries a certain column in the table, so that I get a listing of all the records where (column) Catagory=Notices.
    I can do this in the script with:

    $result = mysql_query("SELECT * FROM intranet_table WHERE Catagory='Notices'",$db);
    if ($myrow = mysql_fetch_array($result)) {
    do {

    However, what I would like to do is pass the variable thru the url like: http://www.myserver.com/query.php3?Catagory=Notices

    and leave the sql statment in the script:
    $result = mysql_query("SELECT * FROM intranet_table",$db);

    This way, I can change the select query from a hyperlink, instead of having a different php script for each catagory search that i need... All help is appreciated, as I can't find what I need to do anywhere. If you need more info, please let me know- the complete script is short, and I could post it. THANKS!!!!
    -Dale Cook


    ------------------
    <A HREF="http://www.live2ride.net" TARGET=_blank>www.live2ride.net
    </A> The OnLine Ride...
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Posts
    24
    Rep Power
    0
    Hi Dale,

    I'm looking for the same information. I'd also like to be able to add form fields like a drop down list and pass these through the href. If you find out how to do this please let me know.

    Thanks, Bob


    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Dale Cook:
    Greetings, all.
    I'm new to php and mysql, however, I have gotten my website to dynamically generate pages based on articles/ author data in mysql. This rocks!
    However, I need to hyperlink to a php script that queries a certain column in the table, so that I get a listing of all the records where (column) Catagory=Notices.
    I can do this in the script with:

    $result = mysql_query("SELECT * FROM intranet_table WHERE Catagory='Notices'",$db);
    if ($myrow = mysql_fetch_array($result)) {
    do {

    However, what I would like to do is pass the variable thru the url like: http://www.myserver.com/query.php3?Catagory=Notices

    and leave the sql statment in the script:
    $result = mysql_query("SELECT * FROM intranet_table",$db);

    This way, I can change the select query from a hyperlink, instead of having a different php script for each catagory search that i need... All help is appreciated, as I can't find what I need to do anywhere. If you need more info, please let me know- the complete script is short, and I could post it. THANKS!!!!
    -Dale Cook


    [/quote]

  4. #3
  5. No Profile Picture
    Gödelian monster
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 1999
    Location
    Central Florida, USA
    Posts
    2,307
    Rep Power
    62
    If your PHP page is called by the URL
    http://www.myserver.com/query.php3?Category=Notices

    Then the variable $Category is automatically available in you PHP script. It can be passed into the query like this:

    $result = mysql_query("SELECT * FROM intranet_table WHERE Category='$Category'",$db);
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Posts
    6
    Rep Power
    0
    For some reason, that did not work for my application, so I'll try to be a little more detailed- in hopes that someone can point out what the hell i'm doing wrong!! Following are both scripts that I am using, followed by an explaination.

    script1.php3 :
    _________________________________________

    <body>
    <?php
    $db = mysql_connect("localhost", "root");
    mysql_select_db("intranet",$db);

    $result = mysql_query("SELECT * FROM intranet_table",$db);

    if ($myrow = mysql_fetch_array($result)) {

    // display list if there are records to display

    do {
    printf("<a href="%s?id=%s">%s %s</a><br>n", 'sqlqueryscriptpartdos.php3', $myrow["id"], $myrow["Title"], $myrow["Author"]);
    } while ($myrow = mysql_fetch_array($result));
    } else {
    // no records to display
    echo "Sorry, no records were found!";
    }
    ?>
    </body>

    _________________________________________

    *****************************************

    sqlqueryscriptpartdos.php3 :
    _________________________________________

    <?php
    // part two of a two part script.
    $db = mysql_connect("localhost", "root");

    mysql_select_db("intranet",$db);

    // display individual record

    if ($id) {

    $result = mysql_query("SELECT * FROM intranet_table WHERE id=$id",$db);

    $myrow = mysql_fetch_array($result);

    printf("Title: %sn<br>", $myrow["Title"]);

    printf("Author: %sn<br>", $myrow["Author"]);

    printf("Email: %sn<br>", $myrow["Author_Email"]);

    printf("Article: %sn<br>", $myrow["Article"]);

    } else {

    // no records to display

    echo "
    <center>

    Sorry, But there are no records to return!

    </center>";

    }

    ?>

    _________________________________________

    As you can see... this is a two part script where the first script queries the mysql db for all records, and returns them to the browser as a hyperlink to script #2 (sqlqueryscriptpartdos.php3) in the following format: www.myserver.com/sqlqueryscriptpartdos.php3?id=(record id).

    This all works. What I would like to do, is in the first script, instead of having it return all records when I call it (www.myserver.com/script1.php3), I would like to pass the variable to the script thru the URL such as: www.myserver.com/script1.php3?Catagory=Notices - so that it only returns records that have the value 'notices' in the column 'Catagory'...

    When I try this now, it still returns all records, not just the records that have the value 'Notices' in the column 'catagory', which is all in the same table... (yes the entire database has only one table- only eight columns.) I know this is not the best way to do this, but I would like to get some basics down, before I attempt table joins/links...

    Thanks for the time/help/advice!!!!

    - Dale Cook
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    ? should - $result = mysql_query("SELECT * FROM intranet_table WHERE id=$id",$db);

    not read - $result = mysql_query("SELECT * FROM intranet_table WHERE category=$id",$db); on the second form query?

    Regards
    Simon
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Posts
    6
    Rep Power
    0
    Simon,
    I don't think that I understand exactly what you are trying to get across to me... Please clarify.

    Thanks

    Dale Cook
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Posts
    16
    Rep Power
    0
    OK this is what I can Make of it.

    THIS is assuming the the COLUMN 'ID' in your
    mysql database is unique or it is what you want to use to index you querys.


    Script1.php3


    $db = mysql_connect("localhost", "root");
    mysql_select_db("intranet",$db);

    $result = mysql_query("SELECT * FROM intranet_table",$db);

    while($rowinfo=mysql_fetch_array($result)){
    echo "<a href=script2.php3?ID=".$rowinfo['ID'].">".$rowinfo['title']." "$rowinfo['author']."</a>";
    }

    HERE is the second script which is passed the ID


    Script2.php3


    $db = mysql_connect("localhost", "root");
    mysql_select_db("intranet",$db);

    $query="Select * from intranet_table where ID='$ID'";

    $result = mysql_query($query,$db);

    if($myrow = mysql_fetch_array($result)){

    printf("Title: %sn<br>", $myrow["Title"]);

    printf("Author: %sn<br>", $myrow["Author"]);

    printf("Email: %sn<br>", $myrow["Author_Email"]);

    printf("Article: %sn<br>", $myrow["Article"]);

    } else {

    // no records to display

    echo "
    <center>

    Sorry, But there are no records to return!

    </center>";

    }


    ?>

    For what I can Make out of what you are saying something like that should work.

  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 1999
    Location
    Seattle
    Posts
    133
    Rep Power
    16
    This way, I can change the select query from a hyperlink, instead of having a different php script for each catagory search that i need

    It's a little inefficient to do it the way you are asking, you would have to get all of the records out of the database, then use the PHP script to filter the category out, like this:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $result = mysql_query("SELECT * FROM intranet_table",$db);

    while($rowinfo=mysql_fetch_array($result)){
    if ($rowinfo["category'] == 'Notices') {
    echo "<a href=script2.php3?ID=".$rowinfo['ID'].">".$rowinfo['title']." "$rowinfo['author']."</a>";
    }
    }[/code]

    It's more efficient to let MySQL filter out the information you don't need. Of course it's a bit more of a hassle to have to modify your query statement, but you can build a query statement based on whether a variable exists from the incoming URL like this:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $query = "SELECT * FROM table" .
    $Category != "" ? " WHERE Category = '$Category'" : "";

    [/code]

    This concatenates the where clause onto the query statement if the $Category variable was passed in the URL string.

    Don

    [This message has been edited by donarb (edited April 02, 2000).]
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Posts
    6
    Rep Power
    0
    donarb,

    I have attempted the following code based on your suggestion, and I get a
    '0 is not a MySQL result index... on line 10' error when I run it...

    I am attempting to access the following script with (www.myserver.com/script1.php3?Catagory=Notices)

    <html>
    <body>
    <?php
    $db = mysql_connect("localhost", "root");
    mysql_select_db("intranet",$db);

    $query = "SELECT * FROM table" ;
    $Catagory != "" ? " WHERE Catagory = '$Catagory'" : "";

    if ($myrow = mysql_fetch_array($query)) {

    // display list if there are records to display

    do {
    printf("<a href="%s?id=%s">%s %s</a><br>n", 'sqlqueryscriptpartdos.php3', $myrow["id"], $myrow["Title"], $myrow["Author"]);
    } while ($myrow = mysql_fetch_array($result));
    } else {
    // no records to display
    echo "Sorry, no records were found!";
    }
    ?>
    </body>
    </html>


    Your help is greatly appreciated. THANKS!
    Dale


    [This message has been edited by Dale Cook (edited April 03, 2000).]
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Posts
    1
    Rep Power
    0
    The simplest way to pass the parameters, is to take the QUERY_STRING as is and append it to your sql line.
    $sql = "SELECT * from Table Where ". $QUERY_STRING;
    $result = mysql_query($sql,$db); ...

    It should work just fine.
    Eli Argon.

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Dale Cook:
    Greetings, all.
    I'm new to php and mysql, however, I have gotten my website to dynamically generate pages based on articles/ author data in mysql. This rocks!
    However, I need to hyperlink to a php script that queries a certain column in the table, so that I get a listing of all the records where (column) Catagory=Notices.
    I can do this in the script with:

    $result = mysql_query("SELECT * FROM intranet_table WHERE Catagory='Notices'",$db);
    if ($myrow = mysql_fetch_array($result)) {
    do {

    However, what I would like to do is pass the variable thru the url like: http://www.myserver.com/query.php3?Catagory=Notices

    and leave the sql statment in the script:
    $result = mysql_query("SELECT * FROM intranet_table",$db);

    This way, I can change the select query from a hyperlink, instead of having a different php script for each catagory search that i need... All help is appreciated, as I can't find what I need to do anywhere. If you need more info, please let me know- the complete script is short, and I could post it. THANKS!!!!
    -Dale Cook


    [/quote]

  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 1999
    Location
    Seattle
    Posts
    133
    Rep Power
    16
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>
    I have attempted the following code based on your suggestion, and I get a
    '0 is not a MySQL result index... on line 10' error when I run it...
    [/quote]

    You didn't submit the query. Put the call to mysql_query() before the if ($myrow = ...

    Don

Similar Threads

  1. Passing POST variables
    By rlorenzo in forum PHP Development
    Replies: 11
    Last Post: June 6th, 2004, 03:00 PM
  2. passing variables with url
    By jackass in forum PHP Development
    Replies: 5
    Last Post: January 28th, 2004, 06:19 PM
  3. php variables in java passing through the url
    By panahi in forum Java Help
    Replies: 0
    Last Post: January 23rd, 2004, 04:50 PM
  4. Passing variables through URL
    By pilot1 in forum PHP Development
    Replies: 0
    Last Post: December 7th, 2003, 09:08 AM
  5. variables passing within the URL in perl
    By stage_watchman in forum Perl Programming
    Replies: 7
    Last Post: November 20th, 2003, 03:17 PM

IMN logo majestic logo threadwatch logo seochat tools logo