1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

mysql_query select help

Discussion in 'PHP' started by Divvy, Oct 13, 2015.

  1. #1
    Hello guys,

    Please give me a little help in a mysql_query (select)...

    At the moment, I have this in ID order:
    http://i.imgur.com/7fclwVp.png

    But I want to order by date... can someone help me how to do it?

    This is my code:
    $listFgpost =mysql_query("select * from $postTab where post_type='freegallery' and post_status='publish' and post_parent='$mrpostParent' order by ID DESC limit $start,$limit");
    PHP:
    This is my variables:
    $postTab = $wpdb->prefix."posts";
    $postMeta = $wpdb->prefix."postmeta";
    Code (markup):
    And this is my database tables:
    http://i.imgur.com/hQfKct8.png
    http://i.imgur.com/OizPuST.png
    http://i.imgur.com/78dkOy7.png

    Like I have, ordered by ID, is getting the info from prefix."posts";
    And the date that I want, I need to get from prefix."postmeta";
    Here you can see the meta_value that I need: http://i.imgur.com/78dkOy7.png

    Here is a mysql_query("select getting from prefix."postmeta"; maybe is useful:
    $query=mysql_query("select * from $postTab as a,$postMeta as b where a.ID=b.post_id and a.post_type='model' and a.post_status='publish' and b.meta_key='mr_model_appear_sites' and FIND_IN_SET($mrpostParent,b.meta_value) order by a.ID DESC");
    PHP:
    Can someone please help me? :)

    Thank you very much!!
     
    Divvy, Oct 13, 2015 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    I have a silly question: Why do all your field names have such meaningless, well.. names? Why is the date called "meta_value" for example?!?

    As to the answer, just change " order by ID DESC" to "ORDER BY meta_value" to the end of your queries... though gah, which tables are you even pulling from?!?

    But really the little code you have shown is shot full of issues, like the use of the mysql_ functions we've been told for a DECADE to stop using and that are soon to flat out cease to even EXIST, blindly dumping variables into the query string, declaring what TABLE is being used in the query string (that's a BIG "security, what's that?!?" moment), etc, etc... If that code is the slightest bit indicative of the rest of what you are working on, the whole thing should be deep sixed and rewritten from scratch before you either get raped by a cracker, or PHP just flat out stops supporting it.

    Formatting your queries so they aren't stuffed onto single lines could REALLY help with clarity too. Also doesn't help you've got your caps practices reversed; while mysql is case insensitive not all DB's are, and it's bad practice to have uppercase fieldnames and lower case operations.

    Pretty much a proper modern approach should look something more like this:

    $statement = $db->prepare('
    	SELECT *
    	FROM posts
    	WHERE post_type=`freegallery`
    	  AND post_status=`publish`
    	  AND post_parent=:postParent
    	  ORDER BY meta_value DESC
    	  LIMIT :start, :limit
    ');
    $statement->execute([
    	':postParent' => $mrpostParent,
    	':start' => $start,
    	':limit' => $limit
    ]);
    Code (markup):
    Though I'm guessing wildly here since what you are asking for seems unrelated to your queries or tables...
     
    deathshadow, Oct 13, 2015 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Can I just ask... why aren't you just using post_date in the first table to sort by date? Is the meta_value-date something else entirely?
     
    PoPSiCLe, Oct 13, 2015 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    I think that's what I'm getting confused on -- what tables are ACTUALLY being used for the output, why are there so many tables that look redundant, etc, etc...
     
    deathshadow, Oct 13, 2015 IP
  5. Divvy

    Divvy Well-Known Member

    Messages:
    781
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #5
    Thank you for your replies my friends... I understand your doubts but unfortunately my php knowledge is very poor and this wasn't coded by me but with someone else on freelancer.

    Can you guys help me solving this?
    This is my full code: http://paste2.org/9ppz2CjO

    PoPSiCLe, I cant want to use post_date because I don't want to use the date of the post.
    I want to use the date of a custom field that uses this table:
    http://i.imgur.com/OizPuST.png
    http://i.imgur.com/78dkOy7.png
    http://i.imgur.com/EAVbWof.png

    But you are right... maybe is more easy to order by post date right?
    Changing order by ID DESC with order by post_date DESC
    But for that, I need to replace this code:
    <?php echo get_post_meta($ID,'mr_freeGalleryDate',true); ?>
    PHP:
    To show the post date... does anyone know how to display post date in this format? dd/mm/yy?

    Waiting for your replies, thanks :)
     
    Divvy, Oct 14, 2015 IP