Search code examples
phpmysqlfunctionurlforum

Custom Forum function not returning correct ID?


I decided to use a forum that I made about a year ago for a website I am working on now, and everything works (after my revisions) except for after creating the topic, it does not take you to the new post. Here is the functions that is supposed to accomplish that:

function new_post($name) {
    $sqlpost = "SELECT `id` FROM `forum_question` WHERE `name`='$name' ORDER BY `datetime`     ASC LIMIT 1";
    $mysqlpost = mysql_query($sqlpost);
    return $mysqlpost;
}

And here is how I apply that: <a href="view_topic.php?id=<?php echo new_post($name); ?>">View Post</a> (Keep in mind that $name is defined earlier in the document)

This is the URL it takes me to, after creating a post: http://localhost/Nu-Bio/view_topic.php?id=Resource%20id%20#10

Thanks for reading and I hope you can help. ALSO: I know that I should be using MySQLi. I will be updating once I get the next few things I want to finish done~


Solution

  • Return Values for result_query()

    For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

    <?php
    
     function new_post($name) {
      $id = 0;
      $sqlpost = "SELECT id FROM forum_question WHERE name='".$name."' ORDER BY datetime ASC LIMIT 1";
      $mysqlpost = mysql_query($sqlpost);
      if (!$mysqlpost) {
         die('Invalid query: ' . mysql_error());
      }
      else
      {
         $array = mysql_fetch_assoc($mysqlpost);
    
          $id = $array['id'];
      }
      return $id;
     }
    
    ?>
    

    Reference