Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Php function() with mysql queries not working

    I am creating a functions page to be included on all pages so I can keep pages tidy of code. Some of these functions are used to connect to and query a database. however these don't seem to work. Is it not possible to have Mysql queries inside of a function ? Or am I missing something.

    The functions are as follows...
    PHP Code:
    <?php
    function connect() {
                        
    mysql_connect($sql_host,$sql_user,$sql_pass) or die(mysql_error());
                        
    mysql_select_db($sql_data) or die(mysql_error());
                       }
                       

    function 
    indexFetch() {
                          
    $query "SELECT * FROM CDblg_article_test ORDER BY date ASC"
                          
    $latest mysql_query($query) or die(mysql_error());
                          
    $row_i mysql_fetch_assoc($latest);
                          }

    function 
    indexList() {
                          include 
    "inc/eng/index.php";
                
                              }
    ?>
    I am calling the functions via this page...
    PHP Code:
    <?php

        
    include "inc/CDblg.php";
        include 
    "inc/functions.php";

        
    connect();

            
    indexFetch();

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title><?php echo TITLE ?></title>
    </head>

    <body>

    <?php indexList(); ?>
    </body>
    </html>
    indexFetch(); works just fine as I have tested the page that calls the functions by exchanging the functions for the actual code contained within them.

    I am just trying to keep things tidy as the script requires the ability to have end users design their own layout around it. I looked into things such as smarty tags but from what I could tell smarty tags needs installing on the server with root privileges.

    When I test the page it just appears blank and viewing the source shows it to be empty.

    Any ideas ?

    Crowds
    PHP magpie | And President Of The Marmalade Atkins Fan Club | Crowds Design

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by Crowds View Post

    PHP Code:
    ...
    <body>

    <?php indexList(); ?>
    </body>
    ...
    What's indexList() supposed to return? Should that return be echoed?

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    indexlist() is a function that contains html with a table and while loop to echo out the results from a mysql query. Currently that is the only function on the page that works
    PHP magpie | And President Of The Marmalade Atkins Fan Club | Crowds Design

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    mysql functions do work within other functions.

    I'm wondering if the include in indexList() is breaking. Specifically, with respect to the path. Maybe it's trying to include relative to the functions.php path rather than the main path. Do you have access to the logs? If it's the path problem, it should give you an appropriate error there.

  • #5
    Regular Coder
    Join Date
    May 2005
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i don't have access to the logs...

    But this is all it contains..
    PHP Code:
    <table width="100%" border="0" cellpadding="0" cellspacing="0" class="indexTable">
        <?php do { ?>
        <tr>
          <td colspan="2"><img src="img/DEC2.gif" alt="<?php $btitle ?>" width="510" height="47"><h2><a href="topic.php?id_top=<?php echo $row_i['id']; ?>"><?php echo $row_i['heading']; ?></a></h2></td>
        </tr>
        <tr>
          <td style="padding-top:8px"><img src="img/bullit.gif" alt="<?php echo $btitle?>" width="10" height="10" /> <strong><a href="article.php?id_art=<?php echo $row_i['id_area']; ?>"><?php echo $row_i['heading']; ?></a></strong>&nbsp;</td>
          <td><div align="right"><small><strong>Published: <?php echo $row_i['date']; ?></strong></small></div></td>
        </tr>
        <tr>
          <td colspan="2"><br /><?php echo $row_i['text']; ?> 
          <br /><br />
        
         
              </td>
        </tr>
        <?php } while ($row_i mysql_fetch_assoc($latest)); ?>

      </table>
    PHP magpie | And President Of The Marmalade Atkins Fan Club | Crowds Design

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    function indexFetch() {
                          
    $query "SELECT * FROM CDblg_article_test ORDER BY date ASC"
                          
    $latest mysql_query($query) or die(mysql_error());
                          
    $row_i mysql_fetch_assoc($latest);
                          return 
    $row_i;
                          }
    ?> 
    if you want to keep code tidy you must check if the things works for example to check $latest if is valid and do something if is not, write into a log file, mail to the ceo/owner of the company, or anything you think is usefull.

    best regards
    Last edited by oesxyl; 03-15-2008 at 01:03 AM.

  • #7
    Regular Coder
    Join Date
    May 2005
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried returning it and there was no change. the page remains blank with no output or errors reported ?!
    PHP magpie | And President Of The Marmalade Atkins Fan Club | Crowds Design

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Crowds View Post
    I tried returning it and there was no change. the page remains blank with no output or errors reported ?!
    this is probabbly because the query don't return anything. Try this:

    PHP Code:
    function indexFetch() {
                          
    $query "SELECT * FROM CDblg_article_test ORDER BY date ASC"
                          
    $latest mysql_query($query) or die(mysql_error());
                          
    $row_i "this query dont work";
                          if(
    $latest && mysql_num_rows($latest) > 0){
                             
    $row_i mysql_fetch_assoc($latest);
                          }
                          return 
    $row_i;
                          } 
    or better:

    PHP Code:
    function indexFetch() {
                          
    $query "SELECT * FROM CDblg_article_test ORDER BY date ASC"
                          
    $latest mysql_query($query);
                          
    $row_i "this query dont work";
                          if(
    $latest){
                             
    $row_i mysql_fetch_assoc($latest);
                          }else{
        
    $row_i mysql_error();
    }
                          return 
    $row_i;
                          } 
    best regards
    Last edited by oesxyl; 03-15-2008 at 03:04 AM.

  • #9
    Regular Coder
    Join Date
    May 2005
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks oesxyl. Tried both versions but still no joy.
    However, I did try adding the include CDblg.php which contains the database details within the connect() function and it now connects to the database but the indexFetch is failing to retrieve any data.
    PHP Code:
    function connect() {
                       include 
    "inc/CDblg.php";
                       
    mysql_connect($sql_host,$sql_user,$sql_pass) or die('Nope cant connect');
                       
    mysql_select_db($sql_data) or die(mysql_error()); 
                       } 
    PHP magpie | And President Of The Marmalade Atkins Fan Club | Crowds Design

  • #10
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Crowds View Post
    Thanks oesxyl. Tried both versions but still no joy.
    However, I did try adding the include CDblg.php which contains the database details within the connect() function and it now connects to the database but the indexFetch is failing to retrieve any data.
    PHP Code:
    function connect() {
                       include 
    "inc/CDblg.php";
                       
    mysql_connect($sql_host,$sql_user,$sql_pass) or die('Nope cant connect');
                       
    mysql_select_db($sql_data) or die(mysql_error()); 
                       } 
    try this, put at the begining of the file before any include:

    PHP Code:
    <?php
    ini_set
    ('display_errors','1');
    ini_set('display_startup_errors','1');
    error_reporting (E_ALL);

    ?>
    best regards

  • #11
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Moving the db connection details into the function and getting a result shows part of the problem. It's all coming come to variable scope issues, apparently. I was able to make your code work by stirring up this stew of functions and passing vars in and out down your function waterfall. Here's the relevant portions on the main index.php and the functions.php:
    PHP Code:
    // index
    $dbconn = connect();
    $arr= indexFetch($dbconn);
    ...
    <?php indexList($arr); ?>
    ...

    // functions
    <?php
    ...
    function 
    indexFetch() {
      
    $query "SELECT * FROM CDblg_article_test ORDER BY date ASC";
      
    $latest mysql_query($query) or die(mysql_error());
      
    $row_i mysql_fetch_assoc($latest);
      
    $rv = array($row_i,$latest);
      return 
    $rv;
    }

    function 
    indexList($arr) {
      
    $row_i $arr[0];
      
    $latest $arr[1];
      include 
    "inc/eng/index.php";
    }
    ?>
    Yes - it's sloppy, but it makes it work. Attempting cleaner code is a good thing, but at a point, it becomes unreadable and difficult to maintain. Merging some (if not all) of this would make it easier to read and easier to debug. (Be sure to follow oesxyl's advice above, too.)

    edit: By the way, my passing back the result AND the row is stupid. I'm only doing that so I didn't have to edit yet another included file (/inc/eng/index.php). If insisting upon this function stew, it would be best to pass back just the result and extract the row from the next function.
    Last edited by tomws; 03-15-2008 at 04:44 AM. Reason: clarification

  • #12
    Regular Coder
    Join Date
    May 2005
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks tomws,

    Sorry for the late reply. Your solution is working fine !

    Thanks again.

    Jim
    PHP magpie | And President Of The Marmalade Atkins Fan Club | Crowds Design

  • #13
    New to the CF scene
    Join Date
    Mar 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    About that listbox generation i found the solution. the problem was that i was trying to use php coding inside a html form and that wont happen unless i editedthe .htaccess file but it comes with a price. the best way to do it is to name the form u are using with a .php extension that worked for me as php doent recognise a code within any file unless its told.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •