Where does the $row variable come from?

Discussion in 'PHP' started by Imozeb, Feb 25, 2010.

  1. #1
    Can someone please explain the parts in this script that I don't understand? Please don't refer me to the PHP manual cause I looked through it and I don't understand where some of the variables come from. I will comment the parts I don't understand in blue.

    <?php
    mysql_connect("localhost", "phpuser", "alm65z");
    mysql_select_db("phpdb");
    //I know I use it in all other PHP/MySQL commands but what is the value of $result?
    $result = mysql_query("SELECT * FROM usertable");
    //I think this is saying if $result isset and there are rows in the database then execute
    if ($result && mysql_num_rows($result)) {
    $numrows = mysql_num_rows($result);
    $rowcount = 1;
    //Okay. This is where I don't understand. It's saying that loop as long as $row = mysql_fetch_assoc($result). I don't know where they got $row from or what the value of mysql_fetch_assoc($result) equals.
    while ($row = mysql_fetch_assoc($result)) {
    print "Row $rowcount<br />";
    //And another loop... I don't know where the variables ($var, $val, and $row) come from. I also don't know how they are doing what they are doing here, but I'm pretty sure I can figure it out if I knew where the variables came from. I figure they are printing $var and $val to the screen.
    while(list($var, $val) = each($row)) {
    print "<B>$var</B>: $val<br />";
    }

    print "<br />";
    ++$rowcount;
    }
    }
    ?>

    Thanks.

    ~imozeb
     
    Imozeb, Feb 25, 2010 IP
  2. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #2
    I'll give it a go...

    
    
    <?php
    mysql_connect("localhost", "phpuser", "pass");
    //These are your login details, I strongly recommend removing your password when posting online...
    
    mysql_select_db("phpdb");
    
    $result = mysql_query("SELECT * FROM usertable");
    //Select all the entries within usertable, result is all of this information
    
    if ($result && mysql_num_rows($result))
    // If result is set and there are rows in the database
    {
    	$numrows = mysql_num_rows($result);
    	//Counts the number of rows in the database
    	
    	$rowcount = 1;
    	//Sets rowcount to equal 1
    	
    	while ($row = mysql_fetch_assoc($result))
    	// Loops through the row from the table that are in result, as set above
    	{
    		print "Row $rowcount<br />";
    		//Print row count
    		
    		while(list($var, $val) = each($row))
    		// Sets the first column from the table as $var and the second column as val, loops through each row of the database showing these values onscreen from print
    		{
    			print "<B>$var</B>: $val<br />";
    		}
    	
    		print "<br />";
    		
    		++$rowcount;
    		//increases the counter by one to tell the program to get the information from the next row in the datbaase
    		
    	}
    }
    ?> 
    
    
    PHP:
     
    Silver89, Feb 25, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    So $rowcount is just a variable to tell the user what row the data came from, it doesn't serve any other function, so I could delete it with no problem?

    And...

    while($row = mysql_fetch_assoc($result))

    ...since it is only using one equals sign is it setting $row to equal mysql_fetch_assoc($result) and... since it is using while it loops until there are no mysql_fetch_assoc($result) (which are rows) left? Am I right?

    By the way thanks Silver89, you explained it really well.

    ~imzoeb
     
    Last edited: Feb 25, 2010
    Imozeb, Feb 25, 2010 IP
  4. Pavol

    Pavol Peon

    Messages:
    29
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You cannot delete $rowcount = 1. If you do, you will get an error later, when the program tries to print the value of $rowcount (print "Row $rowcount<br />";)
    $rowcount = 1; sets the variable for future use.
     
    Pavol, Feb 25, 2010 IP
  5. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #5
    - yes you can remove the rowcount without causing problems.

    - yes you are correct, it will fetch each row, put it into the $row variable, execute the code in the loop then get the next row. When there is no rows left to be fetched, $row will equal null which will end the loop.
     
    killaklown, Feb 25, 2010 IP