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 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏