1
function getjobid() {
global $geotag_table, $wpdb;
$qry = "SELECT * FROM mirdc_jo_form WHERE  jo_id = (SELECT MAX(jo_id)  FROM mirdc_jo_form)";
$desc = $wpdb->get_results($qry);

return $desc; }

can you help me guys.. i am trying to get the id from my table and try to show the data to my wordpress..

<?php  foreach (getjobid() as $generatedid) {
echo $generatedid;?>  #<------ this is the error                       

this is my wordpress code.. can you teach me how i convert the object to string.. to show the result to my wordpress.

9
  • the error is self explanatory that you are trying to echo an object. <?php $array_data = getjobid(); ?> now print it first and then work accordingly. Show use what you got after printing this variable Commented Jan 11, 2017 at 5:16
  • Please Try $desc = $wpdb->get_results($qry,ARRAY_A); Commented Jan 11, 2017 at 5:17
  • uhmm sory sory i am just a begginer how i convert it to string ? Commented Jan 11, 2017 at 5:18
  • @manish jesani the error was gone but it show "array" .. Commented Jan 11, 2017 at 5:19
  • @ElliJoshuaRey print_r($generatedid); Commented Jan 11, 2017 at 5:20

2 Answers 2

2

Please try this is batter for your code

function getjobid() {
global $geotag_table, $wpdb;
$qry = "SELECT jo_id FROM mirdc_jo_form WHERE  jo_id = (SELECT MAX(jo_id)  FROM mirdc_jo_form)";
$desc = $wpdb->get_col($qry);

return $desc[0]; }
$lastid = getjobid();
Sign up to request clarification or add additional context in comments.

7 Comments

ohhhhh .. i see..sr can you explain to me why it is better to my codes.. sory i am a beginner and trying to study here :D
@ElliJoshuaRey your query SELECT * FROM mirdc_jo_form........ you get all field from database table but as your requirement only one field 'jo_id'. so why are you get all field. so your information vary large database you get one field mysql query fast response from database otherwise your site SLOW....
ohhhhhhhhhhh isee thx for your informative information i will always take note it :D
sr i have a question.. what if i dont have value on my table.. and if there is no data on my table. i want to have value of 1.. try an if statement and there is wrong to it if (!$lastid) { echo "Ther is no data"; echo $val1; } else { foreach ($lastid as $row) { $auto = $row->jo_id; if ($auto === ''){ echo "There is no id!"; } else { echo $auto; } } } the wrong part is when theres a value on my table it gives me a lot of 9999999 (my last id is 9) why it give me so many 9?
dumb of me xD why i put an foreach statement when i already get the jo_id if (!$lastid) { echo "Ther is no data"; echo $val1; } else { echo $lastid->jo_id; }
|
1

$generatedid is an Object and containing entire row of data. You need to specify what column you want from that row.

You should try var_dump($generatedid) to view all the data.

foreach (getjobid() as $generatedid) {
    echo $generatedid->jo_id; // or whatever ID you want from DB 
}

7 Comments

OWWW i see that was it :( i am so noob THX SR T_T
i tried it earlier ---- echo $generatedid["jo_id"]; but it was wrong
It's an object, you should try $generatedid->jo_id
i am new here and it help me a lot when i don`t understand in programming :D so many people want to help if i have a question..
It's a learning phase for all of us. Everyone was new at some point of time. Keep up the spirit and keep going. Wish you all the best. :)
|

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.