need help understanding $smcFunc

Started by sathirtythree, February 14, 2014, 08:15:32 PM

Previous topic - Next topic

sathirtythree

why doesn't this work?
       $all_parks = array();
$all_parks = $smcFunc['db_query']('' , '
SELECT *
FROM {db_prefix}parks'
);
print_r($all_parks);


$all_parks is empty

margarett

I'm not sure if some docu is available abouu this

But, like MySQLsyntax, you need a while fetch row loop to fill your array.

Best advice is, look for examples in SMF code ;)
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Arantor

There's no discrete docs on it but let me explain.

The query function doesn't return a set of results. Partly because there are multiple ways you might want the data and partly for performance; if that query were to return a million rows or more (and on some of the DBs I work with, I do have queries that return millions - or more - rows at a time), since then you'd have to cope with all that directly in PHP and PHP would likely choke. (Don't even get into buffered vs unbuffered queries. SMF 2.0 does strictly buffered queries so you can pull rows whenever and close when you're done without having to explicitly clear the queue.)

The simplest example would be:
$all_parks = array();
$request = $smcFunc['db_query']('' , '
SELECT *
FROM {db_prefix}parks'
);
while ($row = $smcFunc['db_fetch_row']($request))
$all_parks[] = $row;
$smcFunc['db_free_result']($request);
print_r($all_parks);


However... I  have no idea what the structure of that table is, which is not particularly useful at this point in time. Using SELECT * is almost never a good idea, and without seeing what the structure of the table is, I can't tell you if that's the case or not. Using a SELECT without an ORDER clause can also have interesting consequences.

Ideally, more information please.

sathirtythree

#3
i did have some more specific stuff in there, but i only have one entry in the table right now so i weened it down to the simplest query i could muster, just to see if i could get it working. I'll look into the directions you've pointed me in and see if i can understand it a little more and come back. thanks.

sathirtythree

As an update, I've been tearing along. I've written about a dozen queries at this point and they've all gone very well thanks to your help. I've had a few stumbles with INNER JOINS, but i found a few examples in Display.php that helped me sort it out.

Advertisement: