Jump to content

for loop and mysql help


SN1P3R_85

Recommended Posts

Ok, so i have a website i've been working on for a couple weeks now, and i decided to add a forum. Im not sure if my forum code is conventional, i just made it all up, so im guessing there is a better way to do it. I still want to try to get this one to work however. My code doesn't have any errors, but it doesn't do what it should. here is my code:

 

<?php

session_start();

include( '../SQL_PASS.inc' );        //includes my sql info

include( '../user.inc' );                //includes user validation, with some user info

$_SESSION['return_url'] = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];        //just remembers this page

 

if (!$con)

{

        die('Cannot connect to MySQL server: ' . mysql_error());

}

if (!$db_select)              //$db_select just selects my database

{

        die('Cannon select database: ' . mysql_error());

}

 

$sql_fetch = "SELECT * FROM test_forum";        //selects everything inside the table `test_forum`

$sql_query = mysql_query($sql_fetch);            //runs the query to select it all

 

if (!$sql_query)

{

        die('Could not run query: ' . mysql_error());

}

 

if (mysql_num_rows($sql_query)==0)                //if the table is empty, kills the program

{

        die('there are no entries for this topic');

}

 

if ($row = mysql_fetch_assoc($sql_query))                //this is where the problem is i think.

{

        $max_id = max($row['Id']);                //the highest value in the array $row

        $min_id = min($row['Id']);                //the lowest value in $row

 

        for ($i=$max_id; $i>=$min_id; $i--)        //I want this to print the message in the 'User_msg' column for the highest Id, and then

                                                              //-go to the Id below it and print the message in that row, and so on.

        {

                echo "<p class=\"reg_warn\">" . $row['User_msg'] . ": " . "</p>";

        }

}

 

?>

 

 

What i basically want to do is make an array of the Id column, and then print the information out of that column, and then the next loop, change to a row right below it, (ex. print message in column 17, then 16, then 15, etc).

 

all suggestions are appreciated, except please don't just rewrite my code, and give it back without an explination for why and how it works, thanks

Link to comment
Share on other sites

Yeah, your doing it wrong.

 

mysql_fetch_assoc returns an array as a row. So, if you want to get all the rows, you have to call it a bunch of times.

 

So make this:

 

if ($row = mysql_fetch_assoc($sql_query))

 

this:

 

while($row = mysql_fetch_assoc($sql_query)){

// Some crap...

print_r($row);

}

 

And see what it does.

 

 

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.