FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Create Function to create SQL dynamically





therimalaya
I've just started PHP. I'm very much unknown to Object Oriented part of PHP.
Yesterday, I was trying to create a PHP function that can fetch data from some specified table when the user pass the parameters of fields that is to be displayed.

I encountered an error. I'll paste the code here, please help me..

Code:

<?php
   function createSQL()
   {
      if(func_num_args()==0)
      {
         $sql="SELECT * FROM student";
         return $sql;
         exit();
      }
      else
      {
         $arg=func_get_args();
         $sql="\"Select ";
         for($i=0;$i<count($arg);$i++)
         {
            $sql.=$arg[$i].", ";
         }
         $sql.="FROM student\"";
         return $sql;
         exit();
      }
   }
   function selectdb($host="localhost",$user="root",$pass="",$db="testdb")
   {
      $con=@mysql_connect($host,$user,$pass) or die("Database can not be connected");
      @mysql_select_db($db) or die("Database Cannot be selected");
      return $con;
   }
   function createFullSQL($sql,$where,$order)
   {
   
   }
//Passing SQL
$con=selectdb();
$sql=createSQL(roll, name, address);
$rs=mysql_query($sql);
while($recordArray=mysql_fetch_assoc($rs)){
print_r($recordArray);
}
?>
macky
what is the error says?
jmraker
Maybe you're seeing warnings about constants not being defined so it's treating them as strings

createSQL(roll, name, address);
should have quotes to say they aren't constants
createSQL('roll', 'name', 'address');

If the query isn't working you can add output the string returned from mysql_error()
http://us2.php.net/mysql_error

If I were to make the createSQL function I'd make the 1st parameter an array of fields so you could build a query that could have a where clause and order clause

function createSQL($fields, $table='student', $where=null, $order=null);

and use createSQL(array('roll', 'name', 'address'), 'student', array('name LIKE "bob%"'), array('roll', 'name'));
to create SELECT roll, name, address from student WHERE name LIKE "bob%" ORDER BY roll, name
davidv
I'm probably going to be more of a nuisance than of any help but I don't understand the point of having the exit(); after the return statement. In the languages I know, once you've hit that line any other line below it becomes redundant.
therimalaya
Thank you all for all your responses. I found out the mistake, I have created the SQL with extra comma(,) in the last field I should have use the function rtrim() funciton.
Dear jmraker, I'm trying to create the function as you have suggested. When I'll finish I'll paste it.

Thank you
Fire Boar
davidv wrote:
I'm probably going to be more of a nuisance than of any help but I don't understand the point of having the exit(); after the return statement. In the languages I know, once you've hit that line any other line below it becomes redundant.


This is correct, and the exit() is actually an error, since exit doesn't return from the function, it ends the script entirely. Fortunately the code never gets this far because, as davidv pointed out, return stops the execution.
ProwerBot
Isn't exit() basically the same thing as die()? They both end the web page.
Fire Boar
The die and exit constructs are in fact equivalent. I tend to prefer exit for stopping the script prematurely for debugging (e.g. print_r($variable); exit;) and normal termination if necessary, and die for errors (I'd usually have the error message rendered in the site template if possible though).
Related topics
SQL and PHP
Can I create many SQL database by my self
phpBB in my account?
phpBB - Installation Tutorial
Help with linux
tranfering phpbb2 forum
Export and Import Mysql Table
Switching Hosts, help me!
Need help fixing my php file
adding a function to a class dynamically
newbie MySQL question
Need someone to help me out. [100 frih$ or a PR 4 link]
c++ object design help
how to center swf title to a flash website does anyone know?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.