Writing table from SQL-command

Permalink
Hi!

Sorry for my 'noob' question - i have done a lot of reading with no result. Thanks for any helpful suggestions!

I need to run a simple 'select * from TABLE' an then write the results into a table.

I have tried to create a simple function (running in a Simple PHP block) to output my sql statement:

function SQLResultTable($Query)
{
      $db = Loader::db( 'localhost', 'askwiki', 'PW', 'askwiki-moodle', true);         
      $db->Execute($Query);
    $Table = "";  //initialize table variable
    $Table.= "<table border='1' style=\"border-collapse: collapse;\">"; //Open HTML Table
    if(mysql_error())
    {
        $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
    }
    else
    {
        //Header Row with Field Names
        $NumFields = mysql_num_fields($Result);
        $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\">";


What am I doing wrong? A simple block with fields like (SQL-Query, DB, User, PW, Server) would be very handy for me - can you point me in the right direction? Many THANKS!

ottigerb
 
Remo replied on at Permalink Reply
Remo
the variable $Result doesn't seem to be initialized. Also, it looks like you're mixing the plain php mysql api with the adodb api.

You might want to check this pagehttp://phplens.com/lens/adodb/docs-adodb.htm... to get a deeper understand of adodb and avoid the mysql_* functions.
Remo replied on at Permalink Best Answer Reply
Remo
You can find an almost complete example on this page:

http://phplens.com/lens/adodb/tute.htm...

include("adodb.inc.php");
 $db = NewADOConnection('mysql');
 $db->Connect("localhost", "root", "password", "mydb");
 $result = $db->Execute("SELECT * FROM employees");
 if ($result === false) die("failed");  
 while (!$result->EOF) {
   for ($i=0, $max=$result->FieldCount(); $i < $max; $i++)
         print $result->fields[$i].' ';
   $result->MoveNext();
   print "<br>\n";
 }


all you have to do is to add the table tags and remove the first two lines as you already get the db object using Loader::db