0

I have a basic query in Joomla! and I really, really can't figure out why it doesn't return anything:

$database =& JFactory::getDBO();
$query = "SELECT * FROM my_table";
$database->setQuery($query);
$result = $database->loadObjectList();
var_dump($result);
die();

Query is very, very basic, I know. It returns $result as null. Thing is, I run this query in a separate .php scrip file (localhost/myscript.php). All other queries in the rest of my website seem to run just fine (including some in other script files like this one).

I've run this query in a terminal and returns what I want. Please, I need some idea :)

kratzz
  • 1
  • did you include the right classes files ? – HamZa Jun 01 '12 at 13:14
  • add error checkers at various steps... echo the query then try to print result so that its easy to code debug. Try this also--> echo "Database prefix is : " . $database->getPrefix(); so that you can confirm that your first line is also working – swapnesh Jun 01 '12 at 14:33

2 Answers2

1

If your trying this in a separate php file (localhost/myscipt.php) as you say, you need the proper classes. See this post. The last answer has some details.

However this is not recommended. You should be using module or plugin development all within the framework.

Alternatively, you could use Jumi which allows you to write any code you want and include it as part of a module. Makes life a lot easier.

Community
  • 1
  • 1
Tom
  • 2,604
  • 11
  • 57
  • 96
0

I think your query is missing a table prefix, you can echo the prefix using $database->getPrefix(); also try changing

$query = "SELECT * FROM my_table";

to

$query = "SELECT * FROM `#__my_table`";

NOTE: Joomla uses a placeholder for the prefix, the “#__” will be replaced with the correct prefix.

Aung Myo Linn
  • 2,820
  • 3
  • 27
  • 38