1

I have My code Below. I have a function "getdet" that return array of record,but i cant able to print the all records the codes displaying only the first records may time. Pls help to solve this issue.

<?php

function getdet($qry){
global $con;
$Selecting=mysqli_query($con,$qry);
 $Fetch=mysqli_fetch_array($Selecting);
 return $Fetch;
 }
$qry="SELECT * FROM `tbs_employee` WHERE EmpId='1' limit 1";
$GetData=getdet($qry);
$i=0;
while($GetData){
echo $i."-".$GetData['EmpId'].":".$GetData['EmpName']."<br>";
$i++;
}
?>

Below is my Table

enter image description here

Here is my result

0-1:Fst Employee
1-1:Fst Employee
2-1:Fst Employee
3-1:Fst Employee
4-1:Fst Employee
5-1:Fst Employee
6-1:Fst Employee
.
.
.
infinity

4 Answers 4

1

You are running the same query ($Selecting=mysqli_query($con,$qry);) over and over again inside your getDet-Function, so it will never quit the while-loop:

while($GetData){

Should be:

$qry="SELECT * FROM `tbs_employee` WHERE EmpId='1' limit 1";
$Selecting=mysqli_query($con,$qry);
$i=0;
while($GetData = mysqli_fetch_array($Selecting)){
  echo $i."-".$GetData['EmpId'].":".$GetData['EmpName']."<br>";
  $i++;
}

mysqli_fetch_array will return null, if all results are returned, then the while-loop can exit.

Sign up to request clarification or add additional context in comments.

5 Comments

He needs to get all records, there's problem in sql-query
Sorry, and in the loop too)
@OlegNurutdinov he's not getting all results, because he always runs the query again, after the first row is processed, leading to an infinite amount of "first rows" for the final result. (Despite his query is limiting the result to one row exactly)
@dognose thanks for your answer. but I don't want to repeat the mysql_query(),mysqli_fetch_array() functions, I need a function to pass a query and return the result as an array. I just want to reduce my lines of code
@BrindhaBaskaran Then you can do the while (mysqli_fetch_array())- loop inside the method. The returned result will then be an array of all result rows. But since this would result in 2 iterations over the same array (one time building it, one time reading it ) I wouldn't recommend it.
0

Well, your function only return a data. Here's the right way:

<?php
function getdet($qry){
global $con;
$Selecting=mysqli_query($con,$qry);
  while($row=mysqli_fetch_array($Selecting, MYSQLI_ASSOC)){
    $Fetch[] = $row;
  }
 return $Fetch;
 }
$qry="SELECT * FROM `tbs_employee`";
$GetData=getdet($qry);
foreach($GetData as $i=>$row){
  echo $i."-".$row['EmpId'].":".$row['EmpName']."<br/>";
}
?>

Comments

0

In your query you filtered employers by the ID WHERE EmpId='1' limit 1

If you wants to get all records, delete this statement.

5 Comments

Changed to $qry="SELECT * FROM tbs_employee WHERE EmpId='1' "; but same result
delete WHERE EmpId='1' statement as I advice you in answer
you query sholud be "SELECT * FROM tbs_employee"
tried $qry="SELECT * FROM tbs_employee WHERE 1 "; but the result is 0-2:test Employee 1-2:test Employee 2-2:test Employee 3-2:test Employee 4-2:test Employee 5-2:test Employee 6-2:test Employee 7-2:test Employee
@BrindhaBaskaran you query sholud be "SELECT * FROM tbs_employee"
0

The reason this happens, is because you have created an endless loop. The way you use it, is the same as 'true', small example:

$example = 'some value';
if ($example) {
    echo 'You will see this echo!';
}

If we now take it one step further; you have a while loop. But the way you use it now, is as above. In other words it's always true:

$example = 'some value';
while ($example) {
    echo 'You will see this echo! At least untill the server runs out of resources';
}

What you have, is that you set $GetData before the loop. This way, after the loop has run one itteration, the value does not change. You only set it's value before the while, so you never move to the next line!

Luckily, the solution is very simple. We need to assign it every itteration. The following snippet is a common way to do it:

while($GetData = getdet($qry)){
    // Now, after every run, we set $GetData to the next line
}

The last run, the mysqli_fetch_array function returns null, so your function does too, so the while loop stops.


Offtopic: We have a few coding standards which make reading code easier. One of them is indentation; as you can see, my code reads easier. This is because I use 4 spaces (1 tab) in an if-statement for example. I strongly recommend you do this too (possible start with editting your topic here).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.