Jump to content


Photo

Simple question


  • Please log in to reply
5 replies to this topic

#1 jwwceo

jwwceo
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts

Posted 26 August 2006 - 08:51 PM

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>";
?>

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 26 August 2006 - 08:58 PM

$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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 jwwceo

jwwceo
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts

Posted 26 August 2006 - 09:04 PM

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???

#4 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 26 August 2006 - 09:05 PM

^ beat me to it shocker-z. I think that I need to retake a typeing class.


You are declaring info as
mysql_fetch_array($data)
. 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
$info['your_field']
.

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

<?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>";
?>


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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#5 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 26 August 2006 - 09:06 PM

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 :)
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#6 jwwceo

jwwceo
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts

Posted 26 August 2006 - 09:10 PM

Nice. Thanks guys!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users