Jump to content

JOIN ON in combinattion with fetch_object


cK

Recommended Posts


$query = "SELECT test_jobs.id, test_jobs.title, test_jobcontacts.id FROM test_jobs LEFT JOIN test_jobcontacts ON test_jobs.contact_id = test_jobcontacts.id WHERE (test_jobs.category_id=\'$category[id]\' OR test_jobs.category_id=\'9\') AND ((TO_DAYS(NOW()) - TO_DAYS(test_jobs.timestamp)) <= test_jobs.days_valid) AND ((TO_DAYS(NOW()) - TO_DAYS(test_jobs.timestamp)) >= 0) ORDER BY test_jobs.timestamp DESC LIMIT $pager->offset,$pager->limit";

 

How does this (JOIN ON) work in combination with \"while($row = mysql_fetch_object($result))\"?

 

I used to take $row->id and so on... But do I know need to do $row->jobcontacts.id (what doesn\'t work) or....

Link to comment
https://forums.phpfreaks.com/topic/514-join-on-in-combinattion-with-fetch_object/
Share on other sites

Your sql statement is carried out, so it will return a number of rows like always...

 

Then php will grab them one at a time as usual.

 

And your coloumn names will be (after carrying out the query!):

 

while ($row = mysql_fetch_object($result)) {

$row->test_jobs.id;

$row->test_jobs.title;

$row->test_jobcontacts.id;

}

mysql_free_result($result);

 

 

P.

Doesn\'t seem to work with me :x

 

I get \"titleresults\" with $title = $row->test_jobs.title . \"results\";

 




function show_detail_page($id)

{

$query = \"SELECT test_jobs.id,test_jobs.title,test_jobs.description,test_jobcontacts.name FROM test_jobs LEFT JOIN test_jobcontacts ON test_jobs.contact_id = test_jobcontacts.id WHERE test_jobs.id=\'$id\' AND ((TO_DAYS(NOW()) - TO_DAYS(test_jobs.timestamp)) <= test_jobs.days_valid) AND ((TO_DAYS(NOW()) - TO_DAYS(test_jobs.timestamp)) >= 0)\";

$result = mysql_query($query) or error (\"Unable to connect to SQL server. Try again later.\");

$row = mysql_fetch_object($result);



if ($row)

{

 $title = $row->test_jobs.title . \"results\";



(...)

Hmm.... I\'m not sure if the . is bad for OO code (?) anyone ?

 

Try the following:

 




function show_detail_page($id)

{

$query = "SELECT test_jobs.id AS id ,test_jobs.title AS title ,test_jobs.description AS description,test_jobcontacts.name AS name FROM test_jobs LEFT JOIN test_jobcontacts ON test_jobs.contact_id = test_jobcontacts.id WHERE test_jobs.id=\'$id\' AND ((TO_DAYS(NOW()) - TO_DAYS(test_jobs.timestamp)) <= test_jobs.days_valid) AND ((TO_DAYS(NOW()) - TO_DAYS(test_jobs.timestamp)) >= 0)";

$result = mysql_query($query) or error ("Unable to connect to SQL server. Try again later.");

$row = mysql_fetch_object($result);



if ($row)

{

$title = $row->title . "results";



(...)

 

P.

 

I.e. renaming your result names... alternatively you MAY use $row->\"table.fieldname\" (?)

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.