tqla Posted December 29, 2008 Share Posted December 29, 2008 Can someone help me with this? I am trying look at the "title" field of a specific row in a table to see if it matches the "category_group" field of any row in a different table. If there is a match I want to echo a message. Is my "if statement" correct? It's not echoing anything and there is a match. <?php $sql1 = "SELECT title FROM category WHERE id = $id"; $result1 = mysql_query($sql1) or die(mysql_error()); $row1 = mysql_fetch_assoc($result1); $cat = $row1['title']; $sql2 = "SELECT category_group FROM content"; $result2 = mysql_query($sql1) or die(mysql_error()); $row2 = mysql_fetch_assoc($result1); $catgroup = $row2['category_group']; if ( $cat == $catgroup ) { echo "You have content with category groups that match the title"; } ?> Quote Link to comment Share on other sites More sharing options...
RussellReal Posted December 29, 2008 Share Posted December 29, 2008 $q = "SELECT * FROM `category` JOIN `content` ON (category.title = content.category_group) WHERE category.id = '{$id}'"; if (@mysql_fetch_array(@mysql_query($q))) { echo "You have content with category groups that match the title"; } Quote Link to comment Share on other sites More sharing options...
tqla Posted December 30, 2008 Author Share Posted December 30, 2008 Thank RussellReal, man I really gotta learn about JOIN - like today. But it's not echoing the statement. Here is my test script <?php include('includes/connnection.inc.php'); $id = $_GET['id']; $q = "SELECT 'title' FROM `category` JOIN `content` ON (category.title == content.category_group) WHERE category.id = '{$id}'"; if (@mysql_fetch_array(@mysql_query($q))) { echo "You have content with category groups that match the title"; } ?> I go to this URL on my WAMP server: localhost/cms/test.php?id=3 And nothing. Not error, no echo. But the "title" field in ID3 in the "category" table matches several of the "category_group" fields in rows in the "content" table. It should echo. Right? Quote Link to comment Share on other sites More sharing options...
revraz Posted December 30, 2008 Share Posted December 30, 2008 There is no error because you are not grabbing any errors. Remove the single quotes around title and it should work. Quote Link to comment Share on other sites More sharing options...
RussellReal Posted December 30, 2008 Share Posted December 30, 2008 good catch revraz it should be ` ` not ' ' Quote Link to comment Share on other sites More sharing options...
tqla Posted December 30, 2008 Author Share Posted December 30, 2008 Thanks revraz but it's still not echoing. I even cleared the value the "title" field of id 3 of the "category" table and several of the "category_group" fields in the "content" table and it still will not echo. <?php include('includes/connnection.inc.php'); $id = $_GET['id']; $q = "SELECT title FROM category JOIN content ON (category.title == content.category_group) WHERE category.id = '{$id}'"; if (@mysql_fetch_array(@mysql_query($q))) { echo "You have content with category groups that match the title"; } ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 30, 2008 Share Posted December 30, 2008 Using the "@" suppresses error messages. Don't use it when debugging scripts. Do this and see what is displayed: <?php include('includes/connnection.inc.php'); $id = $_GET['id']; $q = "SELECT title FROM category JOIN content ON (category.title == content.category_group) WHERE category.id = '{$id}'"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); if (mysql_num_rows($rs) > 0) echo "You have content with category groups that match the title"; ?> Ken Quote Link to comment Share on other sites More sharing options...
newb Posted December 30, 2008 Share Posted December 30, 2008 try this <?php include('includes/connnection.inc.php'); $id = $_GET['id']; $q = "SELECT title FROM category JOIN content ON (category.title == content.category_group) WHERE category.id = '{$id}'"; if (@mysql_fetch_array(@mysql_query($q))) { echo "You have content with category groups that match the title"; } ?> Quote Link to comment Share on other sites More sharing options...
RussellReal Posted December 30, 2008 Share Posted December 30, 2008 I messed up the first post.. but I modified it like a minute later when I noticed it.. I don't really know if it matters, but (category.title == content.category_group) is usually (category.title = content.category_group) just 1 equal sign.. Quote Link to comment Share on other sites More sharing options...
tqla Posted December 30, 2008 Author Share Posted December 30, 2008 Yes RussellReal! That did it! One equal sign. Thank you and thanks to all of you for helping out too. I'm going to hit the books on JOIN tonight. 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.