Jump to content

Understanding the code


Pain

Recommended Posts

Hey guys. I have this code which connect me to the database and displays info in the web. However i do not understand a few lines of it.

 

<?php
while ($query = mysql_fecth_assoc($result)) {

$field01 = $result['Name'];
$field02 = $result['Username'];
?>

 

Can someone try to explain in they're own, simple words? Thank you.

 

 

Full code.

<?php

$dbhost = "";
$dbuser = "";
$dbpass = "";
$db = "";

$connection = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($db, $connection);

$query = 'SELECT * FROM users';
$result = mysql_query($query);

while ($query = mysql_fecth_assoc($result)) {

$field01 = $result['Name'];
$field02 = $result['Username'];

echo $field01;
echo $field02;

}


?>

Link to comment
Share on other sites

<?php
// defines what you want to select from the database table called users, in this case * means select everything.
$query = 'SELECT * FROM users';
// requests the above task from your database server
$result = mysql_query($query);
// while each record is being pulled out, perform whatever is in between the curly braces {...}
while ($query = mysql_fecth_assoc($result)) {
// grabs whatever value is in field 'Name' (of your database table) and stores it in a variable called '$field01'
$field01 = $result['Name'];
// same as above, but for field 'Username'
$field02 = $result['Username'];
...
}
?>

 

this code won't really work though, you have several errors.

Link to comment
Share on other sites

<?php
...

// while each record is being pulled out, perform whatever is in between the curly braces {...}
while ($query = mysql_fecth_assoc($result)) {

...
?>

 

How is it pulled out? How does this mysql_fetch_assoc work?

Link to comment
Share on other sites

it reads ONE line at a time from your table (as long as they match the initial criteria) and returns them in an array...

 

you have the variable names all messed up... $query is your query, then you're also trying to store the results in the same variable, and then trying to grab them from $result.... oh dear.

Link to comment
Share on other sites

try changing

 

while ($query = mysql_fecth_assoc($result)) {
// grabs whatever value is in field 'Name' (of your database table) and stores it in a variable called '$field01'
$field01 = $result['Name'];
// same as above, but for field 'Username'
$field02 = $result['Username'];
...
}

 

to

 

while ($res = mysql_fecth_assoc($result)) {
// grabs whatever value is in field 'Name' (of your database table) and stores it in a variable called '$field01'
$field01 = $res['Name'];
// same as above, but for field 'Username'
$field02 = $res['Username'];
...
}

 

so that the results will be returned in an array called $res (i.e. that isn't being used for anything else)

Link to comment
Share on other sites

One clarification to what WebStyles stated. The mysql_fecth_assoc() reads one record from the result set of the query that was performed - it is not reading the data from the database table. There's an important distinction there.

 

Although you can think of the result set as a temporary table created from the query. Then, as he stated, that function will get the 'next' record from the result set and returns the values of that record an an array. So, when you use somethign like

$row = mysql_fetch_assoc($result)

The variable $result will be an array of the values from the record returned by the function. That type of line is typically run in a while loop such as

while ($query = mysql_fecth_assoc($result)) {

so it will get the next record, perform some actions on that record, and repeat (i.e. get the next record). So, the loop will continue as long as the result of $row = mysql_fetch_assoc($result) is not false - as long as there are records being returned. As soo, as you have reached the last record, that condition will return false and the loop ends. So, basically, that while loop is an efficient means of looping through all the records in a query result set.

 

And, as stated above, your variable names are messed up. The code should look something like this (added some error handling)

//Define the query to run as the variable $query
$query = 'SELECT `Name`, `Username` FROM `users`';
//Executre the query and assign the result set to the variable $result
$result = mysql_query($query);

if(!$result)
{
    echo "The query failed so a FALSE value was assigned to $result<br>";
    echo "The error was " . mysql_error();
}
elseif(mysql_num_rows($result)==0)
{
    echo "There were no rows (i.e. records) in the result set";
}
else
{
    //Records were returned, let's process them
    echo "Here are the results of the query:<br>\n";
    while ($row = mysql_fecth_assoc($result))
    {
        //Get values from the row/record based on the field name as the array index
        //And assign to variable to be used in the processign code
        $name = $row['Name'];
        $username = $row['Username'];
        //Output the results
        echo "Name: {$name}, Username: {$username}<br>\n";
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.