0

here
i dont understand what does this function do.
any can explain it to me
thanks beforehands

function query($qry) 
    { 
        if(!isset($this->database_link)) $this->connect(); 
        $result = mysql_query($qry, $this->database_link) or die("Error: ". mysql_error()); 
        $returnArray = array(); 
        $i=0; 
        while ($row = mysql_fetch_array($result, MYSQL_BOTH)) 
            if ($row) 
     $returnArray[$i++]=$row; 
mysql_free_result($result); 
        return $returnArray; 
    } 
}
2
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by LethargicCoder
1

Added comments to the end of each line.

here
i dont understand what does this function do.
any can explain it to me
thanks beforehands

function query($qry) // Pass in a query.
    { 
        if(!isset($this->database_link)) $this->connect(); // Check if  DB connection exists and if not, creates one.
        $result = mysql_query($qry, $this->database_link) or die("Error: ". mysql_error()); // Executes the query or returns error if fails.
        $returnArray = array(); // Create a new array variable.
        $i=0; // Initialize a counter.
        while ($row = mysql_fetch_array($result, MYSQL_BOTH)) // Start a loop, retrieving a row at a time of the results with both associative and numeric indices.
            if ($row) // Check if row was obtained.
                $returnArray[$i++]=$row; // If so, add row to the array variable
        // Loop finished
        mysql_free_result($result); // Free up resource.
        return $returnArray;  // return the array of query results.
    } 
}
0

hi
I again disturb you. i try to undertand of loop objective but nothink i understood.
can you explain me for what reason there is loop used and what is a purpose to create this array

$returnArray = array();

Thanks beforehands

0

The loop is because more than 1 result is expected from the query. Each result is going to be processed separately.

The returnarray will be a container to hold the results returned from the query. $row is an array containing the data for 1 result. For each query result there is a $row array added to the returnarray. This is defined before the loop so that it is in scope for the duration of the loop and still exists for the return.

Allowing you to release the DB result (resource). The function then returns $returnarray which is an array of arrays. Each array inside containing the data from 1 DB result.

0

i understood approximately 80 % can you show me in example (very simple) where i will use returnArray array
thank you for attention

0

i understood approximately 80 % can you show me in example (very simple) where i will use returnArray array
thank you for attention

Example showing something resembling a phone book

<?php

$qry = "select firstname, lastname, phone from friends"; // example query.

$r_arr = query($qry); // query() function called, passing above query ($qry), assigning returned array to $r_arr

?>
<table>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Phone number</th>
  </tr>
<?php
foreach($r_arr as $friend){ // $r_arr is the returned array, we loop over each element which happens to be another array containing the data from a db result.
?>
  <tr>
    <td><?php echo $friend['firstname'];?></td>
    <td><?php echo $friend['lastname']; ?></td>
    <td><?php echo $friend['phone']; ?></td>
  </tr>
<?php } ?>
</table>
0

hi again
thank you for attention
one question i would like to give you
i created such one below

<?php

$az=mysql_connect("localhost", "root", "19841223");
mysql_select_db("dbname", $az);
$query=mysql_query("select name, surname, email from student");

while($row=mysql_fetch_array($query))
{
    
   echo $row[0]." ".$row[1]."".row[2].<br/>";
}

?>

very simple
so both the one which you gave me and this does the same thing
but can you explain me (if possible) what is differense between this.
Thank you very very much for your attention

0

hi again
thank you for attention
one question i would like to give you
i created such one below

<?php

$az=mysql_connect("localhost", "root", "19841223");
mysql_select_db("dbname", $az);
$query=mysql_query("select name, surname, email from student");

while($row=mysql_fetch_array($query))
{
    
   echo $row[0]." ".$row[1]."".row[2].<br/>";
}

?>

very simple
so both the one which you gave me and this does the same thing
but can you explain me (if possible) what is differense between this.
Thank you very very much for your attention

They are essentially the same thing. In this one you are looping through $query and using the data in output immediately instead of placing in another array (container) to be passed to the calling script that would output the data.

I included more HTML but that is totally dependent on what you need.

The use of the query function in the first example removes the DB from the rest of your application. You provide a query and get data. No need to worry about what kind of database (assuming you can use standard SQL with it), getting connections(having to provide connection, username, password information) or how to handle response.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.