drus39136 2011-08-08 18:48
浏览 83
已采纳

将所有SQL查询放在一个PHP文件中

I am developing a PHP/Mysql site.

The site consists of static html templates. Data is dynamically populated into these templates with php require_once

For example this is simplified version of the Home page

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Home</title>
</head>
<body>
<?php require_once $_SERVER['DOCUMENT_ROOT']."/includes/header.php"; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT']."/includes/connection.php"; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT']."/includes/sql.php"; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT']."/includes/content.php"; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT']."/includes/footer.php"; ?>
</body

The Header and Footer are themselves static so are included as is. However to display the right content I establish a connection(connection.php) to the database, query it(sql.php) and then echo it(content.php).

I repeat this for all other pages on the site each page referencing a modified sql.php.

All this works, But I know it's not very efficient.

My question is how can I restructure my files/code so that hopefully I will end-up with one file that contains all my sql queries and somehow how "choose" the right query to execute depending on which page that requested it.

Thanks for your help.

  • 写回答

2条回答 默认 最新

  • dongyi4170 2011-08-08 18:56
    关注

    Use functions in your files. So you can easely call functions from differend files like

    $something->this_query();
    

    http://php.net/manual/en/language.functions.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100