kingnutter Posted March 11, 2009 Share Posted March 11, 2009 Hi everyone, I'm getting a bit mixed up with fetch_row / fetch_array / fetch_object. Can anyone tell me what would be best to get a list of all the titles and tracks in my database and what I am doing wrong in the script below? Many thanks, Gary <html> <head></head> <body> <!-- standard page header begins --> <p> <p> <table width="100%" cellspacing="0" cellpadding="5"> <tr> <td></td> </tr> <td bgcolor="Navy"><font size="-1" color="White"> <b>CDs</b></font> </td> </tr> </table> <!--standard page header ends --> ECHO CD TITLE / ISSUE TITLE HERE??? <?php // includes include('conf.php'); include('functions.php'); // check for record id if ((!isset($_GET['id']) || trim($_GET['id']) == '')) { die('Missing record ID: No such CD in Database'); } // open database connection $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect'); //select database mysql_select_db($db) or die ('Unable to select database'); // generate and execute query $id = $_GET['id']; $query = "SELECT track_no, track_title, track_artist FROM tracks WHERE moj_id = '$id' ORDER BY track_no"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); if (mysql_num_rows($result) > 0 { // get resultset as object while($row = mysql_fetch_array($result)) // print details { ?> <p /> <b><?php echo $row->track_no; ?>. </b> <?php echo $row->track_artist; ?>. : <?php echo $row->track_title; ?> <?php } else { ?> <p /> <font size="-1">That CD could not be located in the database.</font> <?php } } // close database connection mysql_close($connection); ?> <!-- standard page footer begins --> <p> <table width="100%" cellspacing="0" cellpadding="5"> <tr> <td align="center"><font size="-2"> A King Nutter Production</td> </tr> </table> <!-- standard page footer ends --> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/149012-solved-fetching-and-iterating/ Share on other sites More sharing options...
MadTechie Posted March 12, 2009 Share Posted March 12, 2009 change <b><?php echo $row->track_no; ?>. </b> <?php echo $row->track_artist; ?>. : <?php echo $row->track_title; ?> to <b><?php echo $row['track_no']; ?>. </b> <?php echo $row['track_artist']; ?>. : <?php echo $row['track_title']; ?> What they do.. mysql_fetch_assoc — Fetch a result row as an associative array meaning uses the field name $row['track_no'] = the track_no field $row['track_title'] = the track_no field mysql_fetch_row — Get a result row as an enumerated array meaning its a array with number keys ie $row[0] = the track_no field $row[1] = the track_title mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both Both of the above $row[0] = the track_no field $row['track_no'] = the track_no field $row[1] = the track_title $row['track_title'] = the track_no field Quote Link to comment https://forums.phpfreaks.com/topic/149012-solved-fetching-and-iterating/#findComment-782638 Share on other sites More sharing options...
kingnutter Posted March 12, 2009 Author Share Posted March 12, 2009 Thanks MadTechie, This is almost working now, but I'm having trouble with my brackets and knowing when to come in and out of php Is there any (Mac friendly) software that sort of splits the two up for me? I just get lost with all the nesting. Or do you just get a knack for it after months of coding? If the error jumps out at anyone in the code below please advise. Cheers everyone. <html> <head></head> <body> <!-- standard page header begins --> <p> <p> <table width="100%" cellspacing="0" cellpadding="5"> <tr> <td></td> </tr> <td bgcolor="Navy"><font size="-1" color="White"> <b>Mojo Cover CDs</b></font> </td> </tr> </table> <!--standard page header ends --> ECHO CD TITLE / ISSUE TITLE HERE??? <?php // includes include('conf.php'); include('functions.php'); // check for record id if ((!isset($_GET['id']) || trim($_GET['id']) == '')) { die('Missing record ID: No such CD in Database'); } // open database connection $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect'); //select database mysql_select_db($db) or die ('Unable to select database'); // generate and execute query $id = $_GET['id']; $query = "SELECT track_no, track_title, track_artist FROM tracks WHERE moj_id = '$id' ORDER BY track_no"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // if records present if (mysql_num_rows($result) > 0) { // iterate through resultset // print CD titles while($row = mysql_fetch_object($result)) { ?> <table> <tr> <td> <?php echo $row->track_no; ?> </td> <td> <?php echo $row->track_title; ?></td> <td><?php echo $row->track_artist; ?> </td> </tr> <?php } } // if no records present // display message ?> </table> <?php else { ?> <font size="-1">None CDs!</font> <?php } // close database connection mysql_close($connection); ?> <!-- standard page footer begins --> <p> <table width="100%" cellspacing="0" cellpadding="5"> <tr> <td align="center"><font size="-2"> A King Nutter Production</td> </tr> </table> <!-- standard page footer ends --> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/149012-solved-fetching-and-iterating/#findComment-783248 Share on other sites More sharing options...
redarrow Posted March 12, 2009 Share Posted March 12, 2009 just keep practicing your get there bro. <html> <head></head> <body> <!-- standard page header begins --> <p> <p> <table width="100%" cellspacing="0" cellpadding="5"> <tr> <td></td> </tr> <td bgcolor="Navy"><font size="-1" color="White"> <b>Mojo Cover CDs</b></font> </td> </tr> </table> <!--standard page header ends --> ECHO CD TITLE / ISSUE TITLE HERE??? <?php // includes include('conf.php'); include('functions.php'); // check for record id if ((!isset($_GET['id']) || trim($_GET['id']) == '')) { die('Missing record ID: No such CD in Database'); } // open database connection $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect'); //select database mysql_select_db($db) or die ('Unable to select database'); // generate and execute query $id = $_GET['id']; $query = "SELECT track_no, track_title, track_artist FROM tracks WHERE moj_id = '$id' ORDER BY track_no"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // if records present if (mysql_num_rows($result) > 0) { // iterate through resultset // print CD titles while($row = mysql_fetch_assocc($result)) { ?> <table> <tr> <td> <?php echo $row['track_no']; ?> </td> <td> <?php echo $row['track_title']; ?></td> <td><?php echo $row['track_artist']; ?> </td> </tr> <?php } //}<<<<<< was moved to below row off code. // if no records present // display message ?> </table> <?php }else{ // needed to be here ?> <font size="-1">None CDs!</font> <?php } // close database connection mysql_close($connection); ?> <!-- standard page footer begins --> <p> <table width="100%" cellspacing="0" cellpadding="5"> <tr> <td align="center"><font size="-2"> A King Nutter Production</td> </tr> </table> <!-- standard page footer ends --> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/149012-solved-fetching-and-iterating/#findComment-783252 Share on other sites More sharing options...
sasa Posted March 12, 2009 Share Posted March 12, 2009 change this part of code } } // if no records present // display message ?> </table> <?php else { to } // if no records present // display message ?> </table> <?php } else { move } before else Quote Link to comment https://forums.phpfreaks.com/topic/149012-solved-fetching-and-iterating/#findComment-783257 Share on other sites More sharing options...
kingnutter Posted March 12, 2009 Author Share Posted March 12, 2009 Thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/149012-solved-fetching-and-iterating/#findComment-783319 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.