jimmy - 1 year ago 81

SQL Question

This is not the first time I have used

`mysqli_fetch_assoc()`

`$sql = "SELECT * FROM database.table WHERE PushNumber > 0";`

$result = $conn->query($sql);

if($result->num_rows > 0) {

while($row = mysqli_fetch_array($result)) {

// This will loop through each row, now use your loop here

$row = mysqli_fetch_assoc($result);

$pushNumber = $row['PushNumber'];

$followingIDs = $row['FollowingIDs'];

$usernameID = $row['Username'];

error_log("'$pushNumber", 0);

error_log("'$usernameID", 0);

}

}

There is one row in my table where

`PushNumber`

`0`

`$pushNumber`

`int`

`$usernameID`

`string`

`''`

`PushNumber`

`1`

`Username`

`@yo`

Answer Source

You appear to assign to completely different sets of values into your $row variable.

`$row = mysqli_fetch_array($result)`

transfers the entire array into $row, but then in your loop you use `$row = mysqli_fetch_assoc($result)`

to transfer a single record into $row, which is most likely throwing your loop off.

Try this:

```
if($result->num_rows > 0)
{
while($row = mysqli_fetch_assoc($result))
{
// This will loop through each row, now capture each row's data here
$pushNumber = $row['PushNumber'];
$followingIDs = $row['FollowingIDs'];
$usernameID = $row['Username'];
error_log($pushNumber, 0);
error_log($usernameID, 0);
}
}
```