Jump to content

[SOLVED] QUERY Table based on user selection


JSHINER

Recommended Posts

I am trying to do the following:

 

If the selection = /index.php?var=ABC&var2=123

 

It will query table "table_ABC" and select rows with "123" using the follwing function:

 

function GoGet($db, $var= false, $var2= false) {

$arr = $db->getArray("SELECT * FROM table_$var WHERE table_$var.var2 = $var2 ORDER BY var2 ASC");

return $arr;

}

$page['goget'] = GoGet($db, $_GET['var'], $_GET['var2']);

 

However, it is not returning any results. Do I have an error somewhere?

I was just about to edit to explain why I had table_$var.$var2 .. I am using multiple tables - here is the full function:

 

function GoGet($db, $var= false, $var2= false) {

$arr = $db->getArray("SELECT * FROM table_$var, View_Member, View_Office WHERE table_$var.FIELD = '$var2' AND table_$var.LIST_AGENT = View_Member.ID AND table_$var.LIST_OFFICE = View_Office.MLS_Number ORDER BY LIST_PRICE ASC");

return $arr;

}

 

When I put a table_ABC instead of table_$var that function works fine.

 

Also, what type of error handling would you suggest?

Well, like I said, if you put some error handling within the function you may be able to narrow it down some.

 

Something like....

 

<?php

function GoGet($db, $var= false, $var2= false) {
  $sql = "SELECT * FROM table_$var, View_Member, View_Office WHERE table_$var.FIELD = '$var2' AND table_$var.LIST_AGENT = View_Member.ID AND table_$var.LIST_OFFICE = View_Office.MLS_Number ORDER BY LIST_PRICE ASC";
  if ($arr = $db->getArray($sql)) {
return $arr;
  } else {
    echo "Query failed $sql";
    return false;
  }

}
?>

Though Im not sure how much we can relie on getArray returning false on error either if this function is anything to go by.

Also, how are you calling and tring to display the results?

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.