使用终端连接到Mariadb

I use PHP to send a query to the terminal and I want to receive the table that the database produces. I am new to PHP and having difficulties with the connection of the website and the database that I access from the terminal.

    <?php

    $con = new mysqli('localhost','root','','db');
    $query = "";

    if($con->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } else {
        echo "Connection Successful."
    }

    if($_SERVER["REQUEST_METHOD"] == ["POST"]) {
        $query = test_input($_POST["uni_name"]);
    }

    $prompt = 'select * from students where university like ' . $query . '%';


    $dir = shell_exec("use db");
    if($dir == NULL) {
        $output = shell_exec($prompt);
        echo "test";
    }

    setModal($output);

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    function setModal($query) {
        echo '<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="myModalLabel">Graduate Information</h4>
      </div>
      <div class="modal-body">
      '; 
        echo $query;
        echo '
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>';
    }
?>

The setModal function is to create a Bootstrap modal and display the info I get from the database. My main problem is the connection between the database and the website.

Thanks in advance..

dongzhan9100
dongzhan9100 我会检查出来..谢谢!
3 年多之前 回复
doucheng7808
doucheng7808 这是一种执行查询的创造性方法。错了,但很有创意。你应该看看这个基本的mysqli例子。
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问