CavemanUK Posted May 23, 2008 Share Posted May 23, 2008 Hi, im pretty new to PHP coding.. ive dabbled with modifying CMS packages in the past but its only the last few weeks ive dived in and written my first complete site. Because its my first, i can guarantee its very sloppy and baddly structured. Luckily its a private site thats password protected so its unlikely it will get attacked.. anyway.. getting back on subject, i was wondering about coding etiquette... i have a few basic questions that i was hoping people could give a basic answer to... firstly, declaring variables. Is this a good idea. is it required? and do I need to clear/unload them at the end of the php script? secondly, it seems the following piece of code is the common way of querying a database... $query = "SELECT * FROM table"; $result = mysql_query($query); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { ..code } is this completely necessary for every query (with multiple records)? i mean couldnt the variables be left out and be shortened to.. while ($row = mysql_fetch_array( mysql_query("SELECT * FROM table"), MYSQL_ASSOC)) or does this cause problems? thirdly, Im not an expert on html or css coding and have had a little trouble using <div> tags... for this site, i reverted back to using tables as it seemed much each for working with the data from mysql. Is this ok, or do i really need to start working with <div>'s. has anyone got a good tutorial for working with <div>'s on a tabular layout? Hope you guys can see your way to helping me out.. Caveman Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted May 23, 2008 Share Posted May 23, 2008 This code snippet should answer your number two. <?php $idx = 0; $i = 0; while( $row = get_row( query() ) ) { echo $row . '<br /><br />'; // Comment out this if block to see what would happen in the real world if( $i++ == 10 ) { break; } } function get_row( $data ) { global $idx; if( $idx > 2 ) { return false; } echo 'get_row()<br />'; return $data[$idx++]; } function query() { global $idx; echo 'query()<br />'; $idx = 0; return array( 'a', 'b', 'c' ); } ?> Quote Link to comment Share on other sites More sharing options...
deadonarrival Posted May 25, 2008 Share Posted May 25, 2008 You don't have to declare a variable's EXISTANCE, but make sure it has a value before you try to compare it to anything It's not required or expected, but it doesn't do any harm. There's no need to clear them at the end of the script, that happens anyway. The method you show for accessing a database is almost universal, simply because it works well. You CAN take the variables away, but it starts to get messy. If you're desperate to shorten it, why not make your own function along the lines of function getrow($sql) { $qry = mysql_query($sql); return mysql_fetch_array($qry,MYSQL_ASSOC); } And use while($row = getrow("SELECT * FROM where WHILE foo=bar")) { #Sing the blues } To cut it down to something readable, but a little more compact. Quote Link to comment 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.