Suchy Posted March 12, 2007 Share Posted March 12, 2007 I am getting errors in my Guestbook: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/***/***/***/delete.php on line 21 I can not figure out what is wrong. Here is the code: <?php // Database-specific information: DEFINE (DB_USER, "******"); DEFINE (DB_PASSWORD, "*****"); DEFINE (DB_HOST, "*****"); DEFINE (DB_NAME, "*****"); // conect to database function function connectDB() { // Connect to MySQL: $db_connection = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD); // Select the database: mysql_select_db (DB_NAME); } function getRows($result) { $rows = array(); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $rows[] = $row; } } return $rows; } function in($key) { $value = $_POST[$key]; if (!isset($value)) { $value = $_GET[$key]; } return $value; } // connect to database connectDB(); $query = "SELECT * FROM entries ORDER BY 'time'"; $result = mysql_query($query); $entriesResults = getRows($result); if(in("del") ){ $id = $_GET['del']; $query = "SELECT * FROM entry WHERE id = '$id'"; $result = mysql_query($query); $delResults = getRows($result); $delResult = $delResults[0]; } // confirmation of delete function if(in("confirm")) { $id = $_GET['confirm']; $query = "DELETE FROM entry WHERE id = '$id'"; $result = mysql_query($query); if(!$result) { print(mysql_error()); } } ?> <html> <head> <title>Guest Book</title> <style type="text/css" media="all">@import "images/style.css";</style> </head> <body> <div class="content"> <div class="center"> <h2><a href="#">Administration Panel </a></h2> <p> <a href="*****" class="link">Back to the previous page.</a><br /> <!-- if DELETE THIS ENTRY pressed, then confirmation shows up /--> <? if(in("del")): ?> <!-- prints out name of entry that I selected to delete /--> <p>Are you sure you want to delete entry <strong> <?=$delResult['name']?> </strong>?</p> <!-- if YES pressed, then id of the current entry passed to confirm function /--> <!-- if NO pressed, then goes back to previous screen /--> <p><a href="<?=$_SERVER['PHP_SELF']."?confirm=".$delResult['id']?>">Yes</a> - <a href="<?=$_SERVER['PHP_SELF']?>">No</a></p> <!-- when entry was deleted prints success message and link to home page /--> <? elseif(in("confirm")): ?> <p>Successfully deleted, return to <a href="*****">Home</a></p> <!-- but before prnts all entries with delete buttons next to them /--> <? else: ?> <!-- loop displays all name (entries) /--> <?php foreach($entriesResults as $curResult): ?> <!-- prints name and delete button bext to them /--> <h2><?=$curResult['name'] ?> <- <a href="<?=$_SERVER['PHP_SELF']."?del=".$curResult['id'] ?>">delete this entry</a></h2> <!-- end of loop /--> <?php endforeach; ?> <!-- emd of if statement, ( after delete button presses - it gives confirmation ) /--> <? endif; ?> </div> </div> </body> </html> What is causing this error? Link to comment https://forums.phpfreaks.com/topic/42295-guestbook-not-working/ Share on other sites More sharing options...
JasonLewis Posted March 12, 2007 Share Posted March 12, 2007 change all appearances of this: $result = mysql_query($query); to this: $result = mysql_query($query) or die("Error: ".mysql_error()); the problem is that there is an error with one of your queries and so you need to find any mysql queries and place that die statement on the end. Link to comment https://forums.phpfreaks.com/topic/42295-guestbook-not-working/#findComment-205198 Share on other sites More sharing options...
skali Posted March 12, 2007 Share Posted March 12, 2007 Try function getRows(&$result) { $rows = array(); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $rows[] = $row; } } return $rows; } Link to comment https://forums.phpfreaks.com/topic/42295-guestbook-not-working/#findComment-205344 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.