How does one get the raw " ->sql " to work in editor

How does one get the raw " ->sql " to work in editor

rbyrnsrbyrns Posts: 36Questions: 9Answers: 0

Maybe I am tired, but I have beat on this thing too long. Not that I am great sql writer, but an example would be nice. Everything I try throws errors like "php error - unexpected variable $sql". I think if I could get my head around this function, I would like to have a set of buttons that could send custom queries on a click. If I ever get the date range thing to work, I would like to add a select box that picks the date column and then send it. I keep searching but everything has the note that it is legacy and pre 1.10. I have chased the rabbit anyways a few times, but success is not happening with this one.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Using the sql() method is as simple as doing:

    $data = $db->sql( "SELECT * FROM table" )->fetchAll();
    

    Where of course the SQL string is updated to be whatever you need.

    Allan

  • rbyrnsrbyrns Posts: 36Questions: 9Answers: 0
    edited December 2014
    if(( $_POST['min'] != '')AND ($_POST['max']!='')){
        $min = $_POST['min'];
        $max = $_POST['max'];
        $data = $db->$sql = ("select *\n"
            . "from vert_test \n"
            . "left outer join notes on vert_test.uid = notes.uid\n"
            . "where \n"
            . "un25_test between \'$min\' \n"
            . "AND\n"
            . "\'$max\'");
        ->fetchAll(); 
          echo json_encode($db);
        exit();
    }
    

    I get "PHP Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR)" Referring to the ->fetchAll();

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    $db->$sql = (

    1. You need to execute the method rather than assign a value. Remove the = there.
    2. There is a spare semi-colon on line 10 that breaks the chanining
    3. You want to json encode $data not the $db connection.

    Allan

  • rbyrnsrbyrns Posts: 36Questions: 9Answers: 0
    edited December 2014

    if(( $_POST['min'] != '')AND ($_POST['max']!='')){
    $min = $_POST['min'];
    $max = $_POST['max'];
    $data = $db->$sql("select * from vert_test left outer join notes on vert_test.uid = notes.uid where un25_test between \'$min\' AND \'$max\'")->fetchAll();
    echo json_encode($data);
    exit();
    }

    returns : <b>Notice</b>: Undefined variable: sql in <b>/home/rex/www/cathouse2/php/table.vert_test.php</b> on line <b>42</b><br />
    <br />
    <b>Fatal error</b>: Method name must be a string in <b>/home/rex/www/cathouse2/php/table.vert_test.php</b> on line <b>42</b><br />

  • rbyrnsrbyrns Posts: 36Questions: 9Answers: 0

    I tried to clean that up but not sure how. I use VIM. Is there a setting anyone knows of that will not choke up markup so bad when using paste?

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    Answer ✓

    You still have $db->$sql where you should have $db->sql.

  • rbyrnsrbyrns Posts: 36Questions: 9Answers: 0

    Thanks for that, I guess using other old libraries for so long, made me absolutely blind to that. Working much better now. Still getting a TypeError: c is undefined error from jquery. But at least the data is coming back.

This discussion has been closed.