1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Rep Power
    I have two questions:
    #1. I was reading a tutorial at www.webmonkey.com (yes, I know I'm a traitor ) on PHP/MySQL, and it gave an example of how to display all the records in a table. Here's part of the code:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $db = mysql_connect("localhost", "root");
    $result = mysql_query("SELECT * FROM employees",$db);
    if ($myrow = mysql_fetch_array($result)) {
    do {
    printf("<a href="%s?id=%s">%s %s</a><br>n", $PHP_SELF, $myrow["id"], $myrow["first"], $myrow["last"]);
    } while ($myrow = mysql_fetch_array($result));

    Now, I always knew that in an if/while statement you always used ==, !=, etc... to test, and not = by itself. = is supposed to define/change the variable.. (I even checked php.net) What I'm wondering is, why does this work? Does it check for whatever is after = (for the if/while statement) and also store the results to $myrow on the side? It doesn't make sense..

    #2. I checked the manual at php.net to try to understand when to use print, printf, or echo, and couldn't figure it out. From what I understood, there's no reason for print, since echo can output 1+ strings... Now, printf apparently can get variables from somewhere and then you use them to output data. How does it work? In summary, what's the difference between echo & print (is there a reason print exists?), and how do you use printf?

    Thanks a lot!


    [This message has been edited by terium (edited June 25, 2000).]
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Perth West Australia
    Rep Power

    while ($myrow = mysql_fetch_array($result));

    if it was == then the argument would never be true. The array $myrow will never equal mysql_fetch_array($result) it just tests for its existance - but I get where you are coming from - I do not pretend to realy understand it!

    as for print/echo - I always use echo , but for no reason than I feel comfortable with it? as for printf - read the manual - still pretty confused.

    Simon Wheeler
    FirePages -DHTML/PHP/MySQL
  4. #3
  5. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Caro, Michigan
    Rep Power
    if ($myrow = mysql_fetch_array($result)) works like this...

    it executes the mysql_fetch_array() command, and assigns the results to $myrow. If any rows were returned in $result, then $myrow will be assigned those array values, and therefore not be equal to zero, and therefore the if... statement evaluates to TRUE. if nothing is returned in $result (no rows) then mysql_fetch_array() returns false, and therefore $myrow = false, and therefore the if... statement evaluates to false...

    it's a very good method to use if you are not sure that any rows will be returned out of the database.

    echo and print are the same things. you don't have to use parenthesis with echo, not sure about print.

    printf is good to use for printing out array values and for formatting the printed data easily...but it's just a matter of personal preference whether you use echo,print,or printf...

    printf("My name is: %s",$myrow["name"]);
    echo "My name is: " . $myrow["name"]; //(php3)
    echo "My name is: {$myrow["name"]}"; //(php4 only)

    printf is good for outputting money values also.

    $money1 = 68.75;
    $money2 = 54.35;
    $money = $money1 + $money2;
    // echo $money will output "123.1";
    $formatted = sprintf ("%01.2f", $money);
    // echo $formatted will output "123.10"

    that last bit was copied from the php manual. check out the manual for sprintf, it explains all the codes to use in printf

    hope that helps...

    ---John Holmes

    [This message has been edited by SepodatiCreations (edited June 26, 2000).]
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Rep Power
    Thanks a lot! This was exactly what I needed. There was a tutorial which used BOTH print AND echo and I couldn't logically figure out when to use which.


IMN logo majestic logo threadwatch logo seochat tools logo