Jump to content

[SOLVED] Help with IF Statement


tqla

Recommended Posts

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";
}

?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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";
}
?>

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.