简体   繁体   中英

PHP MySQL better strategy for returning query results

I have just dived into the world of web development and started with basics of PHP and MySQL. My assignment included a question where we had to send a request to the server (I was using XAMPP) which would run a MySQL query with the database and return the results. While I did the question and echo 'ed the results of a select query from the PHP on the client page when there was just one user (that is me), I wondered whether it was the right way to do it when there are multiple users using the application.

Is the server here doing extra processing viz the echo statements? Would returning the query results (an object in this case) to the client and let it render the HTML using AJAX, be a better option? If yes, then why? If no, then what could be a better way to implement this?

Apologies if it's too basic.

<?php
$conn = mysqli_connect('localhost','root','','employee','3307');
$text = $_GET['userInput'];
$sql = "";
switch($_GET['emp'])
{
case "EID": 
            $sql .= "Select * from employee where 'Employee ID' = '$text';";
            break;

case "ENAME": 
            $sql .= "Select * from employee where Name = '$text';";
            break;

case "DNO": 
            $sql .= "Select * from employee where 'Department No.' = '$text';";
            break;

case "LOC": 
            $sql .= "Select * from employee where 'Location' = '$text';";
            break;
}

$result_set = mysqli_query($conn,$sql);
$str = "";
if(mysqli_num_rows($result_set)>0)
{
    while($row = mysqli_fetch_assoc($result_set))
    {
        echo ($row['Employee ID'].$row['Name'].$row['Department No.'].$row['Salary'].$row['Location']."<br>");
        //$rows[] = $r;
    }
}
?>

Pardon the non standard code.

没有“单独的过程”来回显结果,并且使用您当前的方式从PHP输出是完全可以的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM