Page 1 of 1

repeating the row

Posted: Thu Jul 09, 2009 10:48 pm
by melomonk
Hello

I'm stuck - blinders welded on I guess - but i can't see what I'm doing wrong :banghead:

I'm trying to achieve one row of returned data per line while concatenating a URL to the first field
I've pasted the code and the error received

Can someone else see what I'm doing wrong? I thought a new pair of eyes would help....

Code: Select all

 
$rs = mysql_query("select blah from blah") or die ("invalid query");
while ($row = mysql_fetch_assoc($rs)) {
    for($i = 0; $i < $row; $i++){
        echo ("field1: %s  field2: %s  field3: %s  field4: %s", "http://URL.php?id=" . $row["field1"], $row["field2"], $row["field3"], $row["field4"] . "\n");
    }
mysql_free_result($rs);
 
Error produced:
Parse error: syntax error, unexpected ',' in newfile3.php

Re: repeating the row

Posted: Thu Jul 09, 2009 10:56 pm
by califdon
Did you intend to use sprintf()? You omitted the function name, I think.

Re: repeating the row

Posted: Thu Jul 09, 2009 11:02 pm
by melomonk
when i use printf

i get
Parse error: syntax error, unexpected $end in newfile3.php


thanks for looking

Re: repeating the row

Posted: Thu Jul 09, 2009 11:53 pm
by califdon
What I'm trying to tell you is that I can't make any sense of your syntax. And printf() is not the same as sprintf(). Look up the syntax, although I don't even see why you would be trying to use those functions. One thing is sure, you can't just make up your own syntax as you go along. You have to follow the syntax in the manual.

Re: repeating the row

Posted: Fri Jul 10, 2009 12:06 am
by melomonk
thanks
i appreciate your time
it's been helpful

Re: repeating the row

Posted: Fri Jul 10, 2009 12:47 am
by melomonk
echo
printf
sprintf

i just need to row to repeat
until all of the data is returned

Re: repeating the row

Posted: Fri Jul 10, 2009 12:42 pm
by califdon
echo is the commonly used command to simply send text to the browser. print can also be used, there's very little difference between the two. printf() and sprintf() are functions used to format data and I don't see any reason for you to use them in that context.

The usual practice when using mysql_fetch_assoc() is to use extract() to set variables with the names of each field, making it easy to use them in your script without the clumsy syntax of $row[..].

Code: Select all

while ($row = mysql_fetch_assoc($rs)) {
   extract($row);
   echo "http://URL.php?id=$field1, $field2, $field3, $field4\n";
}
I'm not sure that's exactly what you want, since I can't make heads or tails of your original code, but it looks to me like that's what you were trying to do.