0

I'm just trying to get the max value of a row in my table, I then want to insert that value into another table, so I'm trying to get that max value, and every time I'm trying to do something with it, I get this error "illegal string offset" .

$qry2= "SELECT MAX(buss_id) FROM businesses";
$result= mysqli_query($con,$qry2);
$maxid = mysqli_fetch_assoc ($result);

print_r ($maxid);
foreach($maxid as $individual_data)
{
   //Assign the values
   $maxx = $individual_data['buss_id'];
}

My print_r function prints everything properly, but I just can't access the value I need to manipulate. I'm a beginner, please be kind. Thank you. Array ( [MAX(buss_id)] => 47 )

2
  • 1
    The array key will be the column name. In this case the column name is MAX(buss_id) so either you need to change your query to SELECT MAX(buss_id) AS buss_id or change your code to get the key as $maxx = $individual_data['MAX(buss_id)']; Commented Feb 24, 2017 at 7:51
  • @GordonM Illegal string offset 'MAX(max_buss_id)' mith's answer worked, though. Thank you man. Commented Feb 24, 2017 at 9:21

4 Answers 4

2

Try using alias

$qry2= "SELECT MAX(buss_id) AS maxid FROM businesses";
$result= mysqli_query($con,$qry2);
$maxid = mysqli_fetch_assoc ($result);

echo $maxid['maxid'];
Sign up to request clarification or add additional context in comments.

Comments

1

Youn should use a proper alias for assign the name that you use for accessing eg:

 $qry2= "SELECT MAX(buss_id) as buss_id FROM businesses";

1 Comment

@Mar1ak . well if my answer is useful (like others ) you could rate properly ... over 15 you can use upper arrow left to answer for sign as useful
1

Since your query is SELECT MAX(buss_id) FROM businesses, the resulting column name will be MAX(buss_id) as is evident the the print_r result you have shared.

Array ( [MAX(buss_id)] => 47 )

You will have to alias the resulting field name as you want to get it:

$qry2   = "SELECT MAX(buss_id) AS max_buss_id FROM businesses";
$result = mysqli_query($con,$qry2);
$maxid  = mysqli_fetch_assoc ($result);

foreach ($maxid as $individual_data)
{
    //Assign the values
    $maxx = $individual_data['max_buss_id'];
}

5 Comments

This still gave me the same error. mith's answer worked, though. Your suggestion would work if $individual_data['max_buss_id']; became $maxid['max_buss_id']; I really don't understand it, since $individual_data should be the same as $maxid in the foreach loop. But having $individual_data['max_buss_id'] doesn't work and $maxid['max_buss_id'] works. Thanks for answering!
@Mar1ak Did u noticed the AS max_buss_id in the SQL query? You might have missed it I think.
@Jmoos Yes bro I did. Also I just edited my first comment, I meant to say mith's answer. You all suggested the same thing, which is correct but for some reason using the foreach's $key variable AKA $individual_data in my case doesn't work. It's no big deal however. I don't need to foreach anything, I was just trying to assign the value to some variable using foreach. It was already achieved in Mith's answer without the use of foreach. I pointed it out cause it's just weird.
@Mar1ak Hmm. What else is causing the problem then? Anyway, good to know that smith's answer worked and you got it solved :)
@Jmoos I wish I knew. As far as I understand it. The $individual_data should be the same as the $maxid in the foreach loop. Apparently it's not quite like that. Otherwise it should've worked just like you suggested. Luckily I won't need a foreach loop for my code. Thanks a lot man =)
-1

@Mar1ak you did only one mistake, change $individual_data['buss_id'] to $individual_data['max'] because in your query you use max() without any alias so max() will return value with column name max

$qry2= "SELECT MAX(buss_id) FROM businesses";
    $result= mysqli_query($con,$qry2);
    $maxid = mysqli_fetch_assoc ($result);

print_r ($maxid);
foreach($maxid as $individual_data)
{
   //Assign the values
   $maxx = $individual_data['max'];}

2 Comments

That still won't work. The array key name is wrong.
what you are getting in your array print_r ($maxid); ? update your question with this

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.