We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29697
    • 24 Posts
    i have written a php snippet to do a db query but it will not work

    <?php //<-- strip me
    
    $global_dbh = mysql_connect('JONSERV', 'thegent', 'gringo');
    mysql_select_db('drivers', $global_dbh);
    
    function display_db_table($tablename, $connection )
    {
    $query_string = "SELECT * FROM $tablename";
    $result_id = mysql_query($query_string, $connection);
    $column_count = mysql_num_fields($result_id);
    
    print("<table border=1>\n");
    while ($row = mysql_fetch_row($result_id))
    {
    print("<tr align=left valign=top>");
    for ($column_num = 0;
    $column_num < $column_count;
    $column_num++)
    print("<td>$row[$column_num]<td/>\n");
    print("<tr/>\n");
    }
    


    in the page content i put [[snippet name]] and all i get is

    Parse error: syntax error, unexpected $end in C:\apache\Apache2\htdocs\manager\includes\document.parser.class.inc.php(691) : eval()’d code on line 19

    what gives?

    snippet documentation is sketchy to say the least
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Looks to me like you forgot to close your function definition block (i.e. missing } ).

      Also, if you enclose your code samples in the
      ...
      tags (you can automatically create them from the # button on the editing menu) it makes things much easier to see.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 25663 MODX Staff
        • 12,272 Posts
        When you get those type of errors, start looking around line 19 of your snippet as indicated by the last part of the parse error:
        Parse error: syntax error, unexpected $end in C:\apache\Apache2\htdocs\manager\includes\document.parser.class.inc.php(691) : eval()’d code on line 19
          Ryan Thrash, MODX Co-Founder
          Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          I have never had that "unexpected $end" without having missed a closing brace somewhere. And in your code, the function closing brace is missing.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 29697
            • 24 Posts
            ok i finally managed to get it right with this format:

            $global_dbh = mysql_connect(’JONSERV’, ’thegent’, ’gringo’);
            mysql_select_db(’formula1’, $global_dbh);

            function display_db_table($connection )
            {
            $query_string = "SELECT * FROM drivers";
            $result_id = mysql_query($query_string);
            $column_count = mysql_num_fields($result_id);

            print("<table border=1>\n");
            while ($row = mysql_fetch_row($result_id))
            {
            print("<tr align=left valign=top>");
            for ($column_num = 0;
            $column_num < $column_count;
            $column_num++)
            print("<td>$row[$column_num]<td/>\n");
            print("<tr/>\n");
            }
            }

            Problem is that for some reason MODx is putting an empty column in between the real ones.
            Anyone know a reason for this please
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Without knowing what is in your table in the database, or maybe even a screenshot of what you are getting as a result output, it’s impossible to say what’s going on.

              You can put that code in a stand-alone .php file and run it directly from the browser’s address bar, and see if it behaves any differently outside of MODx.

                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 29697
                • 24 Posts
                Quote from: sottwell at Mar 16, 2006, 11:50 PM

                Without knowing what is in your table in the database, or maybe even a screenshot of what you are getting as a result output, it’s impossible to say what’s going on.

                You can put that code in a stand-alone .php file and run it directly from the browser’s address bar, and see if it behaves any differently outside of MODx.


                Go here to see the result
                http://www.sa-alien.co.za/index.php?id=75
                the database table has only the four fields in it
                after each data cell is a narrow blank cell
                  • 10487 MODX Staff
                  • 1,535 Posts
                  Hi theagent,

                  In your code, your closing table row and data cell tags are wrong: <tr/> should be </tr> and <td/> should be </td> wink

                  I believe the <td/> is creating the blank cell. (Haven’t tried this but would put money on this one!)

                  Cheers, Garry
                    Garry Nutting
                    Senior Developer
                    MODX, LLC

                    Email: [email protected]
                    Twitter: @garryn
                    Web: modx.com
                    • 29697
                    • 24 Posts
                    Cheers Garryn
                    Well spotted that man
                    thank you that did the trick