使用smarty和mysql数据库


working with smarty and mysql database?

这是我第一次需要使用smarty,而且看起来很直接。

然而,在我的PHP代码中存在扭曲,这导致了一个问题。

我想做的是:

在你开始指责我没有使用mysqli函数等之前,请注意,这段代码只是对我的一个简单测试,让我先了解smarty。所以我不会在我的项目中使用mysql,我不建议任何人这样做…

不管怎样,这就是我要做的:

我在index.php页面中使用以下代码:

<?php
header("Content-type: text/html; charset=utf-8");
function isSubdomain()
{
    $host = $_SERVER['HTTP_HOST'];
    $host = explode('.',$host);
    $host = (is_array($host)?$host[0]:$host);
    return $host;
}
?>
<?php
// These are the smarty files
require 'libs/Smarty.class.php';
// This is a file which abstracts the DB connecting functionality (Check out PEAR)
include "config/connect_to_mysql.php";
$smarty = new Smarty;
$smarty->compile_check = true;
$smarty->debugging = false;
$smarty->use_sub_dirs = false;
$smarty->caching = true;
// This SQL statement will get the 5 most recently added new items from the database
$storeShop = isSubdomain();
echo $storeShop;
$sql = 'SELECT * ';
$sql .= 'FROM $storeShop ';
$sql .= 'ORDER BY `id` ';
$result = mysql_query($sql) or die("Query failed : " . mysql_error());
// For each result that we got from the Database
while ($line = mysql_fetch_assoc($result))
{
 $value[] = $line;
}
// Assign this array to smarty...
$smarty->assign('storeShop', $value);

// Assign this array to smarty...
$smarty->assign('$storeShop', $value);
// Display the news page through the news template
$smarty->display('index.tpl');
// Thanks to David C James for a code improvement :)
?>

这就是index.tpl文件:

<!-- This is the DOC type declaration and links in the CSS stylesheet etc -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
    <meta name="author" content="Steve Rendell" />
    <meta name="generator" content="EditPlus" />
    <link rel="stylesheet" type="text/css" href="style.css" title="Default CSS Sheet" />
    <title>News Page</title>
  </head>
  <body id="top">
    <!-- OK display the page header to keep it nice-->
    <div id="header">
      <span>Steve's News Page</span>
    </div>
    <!-- This is where the news article will be going -->
    <div id="bodyText">
        <!-- Have a title -->
        <h1 id="welcome">Read All About It</h1>
        <!-- OK this is a section which will loop round for every item in $news (passed in through from PHP) -->
{section name=storeShop loop=$storeShop}
   <!-- For every item, display the Title -->
  <h2 id="{$storeShop[$storeShop].id}">{$storeShop[storeShop].product_name}</h2>
  <!-- Write out the Author information and the date -->
  <h3>{$storeShop[storeShop].price}, {$storeShop[storeShop].details}</h3>
  <!-- Now show the news article -->
  {$storeShop[storeShop].details}
{/section}
    </div>
    <!-- Show copyright information etc -->
    <div id="footer">All Contents Copy Written :)</div>
  <!-- Close the html tags -->
  </body>
</html>

当我在浏览器中运行index.php时,我会得到以下错误:

Query failed : Table 'mrshoppc_mainly.$storeShop' doesn't exist

但当我使用以下代码时,我得到了正确的输出,即name of the subdomainname of the table in mysql database

$storeShop = isSubdomain();
echo $storeShop;

我知道桌子是存在的。附言:表名$storeShop是动态的,所以它可以是用户选择的任何名称,它将在mysql数据库中创建。

我希望我解释得足够好,有人能帮我。

有人能告诉我为什么会出现上述错误以及如何解决吗?

我怀疑这是由smarty引起的,因为在我开始使用smarty之前,我从未遇到过这个错误。

提前谢谢。

您没有解析包含PHP变量的字符串。

$sql .= 'FROM $storeShop ';

对PHP来说,单引号的刺痛' '实际上就是引号之间的刺痛。

" "双引号字符串将由PHP进行解释。

试试这个:

$sql .= "FROM $storeShop ";  // OR
$sql .= 'From '. $storeShop .' ';

PHP字符串

自从我使用smarty以来已经有很长时间了,但据我所知,没有这样的想法:

{$storeShop[$storeShop].id} 

但是,您可以使用:{$storeShop.$another_storeShop.id}如果$storeshop类似数组('storeshop_key'=>数组('id'->'id'))

此外,$smarty->assign('$storeShop', $value);将创建变量$$storeShop,该变量不是正确的

提示:在发送到smarty var_dump($value)之前,先在php中打印数组,然后在smarty中使用{$storeShop|@print_r}来确保一切正常

只需删除

$smarty->assign('$storeShop', $value);

从您的PHP代码