karatekid36 Posted May 9, 2007 Share Posted May 9, 2007 I am not sure why this error is coming up with my code. Everything seems to make sense to me... Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/carrigan/public_html/php_attend/view_users.php on line 108 Last Name First Name Status Email Cellphone Screen Name Birthday Hometown State T-Shirt Graduation Year Pledge Semester Pledge Year Pledge Class Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/carrigan/public_html/php_attend/view_users.php on line 134 <?php # Script 8.5 - view_users.php # (4th version after Scripts 7.4, 7.6, & 8.2) // This script retrieves all the records from the brothers table. // This new version paginates the query results. $page_title = 'View the Current Users'; include ('includes/header.html'); // Page header. echo '<h1 id="mainhead">Brothers</h1>'; require_once ('mysql_connect.php'); // Connect to the db. // Number of records to show per page: $display = 100; // Determine how many pages there are. if (isset($_GET['np'])) { // Already been determined. $num_pages = $_GET['np']; } else { // Need to determine. // Count the number of records $query = "SELECT COUNT(*) FROM brothers ORDER BY last_name ASC"; $result = mysql_query ($query); $row = mysql_fetch_array ($result, MYSQL_NUM); $num_records = $row[0]; // Calculate the number of pages. if ($num_records > $display) { // More than 1 page. $num_pages = ceil ($num_records/$display); } else { $num_pages = 1; } } // End of np IF. // Determine where in the database to start returning results. if (isset($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } //Default column Links. $link1 = "{$_SERVER['PHP_SELF']}?sort=lna"; $link2 = "{$_SERVER['PHP_SELF']}?sort=fna"; $link3 = "{$_SERVER['PHP_SELF']}?sort=sta"; //Determine the sorting order. if(isset($_GET['sort'])) { //Use the exising sorting order. switch ($_GET['sort']) { case 'lna': $order_by = 'last_name ASC'; $link1 = "{$_SERVER['PHP_SELF']}?sort=lnd"; break; case 'lnd': $order_by = 'last_name DESC'; $link1 = "{$_SERVER['PHP_SELF']}?sort=lnd"; break; default: $order_by = 'last_name ASC'; break; } // $sort will be appended to the pagination links $sort = $_GET['sort']; } else { $order_by = 'last name ASC'; $sort = 'lnd'; } // Make the query. $query = "SELECT user_id, last_name, first_name, email, cell, sn, birthday, home_town, state, tshirt, grad_year, pledge_semester, pledge_year, pledge_class, status FROM brothers ORDER BY $order_by ASC LIMIT $start, $display"; $result = mysql_query ($query); // Run the query. // Table header. echo '<table align="center" cellspacing="0" cellpadding="5"> <tr> <td align="left"><b><a href="' . $link1 . '">Last Name</a></b></td> <td align="left"><b>First Name</b></td> <td align="left"><b>Status</b></td> <td align="left"><b>Email</b></td> <td align="left"><b>Cellphone</b></td> <td align="left"><b>Screen Name</b></td> <td align="left"><b>Birthday</b></td> <td align="left"><b>Hometown</b></td> <td align="left"><b>State</b></td> <td align="left"><b>T-Shirt</b></td> <td align="left"><b>Graduation Year</b></td> <td align="left"><b>Pledge Semester</b></td> <td align="left"><b>Pledge Year</b></td> <td align="left"><b>Pledge Class</b></td> </tr> '; // Fetch and print all the records. $bg = '#eeeeee'; // Set the background color. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color. echo '<tr bgcolor="' . $bg . '"> <td align="left">' . $row['last_name'] . '</td> <td align="left">' . $row['first_name'] . '</td> <td align="left">' . $row['status'] . '</td> <td align="left">' . $row['email'] . '</td> <td align="left">' . $row['cell'] . '</td> <td align="left">' . $row['sn'] . '</td> <td align="left">' . $row['birthday'] . '</td> <td align="left">' . $row['home_town'] . '</td> <td align="left">' . $row['state'] . '</td> <td align="left">' . $row['tshirt'] . '</td> <td align="left">' . $row['grad_year'] . '</td> <td align="left">' . $row['pledge_semester'] . '</td> <td align="left">' . $row['pledge_year'] . '</td> <td align="left">' . $row['pledge_class'] . '</td> </tr> '; } echo '</table>'; mysql_free_result ($result); // Free up the resources. mysql_close(); // Close the database connection. // Make the links to other pages, if necessary. if ($num_pages > 1) { echo '<br /><p>'; // Determine what page the script is on. $current_page = ($start/$display) + 1; // If it's not the first page, make a Previous button. if ($current_page != 1) { echo '<a href="view_users.php?s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> '; } // Make all the numbered pages. for ($i = 1; $i <= $num_pages; $i++) { if ($i != $current_page) { echo '<a href="view_users.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '">' . $i . '</a> '; } else { echo $i . ' '; } } // If it's not the last page, make a Next button. if ($current_page != $num_pages) { echo '<a href="view_users.php?s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a>'; } echo '</p>'; } // End of links section. include ('includes/footer.html'); // Include the HTML footer. ?> Quote Link to comment https://forums.phpfreaks.com/topic/50728-solved-why-is-this-happening-with-this-code/ Share on other sites More sharing options...
Psycho Posted May 9, 2007 Share Posted May 9, 2007 There is an error in your query. Add some error handling. After you do a query such as this $result = mysql_query ($query); Add this right after: if (!$result) { echo "The Query:<br>$query<br>Produced the error:<br>".mysql_error(); exit; } Quote Link to comment https://forums.phpfreaks.com/topic/50728-solved-why-is-this-happening-with-this-code/#findComment-249383 Share on other sites More sharing options...
karatekid36 Posted May 10, 2007 Author Share Posted May 10, 2007 That was helpful, but still even with it telling me more directly the error, I still can not figure out how it is wrong. Could some one please save me? Quote Link to comment https://forums.phpfreaks.com/topic/50728-solved-why-is-this-happening-with-this-code/#findComment-249842 Share on other sites More sharing options...
per1os Posted May 10, 2007 Share Posted May 10, 2007 Could you post the error? Chances are it is the status column in this select // Make the query. $query = "SELECT user_id, last_name, first_name, email, cell, sn, birthday, home_town, state, tshirt, grad_year, pledge_semester, pledge_year, pledge_class, `status` Encapsulate it in back ticks ( ` ) and see if that works. Because status is a reservered column name in MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/50728-solved-why-is-this-happening-with-this-code/#findComment-249854 Share on other sites More sharing options...
karatekid36 Posted May 10, 2007 Author Share Posted May 10, 2007 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/carrigan/public_html/php_attend/view_users.php on line 108 Last Name First Name Status Email Cellphone Screen Name Birthday Hometown State T-Shirt Graduation Year Pledge Semester Pledge Year Pledge Class Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/carrigan/public_html/php_attend/view_users.php on line 134 This is the error. I have been messing with it for a little while now and when I go to the page, it gives me an error, but when I click on the Last Name portion of the table I have built, it will display the items. This is really weird.... Quote Link to comment https://forums.phpfreaks.com/topic/50728-solved-why-is-this-happening-with-this-code/#findComment-249857 Share on other sites More sharing options...
Psycho Posted May 10, 2007 Share Posted May 10, 2007 Care to share what the error was from the debug code I gave you? That was why I asked you to add that. Kind of hard to tell you how to fix it if we don't know what the error is. However, I think I can guess the problem. $query = "SELECT COUNT(*) FROM brothers ORDER BY last_name ASC"; What are you wanting to count specifically? If you are wanting to count all the records in that tablle, then you should not have an ORDER BY? If you are wanting to count the number of records for each last_name then you need a GROUP_BY. See this page: http://dev.mysql.com/doc/refman/5.0/en/counting-rows.html Quote Link to comment https://forums.phpfreaks.com/topic/50728-solved-why-is-this-happening-with-this-code/#findComment-249859 Share on other sites More sharing options...
karatekid36 Posted May 10, 2007 Author Share Posted May 10, 2007 Oh I am sorry I misunderstood. Here is the error.... The Query: SELECT user_id, last_name, first_name, email, cell, sn, birthday, home_town, state, tshirt, grad_year, pledge_semester, pledge_year, pledge_class, status FROM brothers ORDER BY last name ASC LIMIT 0, 100 Produced the error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'name ASC LIMIT 0, 100' at line 1 Sorry about that. I am doing this and studying for exams.... Quote Link to comment https://forums.phpfreaks.com/topic/50728-solved-why-is-this-happening-with-this-code/#findComment-249870 Share on other sites More sharing options...
karatekid36 Posted May 10, 2007 Author Share Posted May 10, 2007 Here is the most current code <?php # Script 8.5 - view_users.php # (4th version after Scripts 7.4, 7.6, & 8.2) // This script retrieves all the records from the brothers table. // This new version paginates the query results. $page_title = 'View the Current Users'; include ('includes/header.html'); // Page header. echo '<h1 id="mainhead">Brothers</h1>'; require_once ('mysql_connect.php'); // Connect to the db. // Number of records to show per page: $display = 100; // Determine how many pages there are. if (isset($_GET['np'])) { // Already been determined. $num_pages = $_GET['np']; } else { // Need to determine. // Count the number of records $query = "SELECT COUNT(*) FROM brothers ORDER BY last_name ASC"; $result = mysql_query ($query); $row = mysql_fetch_array ($result, MYSQL_NUM); $num_records = $row[0]; // Calculate the number of pages. if ($num_records > $display) { // More than 1 page. $num_pages = ceil ($num_records/$display); } else { $num_pages = 1; } } // End of np IF. // Determine where in the database to start returning results. if (isset($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } //Default column Links. $link1 = "{$_SERVER['PHP_SELF']}?sort=lna"; $link2 = "{$_SERVER['PHP_SELF']}?sort=fna"; $link3 = "{$_SERVER['PHP_SELF']}?sort=sta"; //Determine the sorting order. if(isset($_GET['sort'])) { //Use the exising sorting order. switch ($_GET['sort']) { case 'lna': $order_by = 'last_name ASC'; $link1 = "{$_SERVER['PHP_SELF']}?sort=lnd"; break; case 'lnd': $order_by = 'last_name DESC'; $link1 = "{$_SERVER['PHP_SELF']}?sort=lnd"; break; default: $order_by = 'last_name ASC'; break; } // $sort will be appended to the pagination links $sort = $_GET['sort']; } else { $order_by = 'last name ASC'; $sort = 'lnd'; } // Make the query. $query = "SELECT user_id, last_name, first_name, email, cell, sn, birthday, home_town, state, tshirt, grad_year, pledge_semester, pledge_year, pledge_class, status FROM brothers ORDER BY $order_by LIMIT $start, $display"; $result = mysql_query ($query); // Run the query. if (!$result) { echo "The Query: $query Produced the error: ".mysql_error(); exit; } // Table header. echo '<table align="center" cellspacing="0" cellpadding="5"> <tr> <td align="left"><b><a href="' . $link1 . '">Last Name</a></b></td> <td align="left"><b>First Name</b></td> <td align="left"><b>Status</b></td> <td align="left"><b>Email</b></td> <td align="left"><b>Cellphone</b></td> <td align="left"><b>Screen Name</b></td> <td align="left"><b>Birthday</b></td> <td align="left"><b>Hometown</b></td> <td align="left"><b>State</b></td> <td align="left"><b>T-Shirt</b></td> <td align="left"><b>Graduation Year</b></td> <td align="left"><b>Pledge Semester</b></td> <td align="left"><b>Pledge Year</b></td> <td align="left"><b>Pledge Class</b></td> </tr> '; // Fetch and print all the records. $bg = '#eeeeee'; // Set the background color. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color. echo '<tr bgcolor="' . $bg . '"> <td align="left">' . $row['last_name'] . '</td> <td align="left">' . $row['first_name'] . '</td> <td align="left">' . $row['status'] . '</td> <td align="left">' . $row['email'] . '</td> <td align="left">' . $row['cell'] . '</td> <td align="left">' . $row['sn'] . '</td> <td align="left">' . $row['birthday'] . '</td> <td align="left">' . $row['home_town'] . '</td> <td align="left">' . $row['state'] . '</td> <td align="left">' . $row['tshirt'] . '</td> <td align="left">' . $row['grad_year'] . '</td> <td align="left">' . $row['pledge_semester'] . '</td> <td align="left">' . $row['pledge_year'] . '</td> <td align="left">' . $row['pledge_class'] . '</td> </tr> '; } echo '</table>'; mysql_free_result ($result); // Free up the resources. mysql_close(); // Close the database connection. // Make the links to other pages, if necessary. if ($num_pages > 1) { echo '<br /><p>'; // Determine what page the script is on. $current_page = ($start/$display) + 1; // If it's not the first page, make a Previous button. if ($current_page != 1) { echo '<a href="view_users.php?s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> '; } // Make all the numbered pages. for ($i = 1; $i <= $num_pages; $i++) { if ($i != $current_page) { echo '<a href="view_users.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '">' . $i . '</a> '; } else { echo $i . ' '; } } // If it's not the last page, make a Next button. if ($current_page != $num_pages) { echo '<a href="view_users.php?s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a>'; } echo '</p>'; } // End of links section. include ('includes/footer.html'); // Include the HTML footer. ?> Quote Link to comment https://forums.phpfreaks.com/topic/50728-solved-why-is-this-happening-with-this-code/#findComment-249879 Share on other sites More sharing options...
per1os Posted May 10, 2007 Share Posted May 10, 2007 ORDER BY last name ASC LIMIT 0, 100 CHANGE TO ORDER BY last_name ASC LIMIT 0, 100 Quote Link to comment https://forums.phpfreaks.com/topic/50728-solved-why-is-this-happening-with-this-code/#findComment-249880 Share on other sites More sharing options...
karatekid36 Posted May 10, 2007 Author Share Posted May 10, 2007 WOW! I think I a little tired. I can not believe I missed that. By the way, I love this forum. You guys have helped me so much and you guys have helped me learn so much PHP and MYSQL. Thank you very much guys! Quote Link to comment https://forums.phpfreaks.com/topic/50728-solved-why-is-this-happening-with-this-code/#findComment-249884 Share on other sites More sharing options...
Psycho Posted May 10, 2007 Share Posted May 10, 2007 Please mark the thread as SOLVED Quote Link to comment https://forums.phpfreaks.com/topic/50728-solved-why-is-this-happening-with-this-code/#findComment-249890 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.