Hello, I am doing a project where I have a long list of products....
I need to be able to list the title of each product and have it link to a description.....

Anyone have any tips for making a hyperlink in mysql?
I can display the data fine... I just cant find anything on making links out of the data....

Thanks,
Kenny

    <?
    
    // Connect to MySQL Server
    @mysql_connect($server, $username, $password) or DIE("Couldn't Connect to MySQL Database");
    
    // Select Database
    @mysql_select_db($database) or DIE("Couldn't Select Database");
    
    $query = mysql_query("SELECT * FROM yourtable");
    
    while ($a_row=mysql_fetch_array($query)) {
       echo "<a href=details.php?id=" . $a_row["id"] . ">" . $a_row["product"] . "</a><br>\n";
    }
    
    ?>

    Then on details.php, you select everything where id='$id'

    $query = mysql_query("SELECT * FROM yourtable WHERE id='$id'");

    Then display the specific details for each product.

    Is that what you were looking for?

    Cgraz

      Hello, thanks for taking time to post...

      What I need is say....
      a list of titles of products that are links...
      and you can click them to go to another page with details on each one...

      so we can just have one db....
      with a table for products.... with TITLE, DETAILS.......

      would the code you sent work for that?
      there are going to be about 100 product titles listed.

      Thanks again for your time
      Kenny

        As long as you're using an auto_increment field called "id" - which you really ought to be 😉.

        then the hyperlink will link to a different page, but passes the variable "id" (just taken from the db) on along with it - which is what the:

        .....file.php?id=

        part is all about. Then, in the detail page, you just select from the database the needed fields by using

        $query = "SELECT * FROM yourtable WHERE id='$id'";
        $result = @mysql_query($query);
        etc...
        

          Hello, could you help me see what I am going wrong?
          Here is the page that will just list the titles
          <html>
          <head><title>LIST TEST</title></head>
          <body>
          <?php
          $db="jokes";
          $link = mysql_connect("localhost");
          if (! $link)
          die("Couldn't connect to MySQL");
          mysql_select_db($db , $link)
          or die("Couldn't open $db: ".mysql_error());
          $result = mysql_query( "SELECT * FROM jokes" )
          or die("SELECT Error: ".mysql_error());

          while ($a_row=mysql_fetch_array($result))
          {
          echo "<a href=details.php?id=" . $a_row["id"] . ">" . $a_row["product"] . "</a><br>\n";
          }

          ?>
          That takes me to a page that is blank... and when I view the source here is what I get...

          <html>
          <head><title>LIST TEST</title></head>
          <body>
          <a href=details.php?id=></a><br>
          <a href=details.php?id=></a><br>
          <a href=details.php?id=></a><br>
          <a href=details.php?id=></a><br>
          <a href=details.php?id=></a><br>
          <a href=details.php?id=></a><br>
          I am just missing the "title" and the id number....

          Thanks,
          Kenny

            Your variables don't seem to have any values assigned to them. Are your field names in mysql called id and product? If so, are they lowercase, or are they ID for example or Product? Cases make a difference.

            You could try just echoing $a_row["id"] and $a_row["product"] to see if they even have values assigned to them. Just echo them out right after your while statement. Also, count the number of rows mysql is returning. Maybe it's just returning 0 results.

            $result = mysql_query("SELECT * FROM jokes") or die("SELECT Error: ".mysql_error()); 
            $num_rows = mysql_num_rows($result);
               echo "Number of Rows equals " . $num_rows . "";

            See what that outputs.

            You could also clean up your code a little like this

            <?php 
            
            $db="jokes"; 
            $link = mysql_connect("localhost") or die("Couldn't connect to MySQL");
            mysql_select_db($db , $link) or die("Couldn't open $db: ".mysql_error()); 
            
            $result = mysql_query("SELECT * FROM jokes") or die("SELECT Error: ".mysql_error()); 
            
               while ($a_row=mysql_fetch_array($result)) { 
                   echo "<a href=details.php?id=" . $a_row["id"] . ">" . $a_row["product"] . "</a><br>\n"; 
               } 
            
            ?> 

              well i did echo the rows like you suggested and it returned all 6 of the rows that are in that table....

              as for the product and id variable... someone else wrote that part... and I was not sure what it did...

              In the "jokes" table there are only two columns.... joketext and jokedate.....

              I was trying to get a list of "titles" or id numbers or something that would be links that would go to more details......

              like a joke title that would link to a joke
              or a product title that would link to more details....

              I renamed the "product" variable to joketext and it still did not work... any suggestions?

              Thanks for your time -Kenny

                <?php 
                
                $db="jokes"; 
                $link = mysql_connect("localhost") or die("Couldn't connect to MySQL");
                mysql_select_db($db , $link) or die("Couldn't open $db: ".mysql_error()); 
                
                $result = mysql_query("SELECT * FROM jokes") or die("SELECT Error: ".mysql_error()); 
                
                   while ($a_row=mysql_fetch_array($result)) { 
                     echo "Jokedate: " . $a_row["jokedate"] . "<br>";
                     echo "Joketext: " . $a_row["joketext"] . "<br><br>";
                   }
                
                ?> 

                What does that output?

                  Write a Reply...