Delirious Intentions
Delirious Intentions

Reputation: 27

php mysql remove numbered key's from fetched array row

I am trying to do a mysql fetch but it keeps adding numbered and labeled keys to the array. I want it to record only the labeled names and data in the array.

Am I using the wrong mysql call?

global $con,$mysqldb;
$sql="SHOW FIELDS FROM ".$dbtable;
$tr = mysqli_query($con,$sql);
$tl = mysqli_fetch_array($tr);
$tl = mysqli_fetch_array($tr);

$sql="SELECT * FROM ".$mysqldb.".".$dbtable." ORDER BY ".$tl['Field']." LIMIT 3";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
    $table[$row[1]] = $row;
}

foreach($table as $item => $data){
    foreach(array_keys($data) as $pointer => $field) {
        echo"pointer=".$pointer."\t";
        echo"field=".$field."\n";
        echo "data=".$data[$field]."\n";
    }
}

reults

pointer=0       field=0 data=3823
pointer=1       field=PID       data=3823
pointer=2       field=1 data=AA
pointer=3       field=symbol    data=AA
pointer=4       field=2 data=1
pointer=5       field=value   data=1

I want to omit 0, 2, & 4 from the array.

Upvotes: 0

Views: 782

Answers (2)

Tom
Tom

Reputation: 4292

Take a look at the PHP.net manual for the mysqli_fetch_array() function.

You'll see there's an option called resulttype that will accept 1 of 3 values - MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH the default.

Using MYSQLI_ASSOC will remove the numbered keys.

Or check mysqli_fetch_assoc().

Upvotes: 3

Delirious Intentions
Delirious Intentions

Reputation: 27

Thanks to thebluefox for a speedy response. I replaced the fetch with:

while($row = $result->fetch_array(MYSQLI_ASSOC)) {

And now the results are being recorded as they should.

Upvotes: 0

Related Questions