-2

Hello i made this query for wordpress plugin

$anyOne = $wpdb->get_results("SELECT MAX(id) as idlastest FROM " . $table_name_for_select . " WHERE drJlDate='".$dkdrbooking_for_compare."'");

the value in $anyOne is

Array ( [0] => stdClass Object ( [idlastest] => 32 ) ) 

I need to get 32 to use it in another query, but i can't select that. How can I get idlastest from this array ? I tried this code, but it didn't work

echo 'lastestID is : '.$myIds[0]['idlastest'];
Marwie
  • 3,177
  • 3
  • 28
  • 49
A.imahrap
  • 3
  • 1

2 Answers2

1

As you want to fetch single record use get_row function instead of get_results. And if you want to get results in array instead of object you can pass argument "ARRAY_A" in function as below.

//Change in query

$anyOne = $wpdb->get_row("SELECT MAX(id) as idlastest FROM " . $table_name_for_select . " WHERE drJlDate='".$dkdrbooking_for_compare."'",ARRAY_A);

//Fetch result

echo 'lastestID is : '.$anyOne['idlastest'];
Rahul Patel
  • 5,248
  • 2
  • 14
  • 26
  • hi tnx for ur answer acutyl i need run this quary $anyOne = $wpdb->get_results("SELECT drTurn FROM ".$table_name_for_select." WHERE id = (SELECT MAX(id) as lastestid from ".$table_name_for_select." WHERE drJlDate ='".$dkdrbooking_for_compare."')"); – A.imahrap Jul 27 '16 at 07:00
  • but it show me nothig and i chose write it some part – A.imahrap Jul 27 '16 at 07:01
  • @A.imahrap Ok got it. – Rahul Patel Jul 27 '16 at 07:03
  • Yes, if you want to fetch only one record then get_row is better choice than get_results. – Rahul Patel Jul 27 '16 at 07:08
  • tnx so much Rahul , i useing get_row , and my long qury work too , i new in stackoverflow , for show tanx to u just can give u vote ? or i can do anythig ? – A.imahrap Jul 27 '16 at 07:11
  • and can i use get_var too ? – A.imahrap Jul 27 '16 at 07:13
  • Your welcome :) your vote is enough my friend.. you can accept my answer as well but you have done it already by accepting Rakesh's answer so that's ok. No problem. – Rahul Patel Jul 27 '16 at 07:13
0

This is an object in your array, so use -> to get object value

echo 'lastestID is : '.$myIds[0]->idlastest;
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44