Read a row on the server-side

Read a row on the server-side

obrienjobrienj Posts: 93Questions: 38Answers: 0

Assuming:

DB: $editor->db()
Table: "table1"
Where: "id = '54'"

What is the PHP code using datatalbes facilities to read that row into an key-value array?

Regards,
Jim

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,860Questions: 1Answers: 10,135 Site admin

    Hi Jim,

    $editor->db()
      ->select( 'table1', '*', array( 'id' => 54 ) )
      ->fetchAll();
    

    Document for it is available here.

    Allan

  • obrienjobrienj Posts: 93Questions: 38Answers: 0

    Allan,

    Sorry, I didn't know that documentation was there.

    Can I assume that if I write:

    $rowdata = $editor->db()
                      ->select( 'table1', '*', array( 'id' => 54 ) )
                      ->fetchAll();
    

    that the data will be in $rowdata at completion?

    Sorry to ask such a basic question but I get on a 13 hour plane ride in about an hour with no internet to access "stuff".

    Again, thanks for all the support.

    Regards,
    Jim

  • obrienjobrienj Posts: 93Questions: 38Answers: 0

    Allan,

    Just curious, what is the SQL operator "=>" that you used in "'id' => 54"?

    I can't find it documented anywhere but if I use "=" instead the select fails.

    Regards,
    Jim

  • allanallan Posts: 61,860Questions: 1Answers: 10,135 Site admin

    what is the SQL operator "=>" that you used in "'id' => 54"?

    It isn't an SQL operator - its a PHP array assignment. In Javascript it would be { id: 54 }.

    that the data will be in $rowdata at completion?

    Yes - that will contain an array of the row(s) selected. print_r( $rowdata ); to see its structure.

    Alan

  • obrienjobrienj Posts: 93Questions: 38Answers: 0

    Allan,

    I am convinced that PHP will drive me to drink!

    This is what I have: (Note: $log is a logging class I have built)

                    $log->debug('preEdit', "values: " . print_r($values, true) . PHP_EOL);
    
                    $log->debug("preEdit", "'id' = '" . $values['id'] . "'");
    
                                   <<<<<<< I see nothing in the log after the above line >>>>>>>>>>
    
                    $rowdata = $editor->db()
                      ->select( 'calendar.events', '*', array( "'id' => '" . $values['id'] . "'" ) )
                      ->fetchAll();
    
                    $log->debug("preEdit", "Back from select");
    
                    $log->debug('preEdit', print_r($rowdata, true) . PHP_EOL);
    
                    $log->debug("preEdit", "preEdit Ending");
    

    And the log contains: (Note: 'id' is a valid column and the value is also valid as well as the "calendar.events". I see no errors in the Web Console or network traffic.)

    [08/08/2017 10:04:02.000000] (preEdit         ) **DEBUG** values: Array
    (
        [id] => 7a5e78b2-8c98-4076-ac46-f4e28ae66ed8
        [title] => recurring
        [whenHolder] => 
        [rptHolder] => 
        [start] => Monday, August 7, 2017 11:30 AM
        [end] => Monday, August 7, 2017 12:30 PM
        [allDay] => 
        [action] => 22
        [rfc5545] => RRULE:FREQ=WEEKLY;BYDAY=MO,WE,FR;COUNT=10
        [rfcDates] => 2017-08-07 11:30:00~2017-08-07 12:30:00|2017-08-09 11:30:00~2017-08-09 12:30:00|2017-08-11 11:30:00~2017-08-11 12:30:00|2017-08-14 11:30:00~2017-08-14 12:30:00|2017-08-16 11:30:00~2017-08-16 12:30:00|2017-08-18 11:30:00~2017-08-18 12:30:00|2017-08-21 11:30:00~2017-08-21 12:30:00|2017-08-23 11:30:00~2017-08-23 12:30:00|2017-08-25 11:30:00~2017-08-25 12:30:00|2017-08-28 11:30:00~2017-08-28 12:30:00
        [rfcid] => be0c6cf7-86e5-4fdf-9b9d-df30d8d4feea
        [location] => Zack's BBQ, 721 E Bridge St, Hotchkiss, CO 81419, USA
        [placeID] => ChIJD6BEhSG9QIcRpBI7s89KLvU
        [institution] => Zack's BBQ
        [address] => 721 E Bridge St
        [city] => Hotchkiss
        [state] => CO
        [country] => US
        [zipcode] => 81419
        [neighborhood] => 
        [phone] => (970) 872-3199
        [mapURL] => https://maps.google.com/maps?q=721%20E%20Bridge%20St,%20Hotchkiss,%20CO%2081419,%20USA
        [webURL] => http://www.zacksbbq.net/
        [locPicURL] => https://maps.google.com/?cid=17667140644146713252
        [latitude] => 38.7987237
        [longitude] => -107.71074540000001
        [description] => <p>recurring xxx</p>
    
    )
    
    [08/08/2017 10:04:02.000000] (preEdit         ) **DEBUG** 'id' = '7a5e78b2-8c98-4076-ac46-f4e28ae66ed8'
    

    If I remove the "select", the function runs to the end and exits as written.

    Be patient with me, but what is going on?

    Regards,
    Jim

  • tangerinetangerine Posts: 3,350Questions: 37Answers: 394
    edited August 2017 Answer ✓
    ->select( 'calendar.events', '*', array( "'id' => '" . $values['id'] . "'" ) )
    

    Try

      ->select( 'calendar.events', '*', array( 'id' => $values['id'] ) )
    
  • obrienjobrienj Posts: 93Questions: 38Answers: 0

    Thanks tangerine, that did it.

    And PHP has driven me to drink!!

    I'm sitting in the bar at a Hilton mumbling to myself and drinking iced tea.

    I didn't know that after 50 year doing this coputer stuff that learning a new programming language (my 25th by last count) could be so much fun!?!?!

    Regards,
    Jim

  • tangerinetangerine Posts: 3,350Questions: 37Answers: 394

    Long Island iced tea...

  • allanallan Posts: 61,860Questions: 1Answers: 10,135 Site admin

    Many people start on PHP so things only get better ;-)

    In fairness to PHP, it really is quite good these days. You just need to get used to some of its quirks. I enjoy writing in PHP (albeit not as much as some other languages...).

    Allan

This discussion has been closed.