We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12262
    • 40 Posts
    Hello,

    I want to access an external database within a snippet (without having to worry about xPDO...) - just a straight and simple way.

    I tried it using PHP directly, something like:

    $link = mysql_connect("myhost", "myuser", "mypwd");
    mysql_select_db("mydb");
    $query = "Select whatever";
    $result = mysql_query($query);


    After that I can return the field names of the query like that:
    $y=mysql_num_fields($result);
    for ($x=0; $x<$y; $x++)
    {
      echo "<th>".mysql_field_name($result, $x).'</th>';
    } 


    But then I need to extract the contents, and this is what does not work within modX (same script works without modX directly using PHP).

      while ($line = mysql_fetch_object($result)) 
      {
        echo "<tr>";
        foreach ($line as $col_value) 
        {
          echo "<td>$col_value</td>";
        }
        echo "</tr>";
      } 
      mysql_free_result($result);
      mysql_close($link);



    I know that the output should be in a return value, error handling is missing, silly variable names ...
    But this would be the next step. I also tried mysql_fetch_row instead. It looks like it's iterating but as soon as I want to access any value of the row the snippet breaks and I get an empty page.

    Any idea? Did someone do something similar?

    Thomas [ed. note: hochmohr last edited this post 11 years, 3 months ago.]
      • 36572
      • 20 Posts
      Silly question, But are you share you are getting a result?
      And what does the error_log of the server and modx error log have to say wink

      And a little tip. use mysqli_ instead of mysql. It is more secure .

        Developer @ Sterc bureau voor internet & marketing