Ok, this is a real newbie question I'm sure, but the book i am using doesn't really shed to much light on this topic.

How do you know what to use and when?

I understand the concept behind them. But i just don't know how to decide before hand what works.

I have a query that will return one row from a MySQL table.

Now for some reason I have to hard code the exact things I want out of it.

I know i may not be explaining myself properly so here is the query:


$sql = "SELECT * FROM `Questions` WHERE `ID`='$id' AND `Name` = '$Name'";

$result = mysql_query('$sql) or die(mysql_err());

$i=0;
while ( $row = $mysql_fetch_row($result)) {
echo $row['$i'];
$i++;
}

Any thoughts on why this may not work. I sometimes get one value out of it and thats it. It does not seem to loop threw it. Or should i be putting it into a for loop?

Thanx for any help,
ulysses

    You've got a quoting problem in the posted example. Is that a direct quote from your script?

    Also, you've got a $ in front of the function mysql_fetch_row . That's likely to create issues, too....

      Have a look at the php manual for mysql_fetch_array, mysql_fetch_assoc and mysql_fetch_object. They do basically the same thing, but you access the elements of the recordset in different ways (enum or associated array, associative array only, or as an object respectively)

      hth, njm

        Originally posted by dalecosp
        You've got a quoting problem in the posted example. Is that a direct quote from your script?

        Also, you've got a $ in front of the function mysql_fetch_row . That's likely to create issues, too....

        Sorry I should have mentioned it is not a direct quote. Just an IDEA of what i'm doing.

        The situation is, every time i retrieve the row using mysql_fetch_row($result) it fetches the correct row. BUT my problem is this.

        I need to access each column in the row individualy at different times. so i can't do this:

        
        while ($row = mysql_fetch_row($result)) {
        $row[0], $row[1], $row[2];
        }
        
        

        First off that is hard coding it. second of all i need to sort of spit out the elements into certain fields in an html table.

        ....am i clear? I think i'm confusing myself..hrmmm...

        Ok, I'm going to try doing a loop such as this:

        
        for($i=0;$i<get_length($result);$i) {
        
        while($row= mysql_fetch_row($result)) {
        $row[$i];
        }
        }
        

        and see how that goes. I'll let you know.

        THANX for all the help though!!
        Ulysses

          6 years later

          hi buddy,

          mysql fetch array returns the array or all fields and you need to access the fields by their order in database like 0th 1st and 2nd columns in database and in case of object it resturns the resurce id and you can acces the database fields by their names.

          you can find the further details here
          www.sagefix.com/threads-t-114448.htm

          enjoy!

            Write a Reply...