在php和mysql上使用查询功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在php和mysql上使用查询功能相关的知识,希望对你有一定的参考价值。

我想对函数使用查询。我想多次使用同一功能,如何根据使用中的$type变量调用此功能。

这是我尝试过的,我知道它还不完整,但是我不知道我必须做些什么才能工作。

function GeneralQuery($connmysqli){

  // Query mounting according condition of $type
if(empty($type)){
  if($type = "")
  {
    if($type == "school")
    {
      $type = 'GROUP BY itemid, lastname
      ORDER BY u.department';
    }
    else if($type == "class")
    {
      $type = 'GROUP BY class
      ORDER BY class';
    }
    else
    {
      $type == "student";
      $type = 'GROUP BY class, u.id
      ORDER BY class, avg DESC';
    }
  }
}


   $sql = "SELECT id, user, itemid, lastname, department, class from table " . $type . "";

   $res = mysqli_query($connMysqli,$sql)or die(mysqli_connect_errno());
   $list = array();
   while($item = mysqli_fetch_assoc($res)){
      $list[] = $item;
   }

  return $list;
  }

// I don't know what to do here to call query result for School -  
$data = GeneralQuery($connMysqli);
foreach($data as $item){
   echo $item['avg'] . ' - ' . $item['School'] . '<br>';
}

// I don't know what to do here to call query result for Class
$data = GeneralQuery($connMysqli);
foreach($data as $item){
   echo $item['avg'] . ' - ' . $item['class'] . '<br>';
}

// I don't know what to do here to call query result for Student
$data = GeneralQuery($connMysqli);
foreach($data as $item){
   echo $item['avg'] . ' - ' . $item['Student'] . '<br>';
}

// close connection
mysqli_close($connMysqli);
答案

如何使用switch structure

<?php

function GeneralQuery($connMysqli, string $type = null)
{

    switch($type) {
        case "school": {
            $sqlQuery = 'GROUP BY itemid, lastname
            ORDER BY u.department';

            break;
        }

        case "class": {
            $sqlQuery = 'GROUP BY class
            ORDER BY class';

            break;
        }

        case "student": {
            $sqlQuery = 'GROUP BY class, u.id
            ORDER BY class, avg DESC';

            break;
        }

        default : {

            //your default case when
            //none of above mathes
            //for exampe no $type was passed
            //to the function
            $sqlQuery = 'GROUP BY class, u.id
            ORDER BY class, avg DESC';

            break;
        }

    }

    $sql = "SELECT id, user, itemid, lastname, department, class " . $sqlQuery . "";

    $res = mysqli_query($connMysqli,$sql)or die(mysqli_connect_errno());
    $list = array();
    while($item = mysqli_fetch_assoc($res)){
        $list[] = $item;
    }

    return $list;
}

// I don't know what to do here to call query result for School -  
$type = 'school';
$data = GeneralQuery($connMysqli, $type);
foreach($data as $item){
 echo $item['avg'] . ' - ' . $item['School'] . '<br>';
}

// I don't know what to do here to call query result for Class
$type = 'class';
$data = GeneralQuery($connMysqli, $type);
foreach($data as $item){
 echo $item['avg'] . ' - ' . $item['class'] . '<br>';
}

// I don't know what to do here to call query result for Student
$type = 'student';
$data = GeneralQuery($connMysqli, $type);
foreach($data as $item){
 echo $item['avg'] . ' - ' . $item['Student'] . '<br>';
}

// close connection
mysqli_close($connMysqli);

注意,必须阅读有关SQL Injection的信息

以上是关于在php和mysql上使用查询功能的主要内容,如果未能解决你的问题,请参考以下文章

运行/调试你的PHP代码

在php和mysql上使用查询功能

php 一个短代码片段准备在WooCommerce Thank You页面上输出货件跟踪UI。

使用 NodeJS 和 JSDOM/jQuery 从代码片段构建 PHP 页面

在 PHP 上查询 MySQL [关闭]

PHP 代码片段