Solved

Custom Function problem?

Posted on 2001-06-07
20
235 Views
Last Modified: 2006-11-17
I wrote a function that connect's to database and then recieves a query string that i pass it. I'm having problems when calling the function with a select statement and then outputting that content that was selected. I can't return the values if they are not included in the Function. But then the function would not be generic any more?  
0
Comment
Question by:rudidebruyn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 4
  • 2
  • +3
20 Comments
 
LVL 15

Expert Comment

by:a.marsh
ID: 6163414
Can we see the function?

Ant
0
 

Author Comment

by:rudidebruyn
ID: 6163427
Here is the db function i have taken th while statement out. I use the same function for an insert and it works, great, but getting the info out that's the problem!

// Select
     function Qselect($dbHost, $dbUser, $dbPass, $dbName, $Sql){
          $dbh           =      mysql_connect($dbHost, $dbUser, $dbPass)
                              or die ("Could not connect.");
          $res           =      mysql_db_query($dbName, $Sql, $dbh)
                              or die ("Could not run SQL Select query");
         
}
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6163437
I don't understand the problem really - can the function not return the resultset generated? e.g. add the line:

return $res;

Ant
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rudidebruyn
ID: 6163478
Ok wait here is what i want to do the whole thing! I'm trying to write pure php pages so all the html is also stored in functions.

Page one is the systemfunction.php page here i store all my functions:
<?php
      
      /*
            Set the systems headers.
      */
      function html($data) {
            return "<html>\n<head>\n\t<title>\n\t\tGenesys Administration system\n\t</title>\n</head>\n\n$data</html>\n";
      }
      
      /*
            Set the systems headers.
      */
      function body($data) {
            return "<body>\n$data\n</body>\n";
      }
      
      /*
            Time function and format
      */
      function timedate(){
            $today = date("F j, Y");
            return "$today";
      }
      
            
      /*
            Database functions.
            Qinsert function inserts data.
            Qupdate function updates data.
            Qselect function selects data to be printed.
      */
      
      // Select
      function Qselect($dbHost, $dbUser, $dbPass, $dbName, $Sql){
            $dbh             =       mysql_connect($dbHost, $dbUser, $dbPass)
                                    or die ("Could not connect.");
            $res             =       mysql_db_query($dbName, $Sql, $dbh)
                                    or die ("Could not run SQL Select query");
            
      }
      
      // Insert
      function Qinsert($dbHost, $dbUser, $dbPass, $dbName, $Sql){
            
            $dbh             =       mysql_connect($dbHost, $dbUser, $dbPass)
                                    or die ("Could not connect.");
            $res             =       mysql_db_query($dbName, $Sql, $dbh)
                                    or die ("Could not run SQL Insert query");
            
      }
      
      /*
            Functions to create Tables
            These functions include functions named:
            Tb;with it's border, cellpad, cellspace and width.
            Tr's no values here
            Td with values, align, bgcolor, colspan, valign and content.
            
            The Tables and rows must be ended using functions called:
            endTables
            endTr
      */
      
      /*
            Sequence for table function values: $borders, $cellpad, $cellspace, $tblwidth, $data
            Sequence for td function values: $data, $bgcolor, $span, $align, $valign, $width
      */
      
      function tbl ($data) {
            list($borders, $cellpad, $cellspace, $tblwidth, $data) = func_get_args();
            $str = "\n<table";
            if(!empty($borders)) {
                  $str .= " border=\"$borders\"";
            }else{
                  $str .= " border=\"0\"";
            }
            if (!empty($cellpad)) {
                  $str .= " cellpadding=\"$cellpad\"";
            }else{
                  $str .= " cellpadding=\"0\"";
            }
            if (!empty($cellspace)) {
                  $str .= " cellspacing=\"$cellspace\"";
            }else{
                  $str .= " cellspacing=\"0\"";
            }
            if (!empty($tblwidth)) {
                  $str .= " width=\"$tblwidth\"";
            }
            $str .= ">\n$data\n</table>\n";
            return $str;
      }      
      
      function tr ($data) {
      return "\t<tr>\n$data\t</tr>\n";
      }
            
      function td () {
            list($data, $bgcolor, $span, $align, $valign, $width) = func_get_args();
            $str = "\t\t<td";
            if(!empty($bgcolor)) {
                  $str .= " bgcolor=\"$bgcolor\"";
            }
            if(!empty($span)) {
                  $str .= " colspan=\"$span\"";
            }
            if (!empty($align)) {
                  $str .= " align=\"$align\"";
            }
            if (!empty($valign)) {
                  $str .= " valign=\"$valign\"";
            }
            if (!empty($width)) {
                  $str .= " width=\"$width\"";
            }
            $str .= ">\n\t\t\t$data\n\t\t</td>\n";
            return $str;
      }

      /*
            Input types      
      */
            
      function frm($data) {
            list($action, $method, $name, $data) = func_get_args();
            $str = "\n<form";
            if(!empty($action)) {
                  $str .= " action=\"$action\"";
            }
            if (!empty($method)) {
                  $str .= " method=\"$method\"";
            }
            if (!empty($name)) {
                  $str .= " name=\"$name\"";
            }
            $str .= ">\n$data\n</form>\n";
            return $str;
      }      
      
      function textinput ($name, $value) {
            return "<input type=\"text\" name=\"$name\" value=\"$value\">\n";
      }
            
      function submit ($name, $value) {
            return "<input type=\"submit\" name=\"$name\" value=\"$value\">\n";
      }
      
      function clear ($value) {
            return "<input type=\"submit\" name=\"reset\" value=\"$value\">\n";
      }
      
      function radioinput ($name, $value) {
            list($name, $value, $sel) = func_get_args();
            $str = "<input type=\"radio\" name=\"$name\" value=\"$value\"";
            if (!(empty($sel))) {
                  $str .= " checked";
            }
            $str .= ">\n";
            return $str;
      }
      
      function hiddeninput ($name, $value) {
            return "<input type=\"hidden\" name=\"$name\" value=\"$value\">\n";
      }
      
      function selectinput () {
            list($name, $options, $selected) = func_get_args();
            $htm = "<select name=\"$name\">\n";
            $htm .= "<option value=\"\"> -- Select one -- </option>\n";
            
            foreach ($options as $value => $desc) {
                  $htm .= "<option value=\"$value\"";
                  if (!(empty($selected)) && ($selected == $value)) {
                        $htm .= "selected";
                  }
                  $htm .= ">$desc</option>\n";
            
            }
            $htm .= "</select>\n";
            return $htm;
      }            
      
?>

and page 2 is where i want to use it:



0
 

Author Comment

by:rudidebruyn
ID: 6163488
Look for the #@$%&! that is where i wouldlike to displat the results.

<?php
     /*
          Required files are called here.
     */
     require("config.php"); // required database connections
     require("systenfunciotns.php"); // required to call php functions
         
     /*
          Queries that handel the inserts, edit and deletes of the services tbl
          Get SQl string ready for Qselect query.
     */    
     $Sql  = "SELECT * FROM  services;";
     Qselect($dbHost, $dbUser, $dbPass, $dbName, $Sql);
         
     /*
     Sequence for table function values: $borders, $cellpad, $cellspace, $tblwidth, $data
     Sequence for td function values: $data, $bgcolor, $span, $align, $valign, $width
     Add services
     */
     echo html(
               body(
                    frm("createService.php", "get", "",
                         tbl(1,0,0,600,
                              tr(
                                    td("Add services","white","2","center","top")
                              )
                              .tr(
                                    td("Service Name:","white","","left","top",200)
                                   .td(textinput(ServiceName, ""),"white","","left","top")
                              )
                              .tr(
                                    td("Service Description:","white","","left","top")
                                   .td(textinput(ServiceDesc, ""),"white","","left","top")
                              )
                              .tr(
                                    td("Service Price:","white","","left","top")
                                   .td(textinput(ServicePrice, ""),"white","","left","top")
                              )
                              .tr(
                                    td("Status:","white","","left","top")
                                   .td(textinput(Status, "1"),"white","","left","top")
                              )
                              .tr(
                                    td("Once off:","white","","left","top")
                                   .td(radioinput(OnceOff, 1),"white","","left","top")
                              )
                              .tr(
                                    td("Setup Date:","white","","left","top")
                                   .td(textinput(SetupDate, timedate()),"white","","left","top")
                              )
                              .tr(
                                    td(submit("Addserv", "Add").clear(Clear).hiddeninput("Add",1) ,"green","2","center","top")
                              )
                         )
                    )
                    .frm("createService.php", "get", "",
                         tbl(1,0,0,600,
                              tr(
                                    td("Add services","white","2","center","top")
                              )
                              .tr(
                                   td("Service Name goes here:", "white", "", "left", "top", 300)
                                   .td("Data goes here #@$%&! ", "white", "", "left", "top", 300)
                              )
                              .tr(
                                    td("&nbsp;" ,"orange","2","center","top")
                              )
                         )
                    )
               )
          );
     
?>


thax
0
 

Author Comment

by:rudidebruyn
ID: 6163586
Look for the #@$%&! that is where i wouldlike to displat the results.

<?php
     /*
          Required files are called here.
     */
     require("config.php"); // required database connections
     require("systenfunciotns.php"); // required to call php functions
         
     /*
          Queries that handel the inserts, edit and deletes of the services tbl
          Get SQl string ready for Qselect query.
     */    
     $Sql  = "SELECT * FROM  services;";
     Qselect($dbHost, $dbUser, $dbPass, $dbName, $Sql);
         
     /*
     Sequence for table function values: $borders, $cellpad, $cellspace, $tblwidth, $data
     Sequence for td function values: $data, $bgcolor, $span, $align, $valign, $width
     Add services
     */
     echo html(
               body(
                    frm("createService.php", "get", "",
                         tbl(1,0,0,600,
                              tr(
                                    td("Add services","white","2","center","top")
                              )
                              .tr(
                                    td("Service Name:","white","","left","top",200)
                                   .td(textinput(ServiceName, ""),"white","","left","top")
                              )
                              .tr(
                                    td("Service Description:","white","","left","top")
                                   .td(textinput(ServiceDesc, ""),"white","","left","top")
                              )
                              .tr(
                                    td("Service Price:","white","","left","top")
                                   .td(textinput(ServicePrice, ""),"white","","left","top")
                              )
                              .tr(
                                    td("Status:","white","","left","top")
                                   .td(textinput(Status, "1"),"white","","left","top")
                              )
                              .tr(
                                    td("Once off:","white","","left","top")
                                   .td(radioinput(OnceOff, 1),"white","","left","top")
                              )
                              .tr(
                                    td("Setup Date:","white","","left","top")
                                   .td(textinput(SetupDate, timedate()),"white","","left","top")
                              )
                              .tr(
                                    td(submit("Addserv", "Add").clear(Clear).hiddeninput("Add",1) ,"green","2","center","top")
                              )
                         )
                    )
                    .frm("createService.php", "get", "",
                         tbl(1,0,0,600,
                              tr(
                                    td("Add services","white","2","center","top")
                              )
                              .tr(
                                   td("Service Name goes here:", "white", "", "left", "top", 300)
                                   .td("Data goes here #@$%&! ", "white", "", "left", "top", 300)
                              )
                              .tr(
                                    td("&nbsp;" ,"orange","2","center","top")
                              )
                         )
                    )
               )
          );
     
?>


thax
0
 

Author Comment

by:rudidebruyn
ID: 6164215
Look for the #@$%&! that is where i wouldlike to displat the results.

<?php
     /*
          Required files are called here.
     */
     require("config.php"); // required database connections
     require("systenfunciotns.php"); // required to call php functions
         
     /*
          Queries that handel the inserts, edit and deletes of the services tbl
          Get SQl string ready for Qselect query.
     */    
     $Sql  = "SELECT * FROM  services;";
     Qselect($dbHost, $dbUser, $dbPass, $dbName, $Sql);
         
     /*
     Sequence for table function values: $borders, $cellpad, $cellspace, $tblwidth, $data
     Sequence for td function values: $data, $bgcolor, $span, $align, $valign, $width
     Add services
     */
     echo html(
               body(
                    frm("createService.php", "get", "",
                         tbl(1,0,0,600,
                              tr(
                                    td("Add services","white","2","center","top")
                              )
                              .tr(
                                    td("Service Name:","white","","left","top",200)
                                   .td(textinput(ServiceName, ""),"white","","left","top")
                              )
                              .tr(
                                    td("Service Description:","white","","left","top")
                                   .td(textinput(ServiceDesc, ""),"white","","left","top")
                              )
                              .tr(
                                    td("Service Price:","white","","left","top")
                                   .td(textinput(ServicePrice, ""),"white","","left","top")
                              )
                              .tr(
                                    td("Status:","white","","left","top")
                                   .td(textinput(Status, "1"),"white","","left","top")
                              )
                              .tr(
                                    td("Once off:","white","","left","top")
                                   .td(radioinput(OnceOff, 1),"white","","left","top")
                              )
                              .tr(
                                    td("Setup Date:","white","","left","top")
                                   .td(textinput(SetupDate, timedate()),"white","","left","top")
                              )
                              .tr(
                                    td(submit("Addserv", "Add").clear(Clear).hiddeninput("Add",1) ,"green","2","center","top")
                              )
                         )
                    )
                    .frm("createService.php", "get", "",
                         tbl(1,0,0,600,
                              tr(
                                    td("Add services","white","2","center","top")
                              )
                              .tr(
                                   td("Service Name goes here:", "white", "", "left", "top", 300)
                                   .td("Data goes here #@$%&! ", "white", "", "left", "top", 300)
                              )
                              .tr(
                                    td("&nbsp;" ,"orange","2","center","top")
                              )
                         )
                    )
               )
          );
     
?>


thax
0
 
LVL 2

Expert Comment

by:higijj
ID: 6164750
You could also declare a variable at the very top, outside any function.. and inside your Qinsert and Qselect function, use global

$res = "";


inside the function:

global $res;

so you would set the $res variable and be able to use it everywhere in your script.
0
 

Expert Comment

by:jbkerra
ID: 6166200
To get it out of the database and into a variable, you need to go one step further with 'mysql_fetch_object'. Use a while loop and save each field in that array variable. Check this out.

$db = mysql_connect("localhost", "username", "password");
mysql_select_db("dbname", $db)
or die(mysql_errno() . ": " . mysql_error() . "<br>");


$sql = "SELECT field1, field2 FROM table";
     $result=mysql_query($sql, $db);
$i=0;
     
while($obj=mysql_fetch_object($result)){
 //the above line actually gets the data

  $field1var[$i] = $obj->field1;
  $field2var[$i] = $obj->field2;
  //these lines take the data from pointer object
  //to the variable
  print($field1var[$i]);
  print($field2var[$i]);
$i++;
}
0
 

Author Comment

by:rudidebruyn
ID: 6167078
Thax guys but it still does not acomplish what i need to?

My problem is outing puttin the information selected in to this space:
)
                   .frm("createService.php", "get", "",
                        tbl(1,0,0,600,
                             tr(
                                   td("Add services","white","2","center","top")
                             )
                             .tr(
                                  td("Service Name goes here:", "white", "", "left", "top", 300)
                                  .td("Data goes here #@$%&! ", "white", "", "left", "top", 300)
                             )
                             .tr(
                                   td("&nbsp;" ,"orange","2","center","top")
                             )
                        )
                   )
-----------
I will also need to re call the following functions over for each record:

.tr(
                                  td("Service Name goes here:", "white", "", "left", "top", 300)
                                  .td("Data goes here #@$%&! ", "white", "", "left", "top", 300)
                             )

-----------

Thax I hope this make sthe problem more clear.

I'll up the points to 500 knowing this is true brain buster.
0
 

Author Comment

by:rudidebruyn
ID: 6167086
The db fields are:
ServiceID
Status
OnceOff
ServiceName
ServiceDesc
ServicePrice
Setupdate

0
 

Author Comment

by:rudidebruyn
ID: 6167278
The db fields are:
ServiceID
Status
OnceOff
ServiceName
ServiceDesc
ServicePrice
Setupdate

0
 
LVL 1

Expert Comment

by:Ludax
ID: 6178179
If you are sure that your select return only ONE result you could add at the end of your function :

// Select
function Qselect($dbHost, $dbUser, $dbPass, $dbName, $Sql
{
    $dbh=mysql_connect($dbHost, $dbUser, $dbPass)
         or die ("Could not connect.");
    $res=mysql_db_query($dbName, $Sql, $dbh)
         or die ("Could not run SQL Select query");
    if(mysql_num_rows($res)) //then it's not an insert or update
    {
        $row=mysql_fetch_row($res);
        return($row);
    }
}
//The output of the function will be a table that contain each selected fiel in your query :
example:

$MyRES=Qselect('host','user,'pass','MyDB','select field1,field2 from MyTABLE where UniqueID='12');

will return :
$MyRES[0] = field1
$MyRES[1] = field2
$MyRES[2] = field3
0
 
LVL 1

Expert Comment

by:katyan
ID: 6179067
listening
0
 

Author Comment

by:rudidebruyn
ID: 6180062
That's the problem I need a way of getting more than 1 result, and then be able to use it. The biggest problem is getting the info from the function, so that I can call it outside of the function.

Ludax, a.marsh, jbkerra , higijj  Thax for the help so far.
0
 
LVL 1

Expert Comment

by:Ludax
ID: 6180616
If you have more than 1 result that's not really a problem, the only think is that you will be forced to
do some analyse outside the function :

function Qselect($dbHost, $dbUser, $dbPass, $dbName, $Sql
{
   $dbh=mysql_connect($dbHost, $dbUser, $dbPass)
        or die ("Could not connect.");
   $res=mysql_db_query($dbName, $Sql, $dbh)
        or die ("Could not run SQL Select query");
   if(mysql_num_rows($res)) //then it's not an insert or update
       return($res);
   }
}

$res=Qselect('host','user,'pass','MyDB','select field1,field2 from MyTABLE where UniqueID='12');

while($row=mysql_fetch_row($res))
{
   echo $row[0]; (field1)
   echo $row[1]; (field2)
   echo $row[2]; (field3)
}
0
 

Author Comment

by:rudidebruyn
ID: 6180802
Here guys thax for all the help i solved the problem myself here is the code:

<?php

        require("config.php"); // required database connections
        require("systenfunciotns.php"); // required to call php functions
               
        /*
                Queries that handel the inserts, edit and deletes of the services tbl
                Get SQl string ready for Qselect query.
        */      
       
       
        function Qselect($dbHost, $dbUser, $dbPass, $dbName, $Sql)
        {
                $dbh     =       mysql_connect($dbHost, $dbUser, $dbPass)
                                                or die ("Could not connect.");
                $res     =       mysql_db_query($dbName, $Sql, $dbh)
                                                or die ("Could not run SQL Select query");
               
                //The problem was this now MAGIC
                return $res;
        }
       
        //The MAGIC
        $THE_STUFF = Qselect($dbHost, $dbUser, $dbPass, $dbName, "SELECT * FROM  services;");
        //THE_STUFF is now a RECORD SET
         
          while($goeters = mysql_fetch_row($THE_STUFF)){
               echo $goeters[0].$goeters[4]."<br>";
          }
?>
0
 
LVL 1

Accepted Solution

by:
Ludax earned 300 total points
ID: 6181001
hey, that's what I just said man !
0
 

Author Comment

by:rudidebruyn
ID: 6181827
Yeh hey thax for the help, did not see your answer but here is the points. Reward for your time and effort.

Cheers man
0
 
LVL 1

Expert Comment

by:Ludax
ID: 6181842
thanx, see ya ;)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question