Jump to content

Recommended Posts

I am upgrading from an older PHP and am having difficulty with this particular piece of code. I use to be able to refer to elements of an array pulled from a MySQL dB by their number placement within that dB table. That no longer seems to be functioning … at least not with the code I’ve been using.

<?php
include 'r_open_link.php';

$query = "SELECT * FROM ember_seating_charts WHERE course_id = 204";
$result= $pdo->query($query);
$row = $result->fetch();

print "Cell 6: ".$row[6]." should be ".$row['cell1']."<br>";

?>

This is a test script I wrote to check the functionality of my code. $row[6] and $row[‘cell1’] should both be the same value because ‘cell1’ is the 7th field in that table. The code returns the proper value for $row[‘cell1’], but gives the error “Warning: Undefined array key 6 in /home/jandrews/public_html/test_gen.php on line 8” now. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/325297-upgrading-php/
Share on other sites

  • Solution

There are several FETCH modes in PDO.

The most usual is to set the default to PDO::FETCH_ASSOC so the row arrays are indexed by field name. This is normally set in your connect options.

However, even if the default is set it can be overridden when required. So...

$row = $result->fetch(PDO::FETCH_NUM) will allow you to use $row[6].

 

Link to comment
https://forums.phpfreaks.com/topic/325297-upgrading-php/#findComment-1639120
Share on other sites

Thank you! It worked. I did, however, look at the FETCH modes you linked to and chose FETCH_BOTH as I need both. If I were to write the whole script all over again I would use ASSOC here, but when I wrote this particular tool (it’s a seating chart) I didn’t know how to do something like this ${‘cell’.$count} and I’m spending so much time re-writing so much code to bring my PHP5 code up to PHP8 functionality standards that I just can’t re-write the entire tool the way I’d like to. THANK YOU for your help!!!

Link to comment
https://forums.phpfreaks.com/topic/325297-upgrading-php/#findComment-1639187
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.