PHP GET Help

by 7 replies
8
Hey everyone, i've been teaching myself PHP over the past couple of days, for one of my websites i need to create a script on a large scale, but currently i am trying to create the follwing -

where when I select on a page, it gets the information from my db and prints the required info....

script
............

Connect to db info here....

<a href="index.php?Link1">Link 1</a>
<a href="index.php?Link2">Link 2</a>
<a href="index.php?Link3">Link 4</a>
<a href="index.php?Link4">Link 4</a>

<?php
if ($_GET['Link1'] !=NULL) {
$liststring = "SELECT * FROM buysell WHERE subcategoryid='1'";
}
else if ($_GET['Link2'] !=NULL) {
$liststring = "SELECT * FROM buysell WHERE subcategoryid='2'";
}
else if ($_GET['Link3'] !=NULL) {
$liststring = "SELECT * FROM buysell WHERE subcategoryid='3'";
}
else {
$liststring = "SELECT * FROM buysell'";
$listInfo = mysql_query($liststring);
}
?>


<?php
while($row = mysql_fetch_array($listInfo)) {
print "<li>".$row['name']." ".$row['description']."</li>";
}
?>


....

Above is the script and it prints out all of the information, however when it comes to the if statement when wanting the content to change when I select on a link (Link1 , Link2) the information does not appear and i am left with an error.

Any help appreciated...

Stuart
#programming #php
  • whenever Link1, Link2, etc are called, there is no query executed because you put it inside an if statement. move it and your issue is solved.


    else {
    $liststring = "SELECT * FROM buysell'";
    }

    $listInfo = mysql_query($liststring);
    • [ 1 ] Thanks
    • [1] reply
    • Thanks for your help. Now the issue appears and returns

      Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/*******/index.php on line 39

      where line 39 has the following code -

      39. while($row = mysql_fetch_array($listInfo)) {




      Cheers
      • [1] reply
  • This line is probably the culprit (there's an extra ' ):
    $liststring = "SELECT * FROM buysell'";
  • I know you're learning but programming isn't just the language. Efficient logic is a big factor. When returning a query string of the same nature it is best if you can limit the variables. I would go more this route, using one variable instead of 4:

    <a href="index.php?Link=1">Link 1</a>
    <a href="index.php?Link=2">Link 2</a>
    <a href="index.php?Link=3">Link 3</a>
    <a href="index.php?Link=4">Link 4</a>


    <?php
    #Set your variable
    $mygetvar = 0;

    #Run your test

    if ($_GET['Link']){
    $mygetvar = $_GET['Link'];
    }

    #Same as else if but more efficient and easier to work with

    switch ($mygetvar) {

    case 1:
    $liststring = "SELECT * FROM buysell WHERE subcategoryid='1'";
    break;

    case 2:
    $liststring = "SELECT * FROM buysell WHERE subcategoryid='2'";
    break;

    case 3:
    $liststring = "SELECT * FROM buysell WHERE subcategoryid='3'";
    break;

    default:
    $liststring = "SELECT * FROM buysell'";
    }

    $listInfo = mysql_query($liststring);

    while($row = mysql_fetch_array($listInfo)) {
    print "<li>".$row['name']." ".$row['description']."</li>";
    }
    ?>

    http://php.net/manual/en/control-structures.switch.php
    • [1] reply
    • Try this:

      <a href="index.php?Link=1">Link 1</a>
      <a href="index.php?Link=2">Link 2</a>
      <a href="index.php?Link=3">Link 4</a>
      <a href="index.php?Link=4">Link 4</a>

      <?php

      $con = mysql_connect('dbhost', 'dbuser', 'dbpassword') or die(mysql_error());

      $query = "select * from buysell";

      if(isset($_GET["Link"])
      $query = sprintf("SELECT * FROM buysell WHERE category=%d", mysql_real_escape_string($_GET["Link"]));

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

      print("<ul>");
      while ($row = mysql_fetch_assoc($result)) {
      print("<li>".$row['name']." ".$row['description']."</li>");
      }
      print("</ul>");

      mysql_free_result($result);

      I believe you have forgotten to connect to the database..

Next Topics on Trending Feed

  • 8

    Hey everyone, i've been teaching myself PHP over the past couple of days, for one of my websites i need to create a script on a large scale, but currently i am trying to create the follwing - where when I select on a page, it gets the information from my db and prints the required info....