Resource id # 14

hi all,
i m working my way on a cms and i am getting a error
here is my query

    <?php
        $position_set = mysql_query("SELECT * FROM subjects");
        if(!$position_set){
            die ("Cannot process because" . mysql_error());
        }else{
            echo $position_set;
        }
        $subject_row = mysql_num_rows($subject_set);
        for ($count = 1;$count<= $subject_row;$count++){
        "<option value=\\"{$count}\\">{$count}</option>";
        }
        ?>

when i try to echo it out, it gives me Resource id # 14. why does it give this?

$position_set is a mysql query result, it’s what is called a resource in PHP lingo.
What you need to do is to fetch the values from the query result;


$position_set_query = mysql_query("SELECT * FROM subjects");
$position_set = mysql_fetch_assoc($position_set_query);

Now $position_set will contain an array with the values from the first recordset row your query returned.

See your question in post #7 in this thread: http://www.sitepoint.com/forums/showthread.php?797303-fetch-array-error

It’s the same question. And it has been answered.

is my loop right? coz its crashing the browser again n again?

Is the result set called $position_set or $subject_set?

$subject set

Are you sure? Look at the code you posted. How is the result set called in the first line?

here is the code, yes it was different above


    <?php
        $position_set = mysql_query("SELECT * FROM subjects");
        $subject_set= mysql_fetch_assoc($position_set);
            for($count = 1; $count<=$subject_set++; $count++){
                echo "<option value=\\"\\">{$count}</option>";
            }
        }
        ?>

Just take a good look at that code. Explain to yourself what each single line is doing.
I’m sure you’ll see the error.

ok, well i made it.but i am left with infinite loop where as the variable that is being passed into for loop has only 3 entries. i checked the mysql_query and its working fine (meaning printing the right data) but why is my loop going infinite?

    <?php
        $position_set = mysql_query("SELECT * FROM subjects");
        $subject_row = mysql_fetch_assoc($position_set);
        //print_r($subject_row);exit;
        {
            for($count = 1; $count<=$subject_row; $count++){
                echo "<option value=\\"\\">{$count}</option>";
            }
        }
        ?>

does mysql_fetch_assoc return an integer that can be compared too?
If it doesnt then it will have nothing to compare $count to so it will never stop…

I guess no. so i used mysql_num_rows. and it solved the problem!
thanks

Nice :tup: