我的网站上有以下 PHP 代码,它基本上访问一个从 mysql 表中提取信息的 PHP 脚本;
<?php include('event_connect.php'); ?>
是否可以在此脚本中包含对特定代码部分的引用?
基本上我必须为单个下拉列表创建 39 个不同的脚本,例如在“XXX”下拉列表中,仅显示标识符为“xxx”的记录。
我认为比创建 39 个脚本更容易的是(如果可能的话)只需根据上面的代码在页面中的放置位置添加某种引用值即可。有点像 VBA 如何允许您从另一个函数调用选定的函数...
编辑:这是我用来访问 mysql 数据库的 PHP,添加了 niet 提供的代码 - 非常好用!谢谢。
<?php
//Get te CSS styling
// Make a MySQL Connection
mysql_connect("xxxx", "xxxx", "xxxx", "xxxx") or
die(mysql_error());
mysql_select_db("xxxx") or die(mysql_error());
// Retrieve all the data from the "xxxx" table
switch($section) {
case 1:
$result = mysql_query("SELECT * FROM `Events_List` WHERE `Value` = 'value_1' LIMIT 0 , 30") or die(mysql_error());
break;
case 2:
$result = mysql_query("SELECT * FROM `Events_List` WHERE `Value` = 'value_2' LIMIT 0 , 30") or die(mysql_error());
break;
// store the records of the "xxxx" table into $row array and loop through
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $name = $row['name'];
$Event = $row['Event'];
$Date = $row['Date'];
$Type = $row['Type'];
$Description = $row['Description'];
$Event = htmlspecialchars($row['Event'],ENT_QUOTES);
$Date = htmlspecialchars($row['Date'],ENT_QUOTES);
$Type = htmlspecialchars($row['Type'],ENT_QUOTES);
$Description = htmlspecialchars($row['Description'],ENT_QUOTES);
echo " <div id='ev_con'>
<div id='ev_img'> </div>
<div id='Event'> $Event </div>
<div id='Date'> $Date </div>
<div id='Type'> $Type </div>
<div id='Description'> $Description </div>
</div>";}
?>
这可以很好地运行一个查询,但无法运行多个查询。
最佳答案
也许是这样的?
$section = 1;
include("event_connect.php");
在你的 event_connect 文件中:
switch($section) {
case 1:
// do things
break;
case 2:
// do other things
break;
// ...
}
关于php - 引用php代码部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21477821/