dpwjx32578146 2014-03-27 19:16
浏览 58
已采纳

用于SQL查询的PHP脚本

I'm still learning php so please take it easy on me. This might sound a silly question for you guys.

Right so. I have categories lets say Blogs, eCommerce, Portfolios etc. Files according too. Blog.php etc.

I also have functions.php where all my functions are.

MYSQL database is where I store information from them files. Website information, descriptions etc.

I want to have 1 function that queries data from a website just about 1 category so then I can display it in Blogs.php, eCommerce.php etc. My function to query data from mysql looks like this.

This is an example:

function querying_category($category){
    $db = DB::getInstance();
    $all = $db->query('SELECT * FROM website WHERE category = {$category} ORDER BY id DESC');

    if($all->count()){
       foreach($all->results() as $website){
              $web_data[] = $website;
       }
    }
    return $web_data;
}

and then let's say in my blog.php would go like:

$category = 'blog';
$website = querying_category($category);

Could you please tell me what am I doing wrong? I want to declare a variable in my blog.php or ecommerce.php etc without rewriting the following query:

'SELECT * FROM website WHERE category = {$category} ORDER BY id DESC

Can I achieve it with passing in $category as an argument in querying_category() but declaring $category in my blog.php or ecommerce.php?

  • 写回答

1条回答 默认 最新

  • douwen3965 2014-03-28 13:13
    关注

    It seems as if the editing I made to the question fixed the OP's problem.

    There were a few lines of code that were not properly indented along with a few spelling mistakes.

    As per the OP's request, this answer has been given in order to close the question.

    However, this line return $web_data; should have been return $website;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大