ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

queries using php

March 11, 2010 1:45pm

Subscribe [3]
  • #1 / Mar 11, 2010 1:45pm

    Joe Wolin

    206 posts

    I’m following this guide in the EE wiki as I need to do some custom queries (outside the query module).

    http://expressionengine.com/wiki/SQL_query_with_pure_PHP/

    I’m new to php and so wondering what advice people having in best practices for handling the code for the database connection and close.

    Do I have to type the first five lines of this example code in all my custom php pages or is there an easier way to include this information as a global variable.

    <?php global $PREFS; 
    $db = mysql_connect( // Handle the connection to the database. 
    $PREFS->core_ini['db_hostname'], // hostname, 
    $PREFS->core_ini['db_username'], // username and 
    $PREFS->core_ini['db_password']); // password are automatically pulled 
    mysql_select_db($PREFS->core_ini['db_name']); // from EE's config.php


    Looking for good advice on best practices.

  • #2 / Mar 11, 2010 1:58pm

    Gareth Davies

    491 posts

    Is this in a separate database to the EE install as if not you can simply use the database class?

    http://expressionengine.com/docs/development/usage/database.html or http://expressionengine.com/public_beta/docs/development/usage/database.html for 2.0

  • #3 / Mar 11, 2010 2:02pm

    Joe Wolin

    206 posts

    Perfect - thanks Gareth.  I’m just connecting to the EE database.  It’s tough being the one asking such basic questions.  Argh!

  • #4 / Mar 13, 2010 12:11pm

    Focus Lab Dev Team

    1129 posts

    Joe,

    If you have any questions or complications using the database class come back and ask away! Have you looked at that to solve your problem yet?

  • #5 / Mar 13, 2010 1:10pm

    Joe Wolin

    206 posts

    Thanks Erik,

    I got the database class to work perfect as mentioned by Gareth.  I appreciate you following up…

  • #6 / Mar 19, 2010 2:12pm

    jaasum

    36 posts

    I too am trying this technique. I have pulled the table from my clients old site as well as the function performing the query. I know virtually no php, so this currently isn’t working, but it isn’t killing my page either. Any advice?

    <?php 
                            
                            function getreading() {
                                global $DB;
                                $month = date("m");
                                $day = date("d");
                                $result = mysql_query("SELECT * FROM `scriptures` WHERE `month` = '$month' AND `day` = '$day'");
                                $verse = mysql_fetch_array($result);
                                    $reading = "<ul>\r\n\t\t";
                                    if ($verse[verse1]) {$reading .= "<li><a >$verse[verse1]</a></li>\r\n\t\t"; };
                                    if ($verse[verse2]) {$reading .= "<li><a >$verse[verse2]</a></li>\r\n\t\t"; };
                                    if ($verse[verse3]) {$reading .= "<li><a >$verse[verse3]</a></li>\r\n\t\t"; };
                                    if ($verse[verse4]) {$reading .= "<li><a >$verse[verse4]</a></li>\r\n\t\t"; };
                                    $reading .= "</ul>\r\n";
                                    return $reading;
                                }
                                
                            getreading();
                            
                            ?>
  • #7 / Mar 19, 2010 2:32pm

    Focus Lab Dev Team

    1129 posts

    Jaasum

    Can you explain a little more about what you’re trying to do? Assuming you already have a table in your EE database called “scriptures” then this will work with one slight adjustment. Your function “getreading” is returning a value to the PHP parser. That value is holding your unordered list. By calling getreading() you run that function which returns the value. The problem is that you aren’t doing anything with it. Change the last line from

    getreading();
    to
    echo getreading();

    Also, since you don’t know much about PHP be very very careful when writing any code (re: copy/pasting/editing code) that has any interaction with the database. You could royally screw something up or accidentally allow for attacks on your database.

  • #8 / Mar 19, 2010 2:34pm

    Gareth Davies

    491 posts

    Think there are some syntax errors in there as well - remove those extra ; after the closing braces in the if statements

    if ($verse[verse1]) {$reading .= "<li><a >$verse[verse1]</a></li>\r\n\t\t"; }
    if ($verse[verse2]) {$reading .= "<li><a >$verse[verse2]</a></li>\r\n\t\t"; }
    if ($verse[verse3]) {$reading .= "<li><a >$verse[verse3]</a></li>\r\n\t\t"; }
    if ($verse[verse4]) {$reading .= "<li><a >$verse[verse4]</a></li>\r\n\t\t"; }
  • #9 / Mar 19, 2010 2:39pm

    jaasum

    36 posts

    Thanks Eric and Gareth

    Eric, that did the trick. I guess that makes sense, I have just never written a function in php, so I guess I didn’t know how to use it properly.

    Is there anything in this script that could be considered potentially harmful? I am seriously doing nothing more than copy/pasting it from the client’s old site. And I’ve made sure to backup the database before going in and doing this.

    Gareth, I removed them and it works just as well, thank you!

  • #10 / Mar 19, 2010 2:41pm

    ender

    1644 posts

    you’re not using any variables in the query that could have anything malicious in them so it looks fine to me.

  • #11 / Mar 19, 2010 2:42pm

    Focus Lab Dev Team

    1129 posts

    Eric, that did the trick. I guess that makes sense, I have just never written a function in php, so I guess I didn’t know how to use it properly.

    I suggest you read up on some intro tutorials for PHP. There are some great books out there as well. Understanding the basics will help a lot when needing to port over code from one site to another. 😊

    Is there anything in this script that could be considered potentially harmful?

    Nope.

  • #12 / Mar 19, 2010 2:44pm

    jaasum

    36 posts

    ender, thanks

    Eric, That’s the thing, I’ve read through two php books and then I never program in it, so I just lose basic knowledge of the syntax haha. Using ExpressionEngine has made me lazy with PHP I suppose.

  • #13 / Mar 19, 2010 2:46pm

    Focus Lab Dev Team

    1129 posts

    I’ve read through two php books and then I never program in it, so I just lose basic knowledge of the syntax haha. Using ExpressionEngine has made me lazy with PHP I suppose.

    Haha. That’s why I suggest tutorials. They tend to be free and can help for quick things (like forums). Books are only good for you if you act on your newly found knowledge. Otherwise the knowledge just fades away and never becomes understanding. (for most people, Ty excluded 😛)

    Glad it’s working 😊

  • #14 / Mar 20, 2010 1:54pm

    ender

    1644 posts

    (for most people, Ty excluded 😛)

    lolwut?

  • #15 / Mar 21, 2010 3:27pm

    Focus Lab Dev Team

    1129 posts

    (for most people, Ty excluded 😛)

    lolwut?

    Just suggesting that you might retain certain learned facts & techniques longer than the average bear. That’s the impression I had at WFHS at least (of you and your sis) 😊

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases