PDA

View Full Version : db query @ postbit_display_start


PntSingularity
08-03-2008, 07:25 PM
I'm trying to run db queries at the hook postbit_display_start, but for some reason it doesn't seem to work...

When I try this line:
$foobar = $db->query_read("SELECT username FROM " . TABLE_PREFIX . "user WHERE userid = 1");
...the code fails and showthread.php returns an empty page.

Ideas?


EDIT:
Nevermind, it seems it works when I use the "showthread_postbit_create" hook instead.

Now I need to figure out why I get "Resource id #x" instead of the data I'm looking for... *sigh*

MoT3rror
08-03-2008, 08:42 PM
Change your function to query_first then query_read.

Opserty
08-03-2008, 09:35 PM
In the postbit_* hooks you will most likely need to use the $this->registry->db variable or the $vbulletin->db variable as those hooks are executed within a class, if I remember correctly.

PntSingularity
08-05-2008, 12:00 AM
In the postbit_* hooks you will most likely need to use the $this->registry->db variable or the $vbulletin->db variable as those hooks are executed within a class, if I remember correctly.

Thanks. But like I said I switched onto another hook that worked better. ;) Thanks for the info though, might need to use those hooks some other time. :)

Change your function to query_first then query_read.
Umm... why? Unless I'm mistaken query_first reads only the first line whereas query_read reads ALL lines (including the first).


Anyways, I solved the resource id thingy with fetch_array.

MoT3rror
08-05-2008, 03:43 AM
Well query_read only does like mysql_query but query_first does mysql_query and fetch_array both and returns the array. Plus you are only pulling out one row with that query.

PntSingularity
08-05-2008, 04:07 AM
Well query_read only does like mysql_query but query_first does mysql_query and fetch_array both and returns the array. Plus you are only pulling out one row with that query.

Ah ok, now I understand. Thing is that I wanted all rows, I just hadn't figured out how to do that yet. I'm basically learning php while writing a plugin. :rolleyes:

Marco van Herwaarden
08-05-2008, 07:23 AM
To get more then 1 row, you will need to do your query_read() followed by a fetch_array() in a loop.

PntSingularity
08-05-2008, 07:41 AM
To get more then 1 row, you will need to do your query_read() followed by a fetch_array() in a loop.

Thanks, but like I mentioned...
Anyways, I solved the resource id thingy with fetch_array.

;)

sarahk
08-20-2008, 01:05 AM
Anyways, I solved the resource id thingy with fetch_array.I'm stuck on the same thing (so I appreciate the thread). PntSingularity I'm getting none of the post information when I change to the hook you use! How did you access the post data?

turns out the syntax I was after in the original hook was
$stories = $this->registry->db->query_first

PntSingularity
08-20-2008, 04:21 AM
I'm stuck on the same thing (so I appreciate the thread). PntSingularity I'm getting none of the post information when I change to the hook you use! How did you access the post data?

turns out the syntax I was after in the original hook was
$stories = $this->registry->db->query_first

I don't think I ever tried a query_first there. However, I've used the variable Opserty mentioned there with query_read.
$foo = $this->registry->db->query_read('SQL STRING');
while ($bar = $this->registry->db->fetch_array($foo)) {
/* Do stuff.. */
}
$this->registry->db->free_result($foo);

If you're using showthread_postbit_create you should simply use $db->query (or query_first, query_read or query_write).

If you're not already doing that, you might want to ssh to the server (if you have that possibility) and tail the error log to receive real-time error reporting.
In a command shell (cmd/terminal):
ssh example.com -l root
Replace example.com with your domain and enter your root password.
tail -f /var/log/httpd/error_log
Of course, your log may be in a slightly different location. I'm running apache on Fedora 9 myself.