I am using Functions in PHP. I want to write a function like the following function selectTable($tbName) { $result = mysql_query("select * from $tbName") or die("SELECT Error: ".mysql_error()); return $result; } I called the function using selectTable("Users"); But nothing happens... Can anybody help me...
function selectTable($tbName) { $result = mysql_query("SELECT * FROM $tbName") or die("SELECT Error: ".mysql_error()); $result2 = mysql_fetch_array($result); return var_dump($result2); } *note, remove var_dump later on* You might want to go write a mysql class in OOP, doing this will make just cut through a lot of redundancy well.. I really suggest you read a bit more, doing these kind of functions will become a bit troublesome =/
Do u think that it is better to use the select statement in the code rather than putting it in afunction and using it again. I am little bit confused whether I have to call function or use it directly...
I generally just leave most of my specific mysql statements in the code rather than put them in functions. But I might have generic functions that do a bit more. eg, an outputTable function, which has a parameter that is the mysql tablename, and outputs the html code to display that table. If something is only been repeated a few times, and it's only a few lines, I generally wont make it a function. If it's in the code hundreds of times, and contains maybe 5 lines or more, I'll definitely be thinking a function. One thing I have in mind is to not "over-engineer" things. Unless your writing something huge, it usually doesn't take too long to refactor things out to functions later. Trying to do everything perfect can sometime just get in the way of getting it done...
Like BuyMyScripts said, it's better to leave it in the code than a function BUT sometimes that's just asking to write more lines. The whole idea here is to be able re re-use the objects anywhere in the part of the code. For example, <?php getNames('fromTable'); PHP: What is wrong here? certainly it sounds like a great way to do things, but the fact that you are restricting everything to only one parameter will make it a bit hard for you. <?php // Lets set the constructor and initiate a new class $db =new mySQL($host,$user,$password,$port,$dbname); // Certainly, we are querying the server, which uses only mysql_query() $db->query("SELECT name FROM myforums_users WHERE id='9'"); PHP: How come I did this and what is the difference? I'll show you. Certainly, you want to put different methods in your mysql class (which I will show an example of how it would look) // We we do need more functionality, right? $getUserQuery = $db->query("SELECT name FROM myforums_users WHERE id='9'"); $result = $db->getRow($getUserQuery); /* This will output something like this: print_r($result); will print an array with the information of the user */ PHP: Now now, you will see an example on how this would look (the class). class mySQL { private $connection; private $dbname; public function __construct($host,$user,$password,$port,$dbname) { $this->connection = mysql_query('$host:$port',$user,$password); if(!$this->connection) { die('Sorry there was an error:'.mysql_error()); } else { mysql_select_db($dbname); } $this->dbname = $dbname; } public function query($statement) { return mysql_query($statement,$this->connection); } public function getRow($getRow) { return mysql_fetch_array($getRow, MYSQL_ASSOC); } } PHP: You don't have to understand this in an hour or so, it has taken me a great deal of time to learn PHP and well lets go to another example In your first snippet you used function selectTable($tbName) { $result = mysql_query("select * from $tbName") or die("SELECT Error: ".mysql_error()); return $result; } PHP: Now watch this, $query = selectTable('users'); $db->getRow($query); PHP: Wait, did I just used one from my class? yea I did, and it works, why? because you are returning a query resource, now and my getRow will finish the job of getting the row. I hope this helps you under, the whole idea of OOP is to make everything re-usable, but beyond that, to make your life easier too. Go to sites like http://phpfreaks.com they offer a lot of help and I'm around in there daily too