jwwceo Posted August 26, 2006 Share Posted August 26, 2006 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 Databaseinclude '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>";?> Quote Link to comment https://forums.phpfreaks.com/topic/18749-simple-question/ Share on other sites More sharing options...
shocker-z Posted August 26, 2006 Share Posted August 26, 2006 $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?:)RegardsLiam Quote Link to comment https://forums.phpfreaks.com/topic/18749-simple-question/#findComment-80859 Share on other sites More sharing options...
jwwceo Posted August 26, 2006 Author Share Posted August 26, 2006 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??? Quote Link to comment https://forums.phpfreaks.com/topic/18749-simple-question/#findComment-80863 Share on other sites More sharing options...
tomfmason Posted August 26, 2006 Share Posted August 26, 2006 ^ 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 Databaseinclude '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 Quote Link to comment https://forums.phpfreaks.com/topic/18749-simple-question/#findComment-80864 Share on other sites More sharing options...
shocker-z Posted August 26, 2006 Share Posted August 26, 2006 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 :) Quote Link to comment https://forums.phpfreaks.com/topic/18749-simple-question/#findComment-80866 Share on other sites More sharing options...
jwwceo Posted August 26, 2006 Author Share Posted August 26, 2006 Nice. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/18749-simple-question/#findComment-80869 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.