mike12255 Posted October 6, 2009 Share Posted October 6, 2009 So my code below does want i want, grabs info from a db and displays it according to last name, however there is an issue it says this error after showing the names: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mike1992/public_html/driveprotest.ca/petition.php on line does anyone know why? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>DriveProtest.ca</title> <meta name="keywords" content="ontario canada protest drive strike instructor liscense G1 G2 G" /> <meta name="description" content="" /> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <div id="container"> <div id="navcont"> <ul id="nav" > <li ><a href="index.php">Home</a></li> <li><a href="about.php">About</a></li> <li><a href="petition.php">Petition</a></li> <li><a href="contact.php">Contact</a></li> <li><a href="donate.php">Donate</a></li> <li></li> </ul> </div> <div id="content_main"> <h3><br /> </h3> <h3>News: Welcome To DriveProtest.ca</h3> <?php $con = mysql_connect('localhost','mike1992_driver','raven1'); $db = mysql_select_db('mike1992_petition',$con); $ip = GetHostByName($REMOTE_ADDR); $sql = "SELECT * FROM petition WHERE ip = '$ip'"; $res = mysql_query($sql) or die (mysql_error()); if (mysql_num_rows($res) == 0){ // has not signed it yet echo "<p><strong>You have not signed the petition yet!</strong> to do so <a href=\"petition2.php\">click here</a>!"; }else{ // their ip is in our db so dont display forum. echo "</p>You have already signed this peition so you cannot sign it, however you may view all the signatures it holds"; } $sql = "SELECT COUNT(*) AS CNT FROM petition"; $res = mysql_query($sql); echo "<br /> There are currently $CNT signatures!</p>"; $sql = "SELECT fname,lname,comment FROM petition ORDER BY lname DESC"; $res = mysql_query($sql) or die (mysql_error()); while ($res = mysql_fetch_array($res)){ //create the table echo $res['lname'] . " " .$res['fname'] . " " . $res['comment'] ; } ?> </p> <hr /><br /> <br /> <br /> </div> <div id="nav_main"><br /> <h4><strong>Advertisements</strong></h4> <ul> <li> <script type="text/javascript"><!-- google_ad_client = "pub-8552204471372682"; /* 160x600, created 8/26/09 */ google_ad_slot = "4663547552"; google_ad_width = 160; google_ad_height = 600; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </li> </ul> </div> </div> <div id="footer"> <div id="copyright">Copyright © 2009 DriveProtest.ca</div> <ul id="nav_footer"> <li><a href="index.php">Home</a> |</li> <li> <a href="contact.php">Contact</a> |</li> <li> <a href="#">Sitemap</a></li> </ul> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted October 6, 2009 Share Posted October 6, 2009 you're changing the value of $res from the database result to a row from the database change this $res = mysql_query($sql) or die (mysql_error()); while ($res = mysql_fetch_array($res)){ to [php $res = mysql_query($sql) or die (mysql_error()); while ($row = mysql_fetch_array($res)){ echo $row['lname'] . " " .$row['fname'] . " " . $row['comment'] ; } [/code] you do this several times. For each resultset use a different variable. example $sql_petition = "SELECT * FROM petition WHERE ip = '$ip'"; $res_petition = mysql_query($sql_petition) or die (mysql_error()); $sql_petition_cnt = "SELECT COUNT(*) AS CNT FROM petition"; $res_petition_cnt = mysql_query($sql_petition_cnt); $sql_name = "SELECT fname,lname,comment FROM petition ORDER BY lname DESC"; $res_name = mysql_query($sql_name) or die (mysql_error()); // etc also make sure to update the loops accordingly Quote Link to comment Share on other sites More sharing options...
mike12255 Posted October 6, 2009 Author Share Posted October 6, 2009 crap normally i call it row i was really gapping it yesterday...yeah i know about the messy code i just wanted to try somthing so wasnted worried about OOP thanks Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted October 6, 2009 Share Posted October 6, 2009 It's not OOP at all. It's just good practice for readability and avoiding giant headaches in the future. 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.