Jump to content

Simple question


jwwceo

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>";
?>
Link to comment
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
Link to comment
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
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.