unkempt Posted March 26, 2008 Share Posted March 26, 2008 ok so im passing arguments to a function i.e. [pre]function getAllStaff ($live, $tblname) {[/pre] but how do i get $tblname argument to replace "staff" (the table) in the following code: [pre]$sql = sprintf("SELECT * FROM staff WHERE live = '%s';", $live);[/pre] Hope you can help Quote Link to comment https://forums.phpfreaks.com/topic/97948-newbie-passing-arguments-please-help/ Share on other sites More sharing options...
wildteen88 Posted March 26, 2008 Share Posted March 26, 2008 As you're using sprintf, then add tblname as the second parameter, eg: [tt]$sql = sprintf("SELECT * FROM %s WHERE live = '%s';", $live, $tblname);[tt] Quote Link to comment https://forums.phpfreaks.com/topic/97948-newbie-passing-arguments-please-help/#findComment-501129 Share on other sites More sharing options...
ansarka Posted March 26, 2008 Share Posted March 26, 2008 why you want to use like this change the below code $sql = sprintf("SELECT * FROM staff WHERE live = '%s';", $live); TO $sql = "SELECT * FROM ".$tblname." WHERE live ='".$live."'"; OR $sql = "SELECT * FROM $tblname WHERE live ='$live'"; OR $sql = 'SELECT * FROM '.$tblname.' WHERE live ='.$live; Quote Link to comment https://forums.phpfreaks.com/topic/97948-newbie-passing-arguments-please-help/#findComment-501130 Share on other sites More sharing options...
unkempt Posted March 26, 2008 Author Share Posted March 26, 2008 Thanks but that didn't seem to work - here is my function - does this look correct: [pre]function getAllWorks ($live, $tblname) { // Create SQL statement $sql = sprintf("SELECT * FROM %s WHERE live = '%s';", $live, $tblname); // Trace the query in the NetConnection debugger //NetDebug::trace($sql); // Run query on database $result = mysql_query($sql); $return = Array(); while($row=mysql_fetch_object($result)) { $row->last_updated = $this->datetime_mysql_to_mysoup($row->last_updated); array_push($return, $row); } return $return; } [/pre] Quote Link to comment https://forums.phpfreaks.com/topic/97948-newbie-passing-arguments-please-help/#findComment-501135 Share on other sites More sharing options...
unkempt Posted March 26, 2008 Author Share Posted March 26, 2008 Thanks this worked [pre]$sql = "SELECT * FROM $tblname WHERE live ='$live'";[/pre] I was using someone elses code that helped me previously - what is the reason that they would have used sprint Quote Link to comment https://forums.phpfreaks.com/topic/97948-newbie-passing-arguments-please-help/#findComment-501140 Share on other sites More sharing options...
ansarka Posted March 26, 2008 Share Posted March 26, 2008 that you should ask them all programmers will have there own style of coding your sprint also work if you change it to $sql = sprintf("SELECT * FROM %s WHERE live = '%s';", $tblname,$live); Quote Link to comment https://forums.phpfreaks.com/topic/97948-newbie-passing-arguments-please-help/#findComment-501142 Share on other sites More sharing options...
unkempt Posted March 26, 2008 Author Share Posted March 26, 2008 Thankyou all - this is my first post here - you have all been very helpfull - great forum Quote Link to comment https://forums.phpfreaks.com/topic/97948-newbie-passing-arguments-please-help/#findComment-501151 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.