arjun gulyani
arjun gulyani

Reputation: 711

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.

Upvotes: 0

Views: 57

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 157989

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

Upvotes: 1

Related Questions