1

This is my Table structure,

CREATE TABLE `wp_business` (
  `id` int(11) NOT NULL,
  `8a` varchar(100) NOT NULL,
  `address_1` varchar(1000) NOT NULL,
  `city` varchar(250) NOT NULL,
  `state` varchar(250) NOT NULL,
  `zip_code` int(11) NOT NULL,
  `tel` varchar(100) NOT NULL,
  `fax` varchar(100) NOT NULL,
  `email` varchar(1000) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I trying to get data by select query,

SELECT * FROM wp_business;

I got the result but when I try to access the " 8a " column, getting an error.

$table_results[0]->8a;

Error:

[09-Dec-2019 16:38:20 UTC] PHP Parse error: syntax error, unexpected '8' (T_LNUMBER), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in details.php on line 93

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

3 Answers3

0

You cannot get field (variable) name '8a' of a class object in PHP the way you try.

On the other hand, this is not a typical (but legal) way to specify a field starting with digit in MySQL. Same with PHP classes. So there are several alternatives to fix:

  1. Change 8a to something else starting with letter.

  2. Specify array instead of object in PHP routine for MySQL result set.

  3. Specify explicitly SELECT ..., 8a as v8a, ...

  4. Use $table_results[0]->{'8a'};

Anatoliy R
  • 1,749
  • 2
  • 14
  • 20
0

Try variable parsing.

$table_results[0]-> {8a};

Or better way is to try to rename the column name in your SELECT query

SELECT `8a` as eightA FROM wp_business;

hope this helps!

Anand G
  • 3,130
  • 1
  • 22
  • 28
0

Here are some solutions have already been provided for the same problem - laravel how to access column with number name of a table?

Al-Amin
  • 596
  • 3
  • 16