Jump to content

Archived

This topic is now archived and is closed to further replies.

jwwceo

Simple question

Recommended Posts

I have a very basic question. Below is a simple snippet to retrieve info from a database. I tweaked it to work with my fields and it works fine. My question is...why is $info not declared at the start of the script. Don't I have to declare all my variables??

Also is there any command that doesn't require echo to go on every line that's outputted...but rather just outputs everything between a start and end "print" command??
_________________________
<?php
// Connects to your Database

include 'library/config.php';
include 'library/opendb.php';

$data = mysql_query("SELECT * FROM shirts") or die(mysql_error());

echo "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
echo "<tr>";
echo "<th>Name</th> <td>".$info['name']."</td> ";
echo "<th>Web Site</th> <td>".$info['site']." </td>";
echo "<th>Comment</th> <td>".$info['comment'] . "</td> ";
echo "<th>Link</th> <td>".$info['link']."</td>";
echo "<th>Image</th> <td>".$info['image']."</td> ";
echo "<th>Commission</th> <td>".$info['commission']." </td>";
echo "<th>Active</th> <td>".$info['active']."</td></tr>";

}
echo "</table>";
?>

Share this post


Link to post
Share on other sites
$info holds the array of "mysql_fetch_array( $data )" so your running the query and pulling out an array for each row returned and while ($info = mysql_fetch_array( $data ) means that while $info can be set as somthing (not NULL) then set $info into the row array.

If that makes sence to you?

:)

Regards
Liam

Share this post


Link to post
Share on other sites
But why isn't it required to be declared at the top of the script. Even if it's null. I though all variables need to be declared before you can use them???

Share this post


Link to post
Share on other sites
^ beat me to it shocker-z. I think that I need to retake a typeing class.


You are declaring info as [code=php:0]mysql_fetch_array($data)[/code]. What this does is fetches the results from the data query and places them into an array. Then you call individual results in the form of [code=php:0]$info['your_field'][/code].

Now as far as the echoing of each line. It is unnessicary. I would personaly just use one like this

[code]
<?php
// Connects to your Database

include 'library/config.php';
include 'library/opendb.php';

$data = mysql_query("SELECT * FROM shirts") or die(mysql_error());

echo "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
echo "<tr>
           <th>Name</th> <td>".$info['name']."</td>
           <th>Web Site</th> <td>".$info['site']." </td>
           <th>Comment</th> <td>".$info['comment'] . "</td>
           <th>Link</th> <td>".$info['link']."</td>
           <th>Image</th> <td>".$info['image']."</td>
           <th>Commission</th> <td>".$info['commission']." </td>
            <th>Active</th> <td>".$info['active']."</td>
       </tr>";
}
echo "</table>";
?>[/code]


The way that you have it is fine but I personly do not like to type all of those echos so I just use the one.


Good luck,
Tom

Share this post


Link to post
Share on other sites
but by doing this we are setting $info.. you can create a new variable in PHP at ANY stage in your script they dont all have to be created at the start.

The beauty of PHP :)

Share this post


Link to post
Share on other sites

×

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.