Categories > TinyButStrong general >

ezsql inside tbs functions

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Michaelsen
Date: 2011-05-27
Time: 19:00

ezsql inside tbs functions

Hello there,

I noticed since upgrading to tbs version 3+ for some time now, the ezsql codes inside tbs functions did not work anymore for me.

Where before I had something like:

//How many entries for this category
function catCount($BlockName,&$CurrRec,$RecNum) {

    global $db;

    return $db->get_var("SELECT COUNT(cat_id) FROM items_cats WHERE cat_id = ". $CurrRec ."");

    }
Now I have something like this:
//How many entries for this category
function catCount($BlockName,&$CurrRec,$RecNum) {

    global $db;

        $result = mysql_query("SELECT * FROM items_cats WHERE cat_id = ". $CurrRec ."");

    $CurrRec = mysql_num_rows($result);

    return $CurrRec;

    }

When using the ezsql syntax nothing returns on tbs functions.

The same happens for $db->get_row and $db->get_results

However I don't remember anybody complaining about this here in the forum, it is so strange it would be only with me, since I upgraded tbs and ezsql from scratch more than once in recent months.
By: Skrol29
Date: 2011-05-27
Time: 22:49

Re: ezsql inside tbs functions

Hi,
This is strange because ezSQL has nothing to do with TBS.
By the way there is something strange with your function. In the first version it has syntax of an "ondata" function (parameter $CurrRec) while it seems the be an "onformat" function because $CurrRec is used like it is a value instead of record. And its new value is a value (given by $db->get_var(...))
In the second version the new value becomes a record (given by mysql_num_rows()).

Both "ondata" and "onformat" functions should not return any value, they should only modify the arguments given by reference.
By: Michaelsen
Date: 2011-05-28
Time: 00:22

Re: ezsql inside tbs functions

Hello again,

I switched for the correct mode, this is working fine:

//How many entries for this category
function catCount($FieldName,&$CurrVal) {

    global $db;

    $result = mysql_query("SELECT cat_id FROM items_cat WHERE cat_id = ". $CurrVal ."");

    $CurrVal = mysql_num_rows($result);

    }

Now, if I do the following:

//How many entries for this category
function catCount($FieldName,&$CurrVal) {

    global $db;

    $CurrVal = $db->get_var("SELECT COUNT(cat_id) FROM items_cat WHERE cat_id = ". $CurrVal ."");

    }

The strangest thing happens, it returns only one row in html side, [cats.cat_id;block=div] will display only the first item of what should be many, the function will return the correct value for the solitary div, if I change back to traditional php query, it will display all items on html side.
By: TomH
Date: 2011-06-06
Time: 15:59

Re: ezsql inside tbs functions

Not sure I'm getting your problem, but the ezsql db->get_var function will only return one row in any case

Try $CurrVal = $db->get_results("..."); to see what happens

By: Michaelsen
Date: 2011-06-06
Time: 21:30

Re: ezsql inside tbs functions

Hello TomH,

This is what is happening, I did a tbs example from scratch, so I also can be sure nonthing I implemented was causing the problem.


//PHP SIDE

include "ezsql.core.php";//2.03
include "ezsql.mysql.php";
include "tbs.php";//3.7.0 for PHP 5

$dbuser = "admin"; $dbpassword = "******"; $dbname = "mydb"; $dbhost = "localhost";

$db = new ezSQL_mysql($dbuser,$dbpassword,$dbname,$dbhost);

//ezsql tbs function
function tbsdb_ezsql_open(&$db,&$query) {
    $db->get_results($query,ARRAY_A) ;
  return $db ;
}
function tbsdb_ezsql_fetch(&$db,$num) {
    if ($num<=$db->num_rows) {
      return $db->get_row(null,ARRAY_A,$num-1) ;
    } else {
        return False ;
    }
}
function tbsdb_ezsql_close(&$db) {
    // not needed
}

//How many categories inside this category
function catCount($FieldName,&$CurrVal) {

global $db;

$result = mysql_query("SELECT pid FROM cats WHERE pid = ". $CurrVal ."");

$CurrVal = mysql_num_rows($result);

}

$TBS = new clsTinyButStrong;

$TBS->LoadTemplate("tbs-cats.html");

$tbsquery = "SELECT * FROM cats";
$TBS->MergeBlock("cats",$db,$tbsquery);

$TBS->Show();


//HTML SIDE

<div>
[cats.cat_title;block=div] ([cats.cat_id;onformat=catCount])
</div>



//OUTPUT

Cat 1 (6)
Cat 2 (3)
Cat 3 (15)
Cat 4 (12)
Cat 5 (7)

If I change the catCount function as below:

//How many categories inside this category
function catCount($FieldName,&$CurrVal) {

global $db;

$CurrVal = $db->get_var("SELECT COUNT(pid) FROM cats WHERE pid = ". $CurrVal ."");

}

Then the output shows like the following:


//OUTPUT

Cat 1 (6)

If I use $db->get_row or $db->get_results it happens the same thing.

I guess TBS 3.7 has ezsql as native, but it asks me for the tbsdb_ezsql_open, tbsdb_ezsql_fetch and tbsdb_ezsql_close if I dont have it in the code.
By: TomH
Date: 2011-06-09
Time: 15:34

Re: ezsql inside tbs functions

I can only suggest something to evaluate the problem... do plain queries (outside of your program logic) to see what are the actual returned query results -- you may not be getting the results you are expecting.

TBS does NOT have ezSQL native. Something is not correct if you are getting messages asking for ezSQL functions - maybe the versions (ezSQL and Skrol29's adapter for ezSQL)

Oops!!!  I see that you have not included Skrol's plugin for using ezSQL (see tbsdb_ezsql.zip under TBS Database Plugins page) - that maybe part of the problem!

I have used TBS with ezsql  for many years - until Skrol 29 made the recent major revisions to TbsSQL.  Now I have converted completely over to TbsSQL because I have all of the ease of ezSQL features plus more -and- it is maintained by SKrol29 (Justin has essentially dropped development of ezSQL for some time now). Skrol29 has made it very easy to migrate from ezSQL to TbsSQL.

Now the biggest reason for using TbsSQL is the debugging console - I can see every query, the actual results returned, whether it is from cache or not,and any MySQL error statements -- and all in a separate popup window that keeps the debugging stuff completely away from the php/html page output.

TomH
By: Michaelsen
Date: 2011-06-09
Time: 20:14

Re: ezsql inside tbs functions

Ok, I really appreciate your reply and I'm looking forward to TbsSQL, unfortunately up to this date I tought TbsSQL wasn't an ezsql replacement, but mainly kinda of a TBS connector, looking at http://www.tinybutstrong.com/tbssql.php I can see how wrong I was.

Also it is very comfortable to know we have a modern ezsql maintained by someone like Mr. Skrol29.

Thank you!

- Michaelsen