Joins aren't working for me???

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Joins aren't working for me???

Post by cturner »

I am trying to view the clearingsales and that clearingsales's photos on one page. I have tried many joins but I am not getting anywhere. Anyway I have posted my code below. Can someone please help me sort this out? Thanks in advance.

Code: Select all

require "config.php";
if(!isset($_GET['page'])) {
    $page = 1;
} else {
    $page = $_GET['page'];
}

// Define the number of results per page
$max_results = 1;

// Figure out the limit for the query based
// on the current page number
$from = (($page * $max_results) - $max_results);

// Perform MySQL query on only the current page number's results
$sql = mysql_query("SELECT * FROM clearingsales LEFT JOIN clearingsales_photos ON clearingsales.id = clearingsales_photos.parent_id LIMIT $from, $max_results") or die ("Could not query because: " . mysql_error());

while($row = mysql_fetch_array($sql)){
	$propertyname = "";
	if (isset($row['propertyname'])) {
	$propertyname = stripslashes($row['propertyname']);
	$propertyname = stripslashes($propertyname);
	$propertyname = stripslashes($propertyname);
	$propertyname = stripslashes($propertyname);
	$propertyname = mysql_real_escape_string($row['propertyname']);
	}
	$txt1 = "";
	if (isset($row['txt1'])) {
	$txt1 = stripslashes($row['txt1']);
	$txt1 = stripslashes($txt1);
	$txt1 = stripslashes($txt1);
	}
	$txt2 = "";
	if (isset($row['txt2'])) {
	$txt2 = stripslashes($row['txt2']);
	$txt2 = stripslashes($txt2);
	$txt2 = stripslashes($txt2);
	}
	$txt3 = "";
	if (isset($row['txt3'])) {
	$txt3 = stripslashes($row['txt3']);
	$txt3 = stripslashes($txt3);
	$txt3 = stripslashes($txt3);
	}
	$txt4 = "";
	if (isset($row['txt4'])) {
	$txt4 = stripslashes($row['txt4']);
	$txt4 = stripslashes($txt4);
	$txt4 = stripslashes($txt4);
	}
	$txt5 = "";
	if (isset($row['txt5'])) {
	$txt5 = stripslashes($row['txt5']);
	$txt5 = stripslashes($txt5);
	$txt5 = stripslashes($txt5);
	}
	$txt6 = "";
	if (isset($row['txt6'])) {
	$txt6 = stripslashes($row['txt6']);
	$txt6 = stripslashes($txt6);
	$txt6 = stripslashes($txt6);
	}
	$txt7 = "";
	if (isset($row['txt7'])) {
	$txt7 = stripslashes($row['txt7']);
	$txt7 = stripslashes($txt7);
	$txt7 = stripslashes($txt7);
	}
	$txt8 = "";
	if (isset($row['txt8'])) {
	$txt8 = stripslashes($row['txt8']);
	$txt8 = stripslashes($txt8);
	$txt8 = stripslashes($txt8);
	}
	$txt9 = "";
	if (isset($row['txt9'])) {
	$txt9 = stripslashes($row['txt9']);
	$txt9 = stripslashes($txt9);
	$txt9 = stripslashes($txt9);
	}
	$txt10 = "";
	if (isset($row['txt10'])) {
	$txt10 = stripslashes($row['txt10']);
	$txt10 = stripslashes($txt10);
	$txt10 = stripslashes($txt10);
	}
	$txt11 = "";
	if (isset($row['txt11'])) {
	$txt11 = stripslashes($row['txt11']);
	$txt11 = stripslashes($txt11);
	$txt11 = stripslashes($txt11);
	}
	$select1 = "";
	if (isset($row['select1'])) {
	$select1 = mysql_real_escape_string($row['select1']);
	}
	$select2 = "";
	if (isset($row['select2'])) {
	$select2 = mysql_real_escape_string($row['select2']);
	}
	$select3 = "";
	if (isset($row['select3'])) {
	$select3 = mysql_real_escape_string($row['select3']);
	}
	$select4 = "";
	if (isset($row['select4'])) {
	$select4 = mysql_real_escape_string($row['select4']);
	}
	$select5 = "";
	if (isset($row['select5'])) {
	$select5 = mysql_real_escape_string($row['select5']);
	}
	$select6 = "";
	if (isset($row['select6'])) {
	$select6 = mysql_real_escape_string($row['select6']);
	}
	$select7 = "";
	if (isset($row['select7'])) {
	$select7 = mysql_real_escape_string($row['select7']);
	}
	$select8 = "";
	if (isset($row['select8'])) {
	$select8 = mysql_real_escape_string($row['select8']);
	}
	$select9 = "";
	if (isset($row['select9'])) {
	$select9 = mysql_real_escape_string($row['select9']);
	}
	$select10 = "";
	if (isset($row['select10'])) {
	$select10 = mysql_real_escape_string($row['select10']);
	}
	$select11 = "";
	if (isset($row['select11'])) {
	$select11 = mysql_real_escape_string($row['select11']);
	}
	$date = "";
	if (isset($row['date'])) {
	$date = mysql_real_escape_string($row['date']);
	}
	$time = "";
	if (isset($row['time'])) {
	$time = mysql_real_escape_string($row['time']);
	}
	$accountname = "";
	if (isset($row['accountname'])) {
	$accountname = mysql_real_escape_string($row['accountname']);
	}
	$photos = "";
	if (isset($row['photos'])) {
	$photos = mysql_real_escape_string($row['photos']);
	}
	$comments = "";
	if (isset($row['comments'])) {
	$comments = mysql_real_escape_string($row['comments']);
	}
	$filename = "";
	if (isset($row['filename'])) {
	$filename = mysql_real_escape_string($row['filename']);
	}
	$id = "";
	if (isset($row['id'])) {
	$id = mysql_real_escape_string($row['id']);
	}

    // Build your formatted results here
	echo '<b><center>'.$propertyname.'</b><br />';
	echo $date.' commencing '.$time.'<br />';
	echo 'A/c '.$accountname.'</center><br />';
	echo $comments.'<br />';
	echo '<div class=title2>'.$select1.'</div> '.$txt1.'<br />';
	echo '<div class=title2>'.$select2.'</div> '.$txt2.'<br />';
	echo '<div class=title2>'.$select3.'</div> '.$txt3.'<br />';
	echo '<div class=title2>'.$select4.'</div> '.$txt4.'<br />';
	echo '<div class=title2>'.$select5.'</div> '.$txt5.'<br />';
	echo '<div class=title2>'.$select6.'</div> '.$txt6.'<br />';
	echo '<div class=title2>'.$select7.'</div> '.$txt7.'<br />';
	echo '<div class=title2>'.$select8.'</div> '.$txt8.'<br />';
	echo '<div class=title2>'.$select9.'</div> '.$txt9.'<br />';
	echo '<div class=title2>'.$select10.'</div> '.$txt10.'<br />';
	echo '<div class=title2>'.$select11.'</div> '.$txt11.'<br /><br />';
	echo '<a href=# onClick="popWin('.$filename.'.htm\', \'\', \'640\', \'480\')"><img src=http://www.blueguminteractive.biz/hyc/admin/images/'.$photos.' border=0 /></a><br />';
	echo '<a href="edit_clearing_sale.php?id='.$id.'">EDIT</a> ';
	echo '<a href="delete_clearing_sale.php?id='.$id.'">DELETE</a><br /><br />';
}
mysql_close();
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Whats the error you are getting?
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

I am getting no errors. It just doesn't display the photos.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

after your mysql_query line use the myql_num_rows function to get number of records returned and print it out to the screen.
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

I have tried mysql_num_rows but I get internal server error.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

why the heck are you using strip slashes three times on the same variable?

Code: Select all

if (isset($row['txt2'])) {
        $txt2 = stripslashes($row['txt2']);
        $txt2 = stripslashes($txt2);
        $txt2 = stripslashes($txt2);
        }

add the following two lines at the top of your file and it should show errors...

error_reporting(E_ALL);
ini_set('display_errors',1);


echo your query to ensure it's what you expect it to be... i.e.

Code: Select all

echo "SELECT * FROM clearingsales LEFT JOIN clearingsales_photos ON clearingsales.id = clearingsales_photos.parent_id LIMIT $from, $max_results";
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

When you run something like this does it connect okay?

Code: Select all

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?> 
Post Reply