PHP MySQL better strategy for returning query results

arjun gulyani

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.

Your Common Sense

There is no "separate process" to echo the results, and it's perfectly OK to use your current way to output from PHP.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PHP with MySQL query not returning expected results

Why is my PHP code returning incorrect results from a MySQL query?

PHP STMT MySQL query returning LIKE results when set to equals

MySQL - Query not returning proper results

MySQL Query not returning any results

Mysql Query not returning my results

MySQL Query is returning additional results

PHP MySQL search not returning results

Mysql query returning too many results

MySQL cumulative sum query not returning expected results

Mysql select query "order by" not returning results as expected

MySQL query with LEFT JOIN not returning empty results

MySQL Query Not Returning Results With Dual Params

MySql Select query returning erroneous results

MySQL query returning more results than it should

MySQL query not returning values in PHP

PHP MySQL query variables not returning

Why is this PHP/PDO sqlsrv query not returning results?

php sql query not returning correct results

Is it better to handle this Query on MySQL side or PHP side?

Exact query in mysql.connector and regular MySQL not returning same results

Query only returning one row (PHP / MYSQL)

PHP MySQL Query returning data but unable to display

MySQL query is returning null in PHP but is fine in terminal

pymongo query not returning results

Executing query through pymysql not returning the same results as running in MySQL

MYSQL JSON select query returning different results each time it is run

mySQL query returning 0 results using UNIX_TIMESTAMP

Nodejs mysql query returning strange results instead of records