7 replies
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
#php
  • Profile picture of the author Ambius
    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.

    Originally Posted by Smcminigal View Post

    else {
    $ liststring = "SELECT * FROM buysell'";
    $ listInfo = mysql_query();
    }

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

    $listInfo = mysql_query($liststring);
    {{ DiscussionBoard.errors[3224892].message }}
    • Profile picture of the author Smcminigal
      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
      {{ DiscussionBoard.errors[3225308].message }}
      • Profile picture of the author Justin Hedge
        You need to get the result of the query via the mysql_result function.

        IE, this will work:
        $result = mysql_result($listInfo);
        while($row = mysql_fetch_array($result)) {
        ...
        }
        {{ DiscussionBoard.errors[3225368].message }}
        • Profile picture of the author Ambius
          Originally Posted by Justin Hedge View Post

          You need to get the result of the query via the mysql_result function.

          IE, this will work:
          = mysql_result();
          while( = mysql_fetch_array()) {
          ...
          }

          but mysql_result only returns 1 row, right? so that would not loop through the entire listInfo of page names.

          I'm not sure why you are only getting that error sometimes. is it totally random or does it happen every time under certain conditions? the problem might be somewhere before line 39.

          also, you probably don't want an else { select * } because the result could be very large and slow the server a bit. I would only select the subcategoryid, name and description columns on else{}
          {{ DiscussionBoard.errors[3226386].message }}
  • Profile picture of the author Justin Hedge
    This line is probably the culprit (there's an extra ' ):
    $liststring = "SELECT * FROM buysell'";
    {{ DiscussionBoard.errors[3226491].message }}
  • Profile picture of the author Ken Durham
    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
    Signature

    yes, I am....

    {{ DiscussionBoard.errors[3226692].message }}
    • Profile picture of the author mlcmartin
      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..
      {{ DiscussionBoard.errors[3231581].message }}

Trending Topics