MySQL - PHP Problem
| Page: 1 |
| From User | Message Body |
|---|---|
| mielie69 |
Post #476 |
|
Member Date: 4:21 pm, Mar 7 2007 Posts: 3 |
Hi, I'm having some problems with getting and displaying information from a mysql table. What i want to do is have it connect to the database, read name, race, class and online from the table character and then output all the names that have a value of 1 in the online column. This is however proving quite difficult for a novice php/mysql programmer. I have attached my two files that im using for the module, i have also setup mysql in the bot.conf file and i am assuming it connects. Any help on this topic will be much appreciated. Cheers mielie69 mangos.php Code <?php class mangos extends module { public $title = "MaNGOS PHP IRC"; public $author = "mielie69"; public $version = "0.01"; public function online($line, $args) { $channel = $line['to']; if ($channel == $this->ircClass->getNick()) { return; } if ($args['nargs'] == 0) { // $OnlineResult = $this->db->query("SELECT name, race, class FROM 'character' WHERE online=1"); $this->ircClass->notice($line['fromNick'], "Players Online:"); $noticetxt = ""; while ($online = $this->db->query("SELECT name, race, class FROM 'character' WHERE online=1")) { $noticetxt .= $online['name'] . ": " . $online['race'] . " " . $online['class']; $this->ircClass->notice($line['fromNick'], $noticetxt); } $this->ircClass->notice($line['fromNick'], "End of results."); } } } ?> mangos.conf Code file mangos modules/mangos/mangos.php priv !online true true true 0 mangos online P.S I have included the file in the function.php file as well |
| Mad_Clog |
Post #477 |
|
Member Date: 8:13 am, Mar 8 2007 Posts: 176 |
I think you got a little confused there... ;) This should work a little better Code if ($args['nargs'] == 0) { // Query the database $OnlineResult = $this->db->query("SELECT name, race, class FROM 'character' WHERE online=1"); $this->ircClass->notice($line['fromNick'], "Players Online:"); // Get the results while ($online = $this->db->fetchArray($OnlineResult)) { // Build and send the reply $noticetxt = $online['name'] . ": " . $online['race'] . " " . $online['class']; $this->ircClass->notice($line['fromNick'], $noticetxt); } $this->ircClass->notice($line['fromNick'], "End of results."); } |
| mielie69 |
Post #478 |
|
Member Date: 1:58 pm, Mar 8 2007 Posts: 3 |
Thanks for replying Mad_Clog, i replaced my code with yours and now when i run the command i get the following error in the console. Any ideas? Code [04:05:14] (php-irc@FireServ) > :mielie69!mielie69@FSI-F6D313CA.wbs.isadsl.co.za PRIVMSG #testbot :!online Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result reso urce in C:\Program Files\xampp\php\irc\databases\mysql.php on line 138 [04:05:14] (php-irc@FireServ) > NOTICE mielie69 :Players Online: |
| Manick |
Post #481 |
|
Admin Date: 5:51 am, Mar 9 2007 Posts: 223 |
you see this line: while ($online = $this->db->fetchArray($OnlineResult)) You should probably check the number of rows returned with $this->db->numRows($OnlineResult);. If that's greater than zero, then you can use the fetchArray etc... --Manick PHP-IRC Developer |
| Mad_Clog |
Post #482 - Reply to (#477) by Mad_Clog |
|
Member Date: 7:48 am, Mar 9 2007 Posts: 176 |
Sounds to me like either the query is failing or you're not properly connected to the database. You could try the following: Code if ($args['nargs'] == 0) { // Query the database $OnlineResult = $this->db->query("SELECT name, race, class FROM 'character' WHERE online=1"); // Check if the query failed if ($OnlineResult === FALSE) { $this->ircClass->notice($line['fromNick'], 'DB ERROR: '.$this->db->getError()); // No users are online } else if ($this->db->numRows($OnlineResult) == 0) { $this->ircClass->notice($line['fromNick'], "No Players Online"); // 1 or more users online } else { $this->ircClass->notice($line['fromNick'], "Players Online:"); // Get the results while ($online = $this->db->fetchArray($OnlineResult)) { // Build and send the reply $noticetxt = $online['name'] . ": " . $online['race'] . " " . $online['class']; $this->ircClass->notice($line['fromNick'], $noticetxt); } $this->ircClass->notice($line['fromNick'], "End of results."); } } |
| mielie69 |
Post #484 |
|
Member Date: 9:47 am, Mar 9 2007 Posts: 3 |
Well all i can say is thank you so much, that error statement helped alot, there was an error in my query, i was just calling <table> instead of <dbase>.<table>, after changing that it works like a charm. Thank you again for all the help, its much appreciated. Cheers |
