parodys Posted February 8, 2009 Share Posted February 8, 2009 Hi there, A quick question, i have a simple article website I'm putting together. My database is set up like this; # articles id, title, article, created_at, catid # category catid, catname Ive passed a catid through links as below : <?php $query = "SELECT catname, catid FROM category"; $result = mysql_query($query) or die(mysql_error()); // make the catogories list and then passes the values on to archive page $entry = '<ol>'; while ($row = mysql_fetch_array($result, MYSQL_NUM)) { list($catname, $catid) = $row; $entry .= "<li><a href=\"?view=archive&catid=" . $catid . "\">$catname</a></li>\r\n"; } $entry .= '</ol>'; // show the categories list echo $entry; ?> now it passes " http://localhost/codedad/?view=archive&catid=1 " on to the archive page. my problem is getting the archive page to display only the articles under the catid=1. below is the function ive made to display the catitems : <?php function displaycatitems() { db_connect(); $catid = $_GET["catid"]; if(!isset($_GET['catid'])) { $query ="SELECT title, article FROM articles, category WHERE cat=category.catid=".$_GET['id']; $result = mysql_query($query) or die('Error : ' . mysql_error()); while($row = mysql_fetch_array($result)) { echo $row['title']. " - ". $row['article']; echo "<br />"; } } else { echo "No Articles"; } } ?> I know this is more than likely starting me in the face but for the last 3 hours ive been trying to get this to work to no avail! Any help would be appreciated. Link to comment https://forums.phpfreaks.com/topic/144336-solved-using-_get-to-query-the-database/ Share on other sites More sharing options...
ToonMariner Posted February 8, 2009 Share Posted February 8, 2009 <?php SELECT title, article FROM articles, category WHERE category.catid=".mysql_real_escape_string($_GET['id'])." AND articles.catid = category.catid" ?> Link to comment https://forums.phpfreaks.com/topic/144336-solved-using-_get-to-query-the-database/#findComment-757391 Share on other sites More sharing options...
gevans Posted February 8, 2009 Share Posted February 8, 2009 if(!isset($_GET['catid'])) { $query ="SELECT title, article FROM articles, category WHERE cat=category.catid=".$_GET['id']; $result = mysql_query($query) or die('Error : ' . mysql_error()); while($row = mysql_fetch_array($result)) { echo $row['title']. " - ". $row['article']; echo "<br />"; } } You're only running the query if $_GET['catid'] is not set; !isset($_GET['catid']) should be; isset($_GET['catid']) Link to comment https://forums.phpfreaks.com/topic/144336-solved-using-_get-to-query-the-database/#findComment-757393 Share on other sites More sharing options...
parodys Posted February 8, 2009 Author Share Posted February 8, 2009 wow thanks for the quick replies ! Ive done what you both suggested and i get an error. Notice: Undefined index: id in F:\wamp\www\codedad\dbfuncs.php on line 76 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 'AND articles.catid = category.catid' at line 1 Link to comment https://forums.phpfreaks.com/topic/144336-solved-using-_get-to-query-the-database/#findComment-757395 Share on other sites More sharing options...
gevans Posted February 8, 2009 Share Posted February 8, 2009 If you had a quick look you would have noticed that the wrong word was putting in for the get value SELECT title, article FROM articles, category WHERE category.catid=".mysql_real_escape_string($_GET['id'])." AND articles.catid = category.catid" should be <?php SELECT title, article FROM articles, category WHERE category.catid=".mysql_real_escape_string($_GET['catid'])." AND articles.catid = category.catid" ?> Link to comment https://forums.phpfreaks.com/topic/144336-solved-using-_get-to-query-the-database/#findComment-757399 Share on other sites More sharing options...
parodys Posted February 8, 2009 Author Share Posted February 8, 2009 rofl ! TRICKS !! thank you so much !!! Lots of respect to both of you! Link to comment https://forums.phpfreaks.com/topic/144336-solved-using-_get-to-query-the-database/#findComment-757402 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.