0

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.

2 Answers 2

4

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().

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

3 Comments

That fixed it. Thank you very much. Saved me hours of headaches.
As soon as it will let me. You responded so fast the system won't let me select it as the correct answer yet.
This is a very big help. Not only has cleaned up the array, It has freed up half the memory too. Very helpful when working with over 10,000 records at a time.
0

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.

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.