使用smarty和mysql数据库?

时间:2014-04-28 14:31:41

标签: php mysql smarty

这是我第一次需要与smarty合作,而且看起来非常简单。

然而,我的PHP代码中存在 twist ,这导致了问题。

这是我想要做的事情:

在你开始打我没有使用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之前我从未习惯过这个错误。

提前致谢。

3 个答案:

答案 0 :(得分:1)

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

$sql .= 'FROM $storeShop ';

PHP单引号蜇' '实际上是引号之间的内容。

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

试试这个:

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

PHP Strings

答案 1 :(得分:0)

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

{$storeShop[$storeShop].id} 

你可以使用:{$storeShop.$another_storeShop.id} 如果$ storeshop就像数组(&#39; storeshop_key&#39; =&gt;数组(&#39; id&#39; - &gt;&#39; id&#39;))

同样$smarty->assign('$storeShop', $value);将创建变量$$ storeShop,这是不正确的

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

答案 2 :(得分:0)

删除

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

来自您的PHP代码