1

I am trying to create a loop that displays each row in a table. I think I need to make an array out of the PK, but I can't figure out how to do that. Here is my code so far:

$conn = dbConnect('read');
$getData = 'SELECT * FROM table';
$allData = $conn->query($getdata);
if (!$allData) {
    $error = $conn->error;
} else {
    $data = $allData->fetch_assoc();
    $rowId = array($data['PK']);
} while ($rowId <= count($rowId)) {
    // code to be run for each row
    $rowId++;
}

EDIT: Sorry my question is confusing, I'm new to PHP.

8
  • 2
    you know that fetch_assoc() only fetches one row after each other? Commented Jan 7, 2018 at 1:10
  • Julio: It is not called PK, but it is an auto-increment integer. Jeff: I did not know that. What would you suggest instead? Commented Jan 7, 2018 at 1:12
  • your aren't including the $row inside the while and your are "calling" and array "2 times" with the fetch assoc and then array (). Look at my answer. Commented Jan 7, 2018 at 1:18
  • Why would you want to capture just a generated surrogate key? It sounds like there is another problem you're trying to solve which you haven't told us about which you are trying to fix the wrong way. Commented Jan 7, 2018 at 1:18
  • 1
    side note: table isn't the actual name for it, is it? Commented Jan 7, 2018 at 1:30

2 Answers 2

4

Your question is a bit confusing but I think this is what you are trying to do:

$sql = 'SELECT * FROM table';
$query = $conn->query($sql);
while ($row = $query->fetch_assoc()) {
    $data[$row['PK']] = $row;
}

That would iterate over each row, creating an array and using the row's value for column PK as an associative array key.

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

Comments

2

fetch_assoc() (I assume mysqli here now) doesn't fetch all data from a result, but one row after each other. So you don't need to make an array of $row['PK'], but need to loop over the results.

$conn = dbConnect('read');
$getData = 'SELECT * FROM `table`'; // you would need backticks here, if the table really is called "table" (what you shouldn't do...)
$result = $conn->query($getData); // it's not 'allData', it is a result_set. And be carefull about Case! $getData!=$getdata
if (!$result) {
    $error = $conn->error;
} else {
    $cnt=0;
    while($row = $result->fetch_assoc()) {
        // code to be run for each row
        // you can display $row['PK'] now:
        echo $row['PK'];
        // or add that value to something else, whatever you need
        $cnt = $cnt+$row['PK'];
        // or to have a new array with the values of one table-column:
        $columnRows[] = $row['PK'];
    }
    // now you can use the created array
    foreach($columnRows as $PK) {
        echo $PK;
    }
}

4 Comments

Look at these 2 lines: $getData = 'SELECT * FROM table'; $result = $conn->query($getdata); $getData != $getdata.
Thanks. copy&paste from the OP... I didn't see it.
you could have adapted it. It's not a conquest here! We just need to make sure our answers are correct.
Yeah but without codeigniter I would ended doing the same as @billynoah in his answer and we can repeat an answer lol

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.