Jump to content

Query not working.. but it is fine :S


rockinaway

Recommended Posts

$forum = mysql_query('SELECT parent, last_post FROM test_boards WHERE id = '.$_GETS['file'].'');

 

That is my query but it doesn't seem to work. I have several other queries at the same time, they all work.... this one doesn't. This is in a WHILE statement with the others, but the variables were not returning any value.. What could be wrong?

Link to comment
Share on other sites

then see if this will work

 

$file = 12; //or any valid id in your database, if it brings it back then $_GET['file'] doesnt carry what you want it to
$forum = mysql_query("SELECT parent, last_post FROM test_boards WHERE id = '$file'")or die("error in query" . mysql_error());

Link to comment
Share on other sites

Tne structure is fine as I have other queries from the same table and they work..

 

i didn't ask because i was concerned about your structure. i asked because it would help to know what your database looked like so i can help you write this code...

 

an example like:

+---------------------------------------------+
|                   table_name                |
+----------+-----------+------------+---------+
|  column1 |  column2  |   column3  | column4 |
+----------+-----------+------------+---------+
|  data    |    data   |     data   |  data   |
+----------+-----------+------------+---------+
|  data    |    data   |     data   |  data   |
+----------+-----------+------------+---------+
|  data    |    data   |     data   |  data   |
+----------+-----------+------------+---------+

 

would do fine.

Link to comment
Share on other sites

+---------------------------------------------+

|                  test_boards              |

+----------+-----------+------------+---------+

|  id |  name  |  info | parent | last_post |

+----------+-----------+------------+---------+

|  1  |    Test1  |    text  |  0  | 34

+----------+-----------+------------+---------+

|  2  |    Test2  |    text  |  1  | 27

+----------+-----------+------------+---------+

|  2  |    Test3 |    text  |  1  | 15

+----------+-----------+------------+---------+

Link to comment
Share on other sites

i'm assuming 'id' isn't set to AUTO INCRIMENT (it needs to be), name is the name of the thread, info is the text of the thread, parent is probably the post that belongs to which thread, and last post i have no idea what that is... can you elaborate?

Link to comment
Share on other sites

Sorry ...

 

id - is auto_incremented :D

name - name of board

info - desc

parent - parent board

last_post - last submission of file, this has the file ID

 

$file = $_GET['file'];
$forum = mysql_query("SELECT parent, last_post FROM test_boards WHERE id = '$file'")or die("error in query" . mysql_error());

 

with the above in mind then which id are you calling for in the above query, the table id column which is auto-incremented or the file ID which is in the last_post column??

Link to comment
Share on other sites

so as you answer in my last post about putting a valid auto-incremented id value in the query as opposed to using the $_GET['file'] variable, must pull something from the database, if you have a table entry with the ID value of 3 and use the following query

 

$file = "3";
$forum = mysql_query("SELECT parent, last_post FROM test_boards WHERE id = '$file'")or die("error in query" . mysql_error());

 

all things being equal and the spellings and values are correct, it WILL pull one row from the database, if it shows you nothing then it must be the way that you are interacting with the Result Resource $forum that is wrong somewhere

 

Link to comment
Share on other sites

Yes, and when it is deleted I revert back to an older post..

 

first of all that is NOT the way your database should be designed. there's no point in having a 'last_post' column and constantly adding an extra UPDATE query whenever a new post is added or deleted... that's not efficient. all you need is an auto-incrimenting 'id' column. take a look at this example table:

+-----------------------------------------+
|              test_boards                |
+------+--------+---------------+---------+
|  id  | parent |     title     |  data   |
+------+--------+---------------+---------+
|  1   |        |  title 1      |  text   |
+------+--------+---------------+---------+
|  2   |        |  title 2      |  text   |
+------+--------+---------------+---------+
|  3   |   1    |  post title1  |  text   |
+------+--------+---------------+---------+
|  4   |        |  title 3      |  text   |
+------+--------+---------------+---------+
|  5   |        |  title 4      |  text   |
+------+--------+---------------+---------+
|  6   |    2   |  post title 2 |  text   |
+------+--------+---------------+---------+
|  7   |    2   |  post title 3 |  text   |
+------+--------+---------------+---------+
|  8   |    1   |  post title 4 |  text   |
+------+--------+---------------+---------+

 

that's what your table should look like... and the way you can populate results is simple... just use a nested while loop.

 

<?php
        /*connect to your database here*/


        /*pull all threads*/
        $sql = "
                SELECT
                        *
                FROM
                        test_boards
                WHERE
                        parent = NULL
        ";
        $query = mysql_query($sql) OR die(mysql_error());

        while($thread = mysql_fetch_array($query)){
                echo $thread['title'] ."<br />\n";

                /*pull latest post from thread*/
                $sql = "
                        SELECT
                                MAX(id),
                                parent,
                                title,
                                data
                        FROM
                                test_boards
                        WHERE
                                parent = '". $thread['id'] ."'
                ";
                $query = mysql_query($sql) OR die(mysql_error());

                while($post = mysql_fetch_array($query)){
                        echo "<font size=\"-1\"><b>Last Post:</b> {$post['title']} | ". substr(0, 15, $post['data']) ."...</font><br />\n";
                }
        }
?>

 

that should do it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.