Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

creating a smarty array from SQL

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
rshandel
Smarty Rookie


Joined: 29 Mar 2009
Posts: 16

PostPosted: Sat May 16, 2009 1:22 am    Post subject: creating a smarty array from SQL Reply with quote

Hi,

a newbie to SQL and php, I trying to create a smarty array by pulling data from a SQL database.

I have a table in SQL I created called breed. It has 4 fields, the first field is "breed_id" which I created the primary, second field is call "breed".
In my PHP file I did the following:

$breeds[] = "$sql_tbl[breeds].*";
$smarty->assign("dog_breeds", $breeds);

in my tpl

I tried to call out a record from the array:

{$dog_breeds[breed].1}

But it didn't produce anything.

Any help would be greatly appreciated.

Thanks.
Back to top
View user's profile Send private message
alokin
Smarty Regular


Joined: 14 Apr 2009
Posts: 96
Location: Belgrade, Serbia

PostPosted: Sat May 16, 2009 10:09 am    Post subject: Re: creating a smarty array from SQL Reply with quote

rshandel wrote:

"$sql_tbl[breeds].*";

Are you sure?

How did you retrieve data from your database? Open connection, send query, fetch results and put them in array...

Check out first example on this page, just to get you going...
_________________
www.nikolaposa.in.rs
Back to top
View user's profile Send private message
rshandel
Smarty Rookie


Joined: 29 Mar 2009
Posts: 16

PostPosted: Sat May 16, 2009 8:32 pm    Post subject: Reply with quote

I'm calling this within a php program that already is calling other sql tables:

example from file:

$fields[] = "$sql_tbl[products].*";
$from_tbls[] = "pricing";
$left_joins['quick_flags'] = array(
"on" => "$sql_tbl[quick_flags].productid = $sql_tbl[products].productid"
);
$fields[] = "$sql_tbl[quick_flags].*";



What example where you referring to on the first page?
Back to top
View user's profile Send private message
rshandel
Smarty Rookie


Joined: 29 Mar 2009
Posts: 16

PostPosted: Mon May 18, 2009 2:36 am    Post subject: Reply with quote

a little more info:

I'm using X-cart which has macro commands to connect to the database:
Code:

function func_query($query) {
   $result = false;

   if ($p_result = db_query($query)) {
      while ($arr = db_fetch_array($p_result))
         $result[] = $arr;
      db_free_result($p_result);
   }

   return $result;
}


function db_query($query) {
   global $debug_mode;
   global $mysql_autorepair, $sql_max_allowed_packet;

   if (defined("START_TIME")) {
      global $__sql_time;
      $t = func_microtime();
   }

   if ($sql_max_allowed_packet && strlen($query) > $sql_max_allowed_packet) {

      # Check max. allowed packet size
      global $current_location, $REMOTE_ADDR, $login;
      $len = strlen($query);
      $query = substr($query, 0, 1024)."...";
      $mysql_error = "10001 : The size of the data package being transmitted is greater than maximum allowed by the server";
      $msg  = "Site                : ".$current_location."\n";
      $msg .= "Remote IP           : $REMOTE_ADDR\n";
      $msg .= "Logged as           : $login\n";
      $msg .= "Query length        : $len\n";
      $msg .= "Max. allowed packet : $sql_max_allowed_packet\n";
      $msg .= "SQL query           : $query\n";
      $msg .= "Error code          : 10001\n";
      $msg .= "Description         : The size of the data package being transmitted is greater than maximum allowed by the server";

      db_error_generic($query, $mysql_error, $msg);

      return false;
   }

   __add_mark();
   $result = mysql_query($query);

   if (!$result && preg_match("/Lost connection|server has gone away/", mysql_error())) {
      db_connection();
      $result = mysql_query($query);
   }

   $t_end = func_microtime();
   if (defined("START_TIME")) {
      $__sql_time += func_microtime()-$t;
   }

   #
   # Auto repair
   #
   if (!$result && $mysql_autorepair && preg_match("/'(\S+)\.(MYI|MYD)/",mysql_error(), $m)) {
      $stm = "REPAIR TABLE $m[1] EXTENDED";
      error_log("Repairing table $m[1]", 0);
      if ($debug_mode == 1 || $debug_mode == 3) {
         $mysql_error = mysql_errno()." : ".mysql_error();
         echo "<b><font color=\"darkred\">Repairing table $m[1]...</font></b>$mysql_error<br />";
         flush();
      }

      $result = mysql_query($stm);
      if (!$result)
         error_log("Repaire table $m[1] is failed: ".mysql_errno()." : ".mysql_error(), 0);
      else
         $result = mysql_query($query); # try repeat query...
   }

   if (db_error($result, $query) && $debug_mode==1)
      exit;

   $explain = array();
   if (
      defined("BENCH") && constant("BENCH") &&
      !defined("BENCH_BLOCK") && !defined("BENCH_DISPLAY") &&
      defined("BENCH_DISPLAY_TYPE") && constant("BENCH_DISPLAY_TYPE") == "A" &&
      !strncasecmp("SELECT", $query, 6)
   ) {
      $r = mysql_query('EXPLAIN '.$query);
      if ($r !== false) {
         while ($arr = db_fetch_array($r))
            $explain[] = $arr;

         db_free_result($r);
      }
   }

   __add_mark(array("query" => $query, "explain" => $explain), "SQL");

   return $result;
}


function db_fetch_array($result, $flag=MYSQL_ASSOC) {
   return mysql_fetch_array($result, $flag);
}

function db_fetch_field($result, $num = 0) {
   return mysql_fetch_field($result, $num);
}

function db_free_result($result) {
   @mysql_free_result($result);
}



So, I call the function to create an array from an existing table called products.

Code:

$my_array[] = func_query("SELECT * FROM $sql_tbl[products]");


and it produces the array no problem.

Now if I use the same query but try to select from my new table:
Code:

$my_array[] = func_query("SELECT * FROM $sql_tbl[breeds]");

I get the following error:

INVALID SQL: 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
SQL QUERY FAILURE:SELECT * FROM


I use a debugger feature in x-cart:

x_load('debug');
func_print_r($my_array);

and it echos the array when I call existing xcart tables, but produces an empty array with my new "breeds" :

Debug [0/0]: Array
(
[0] =>
)

[/code]

I hoping this info may help someone figure out what may be causing my problems.

Thanks,

Richard
Back to top
View user's profile Send private message
rshandel
Smarty Rookie


Joined: 29 Mar 2009
Posts: 16

PostPosted: Mon May 18, 2009 11:49 am    Post subject: Reply with quote

I found the problem.

Silly oversite - X-cart has an init.php which creates aliases for each table.....
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP