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
https://forums.phpfreaks.com/topic/138799-solved-help-with-if-statement/
Share on other sites

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

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?

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

 

 

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

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

 

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..

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.