Jump to content

Help with a Joined Query


datoshway

Recommended Posts

Below is my query, I have a column called boolActivate in my tblProperties table but I can't figure out how to put in a where clause so it reads something like WHERE boolActivate = 1

 

 

$strQuery = sprintf( "SELECT p.*, a.strAreaName FROM tblProperties p LEFT JOIN tblAreas a ON p.intAreaID = a.intID WHERE p.intID = %d", intval($intPropertyID));

 

Using MYSQL 5

Link to comment
https://forums.phpfreaks.com/topic/292273-help-with-a-joined-query/
Share on other sites

What's the problem? You've already written down the exact WHERE clause.

 

Besides that, you definitely need to learn how to write secure queries with prepared statements or manual escaping. No, casting with intval() is not a valid approach. It's actually harmful.

It's not in my query.  The problem is when I put it in the query like:  

 

$strQuery = sprintf( "SELECT p.*, a.strAreaName FROM tblProperties p LEFT JOIN tblAreas a ON p.intAreaID = a.intID WHERE p.intID = %d AND p.boolActivate = 1", intval($intPropertyID));

 

it doesn't work. 

“Doesn't work”? Can you be a bit more specific?  ::)

 

What is the problem? What's the expected result, and what's the actual result? We don't know anything about your project or the data you're working with, so if you want us to help you, then we need concrete info.

 

Have you echoed the resulting query and tried it in phpmyadmin?

Sure thing.  

 

Below is my full query.  This is for a detail page on a real estate website.  So if boolActivate=0 then the property should not display anything. 

$strQuery = sprintf( "SELECT p.*, a.strAreaName FROM tblProperties p LEFT JOIN tblAreas a ON p.intAreaID = a.intID WHERE p.intID = %d AND p.boolActivate = 1", intval($intPropertyID));
$queryGetProperty = $objDatabase->query($strQuery);

if ($objDatabase->num_rows($queryGetProperty) > 0) {
$arrRow = $objDatabase->fetch_assoc($queryGetProperty);
extract($arrRow);

if (strlen($dtDateAvailable) > 0) {
$dtDateAvailable = date("m/d/y", strtotime($dtDateAvailable));
}
if (strlen($dtDateAvailableSummer) > 0) {
$dtDateAvailable = date("m/d/y", strtotime($dtDateAvailableSummer));
}
if (strlen($dtDateAvailableWinter) > 0) {
$dtDateAvailable = date("m/d/y", strtotime($dtDateAvailableWinter));
}
}

$objDatabase->free_result($queryGetProperty);

$strQuery = sprintf( "SELECT * FROM tblPropertiesPhotos WHERE intPropertyID = %d", intval($intPropertyID));
$queryGetPhotos = $objDatabase->query($strQuery);

Below is a sample echo on the page for part of the content.

<?php 
								if (($dblPriceYearly > 0) && (strlen($dtDateAvailable) > 0)) {
									echo date("m/d/y", strtotime($dtDateAvailable)) . " (Y) ";
								}
								if (($dblPriceSummer > 0) && (strlen($dtDateAvailableSummer) > 0)) {
									echo date("m/d/y", strtotime($dtDateAvailableSummer)) . " (S) ";
								}
								if (($dblPriceWinter > 0) && (strlen($dtDateAvailableWinter) > 0)) {
									echo date("m/d/y", strtotime($dtDateAvailableWinter)) . " (W)";
								}
							?>

What i'm getting is a detail page with all the relaivant info.  I am looking for it to error out and not display any info just as it would if you went to the page and put in an incorrect ?=ID

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.