1

Im following a video tutorial on mysql and php, and a certain line of code has me a bit confused:

    <?php

            $result = mysql_query("SELECT * FROM subjects", $connection);
                if(!$result){
                die("Database query failed: " .mysql_error());
                    }


                while($row = mysql_fetch_array($result)){
                echo $row["Menu_Name"]." ".$row["position"]."<br/>";
                    }
                    ?>

I want to really understand what this code is doing, so let me see if i got it straight. Basically, what it does on my screen is return the various items store in my table subjects and displays them in their position. It does this by returning them in two arrays, one is the [menu-name] which stores the text for each item and the other is [position] which stores the order in which they come out. So, my while loop goes through this array and outputs. But that's what I dont get. What does $row do and how does it manage to go though and loop through. I may be way off here and was hoping someone could shed some light on this.

3
  • 1st learn basic of PHP & MySQL. Commented Mar 22, 2013 at 5:56
  • $row is the array in which the values (of a single row in the database) is stored. You access the values in this row using a string indexer (the names of the columns) to get tot the values in the array $row. Commented Mar 22, 2013 at 5:58
  • Since you are new here, accept the answer(tick) which solved your problem. Up-vote(up arrow) the answer(s) which give(s) you information or help(s) you. Down-vote(down arrow) the answer(s) which are fake. Commented Mar 22, 2013 at 6:22

4 Answers 4

2

mysql_query sends a MySQL query. mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified. mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.

mysql_fetch_array fetches a result row as an associative array, a numeric array, or both. Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

One last thing if you want to know what's happening with a function try referring the documentation. It helps most of the time.

According to the question you have asked I guess you should go through the basic MYSQL and PHP lessons.

Sign up to request clarification or add additional context in comments.

Comments

0

Those are not two arrays. That is one array which is Multi dimensional

while($row = mysql_fetch_array($result)){

When you run that line of code then it grabs one complete row from the list of your results and assigns it to an array named $row. You can then give your Index names to get data from it just like you wrote

$row["Menu_Name"]

So Menu_Name is an index of an array $row containing some value which came from your database. Since this statement is present in a loop, it will loop through all the rows of your returned result and do the same for each of them

Comments

0

this line is retrieving table records, line by line. This is actually an array representing a table record.

    $row = mysql_fetch_array($result)

Now, this array in indexed by the table field names. Because your query is 'select *', it means that its retrieving all fields.

So,

$row["Menu_Name"] 

has the value of the field 'Menu_Name' of the table 'subjects', for the current row.

Comments

0

SEE tutorial example http://www.devmanuals.com/tutorials/php/phpmysql/php-mysql-fetch-array-example.html.It may clear your doubt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.