从定义为变量的表名称中选择名称定义为变量的列

时间:2020-05-19 08:07:36

标签: php mysql database syntax

我正在尝试从名为Y1S1的单个列中提取所有值,该表名为Information Technology,而select查询中的列名是从用户那里选择的标题为$ semester的变量。表名称称为$ course。但是它返回了这个Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\Registration\Courses.php on line 26。 这是我的PHP代码。我已经回显了变量,它们是列和表的确切名称。任何帮助都将不胜感激。

<?php
$course=$semester="";
$course_err=$semester_err="";
$link = mysqli_connect("localhost","root","","courses");
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
if($_SERVER["REQUEST_METHOD"] == "POST"){
      if(empty(trim($_POST["name"]))){
         $course_err= "course  is not set";
      } else {
      $course=($_POST["name"]);
       echo "$course<br>";
      }
     if(empty(trim($_POST["email"]))){
      $semester_err= "semester is not set";
     } else {
      $semester=($_POST["email"]);
     echo $semester;
     }
}

if(empty($course_err || $semester_err)){
$sql="SELECT ($semester) from $course";
$retval=mysqli_query($link,$sql);
       if(mysqli_num_rows($retval)>0){
          while ($row= mysqli_fetch_assoc($retval)){                          
               echo "<li class='list-group-item' style='color:#333300;font- 
              weight:bolder;'>{$row['units']}</li><br>";
       }
       }else{
     echo "<li class='list-group-item'>0 units registered</li>";
       }
}
?>

0 个答案:

没有答案