Hello, Iam not an expert in PHP as I want to echo latest value from the table please help.

My table name is : sym_fruit

fileds name in the above tables are : id, fruitname, date

each day i will add new fruit to this table so the latest fruit name only need to print, if I didn't added any fruit name on a day i need to show a message "New fruit name was not added on today " in my table (sym_fruit) the date was storing like this ( 12/07/2018 10:14:43pm )
so each day added name are insterted to the table with autoincrument of ID , fruit name and date

I tried below code to ech but it was printitng this text : Array below is my code

<?php 
$pname = $con->select("select fruitname from `sym_fruit` order by `id` desc ",false); ?>       

<?php echo $pname; ?>

Recommended Answers

All 4 Replies

You are using DB abstraction - no idea what this "select" method returns - object, resource or array of arrays.
Let us assume its an array of arrays (either indexed or associative).
So:

if( $pname ){
    print_r( $pname );
}

echo does not work for arrays.

Hello,

below is my table screen shot, actually i want to show the last added fruit name on my website. If it can be print there based on the date I mean today is 13/July/2018 I haven't added any names there so I want to show an meesage "New fruit name was not added on today " if in the table there was any fruit name added on today date (13/July/2018) that need to print. If any one know how to do it that will be helpful

Screenshot_3.png

Any one can help me on this it was very urgent..

I believe the general SQL you need is something like:

"SELECT `fruitname` FROM `sym_fruit` ORDER BY `id` DESC LIMIT 1"

That will give you ONE record due to the "LIMIT 1" clause, however, tying it down to your date is a little trickier due to the format you've decided to use for your table. Generally, the format YYYY-MM-DD HH:MM:SS (or the numerical equivalent (YYYYMMDDHHMMSS) is used.

So one solution for this - there my be a better one would be:

"SELECT `fruitname` FROM `sym_fruit` WHERE STR_TO_DATE(`date`, '%d/%m/%Y') =  CURDATE() ORDER BY `id` DESC LIMIT 1"

So if no records returned - show negative statement, else you have your fruit and echo it in your string.

BTW - urgency is irrelevant - we are all volunteers and help when we can - we don't jump when prodded :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.