kpetsche20 Posted July 20, 2008 Share Posted July 20, 2008 Hey, I'm trying to make a function so I don't have to keep writing while loops. Anyway I cannot get any information to display. I dont think the variable is being send to the function because nothing displays when I run the code. index.php <?php include("functions.php"); $par="users"; mywhile($par); ?> loop.php <?php function mywhile($par) { $sql = "SELECT * FROM ".$par.""; $sql = mysql_query($sql); while($data = mysql_fetch_array($sql)) { $name = $data['name']; } echo $name; } ?> Link to comment https://forums.phpfreaks.com/topic/115664-problem-with-making-a-function-display/ Share on other sites More sharing options...
cooldude832 Posted July 20, 2008 Share Posted July 20, 2008 your query might be failing have you tried to write your query in a debugging method such as <?php $q = "Select * from `table`"; $r = mysql_Query($q) or die(mysql_error()."<br /><br />".$q); ?> Link to comment https://forums.phpfreaks.com/topic/115664-problem-with-making-a-function-display/#findComment-594596 Share on other sites More sharing options...
kpetsche20 Posted July 20, 2008 Author Share Posted July 20, 2008 yes i did that, no errors Link to comment https://forums.phpfreaks.com/topic/115664-problem-with-making-a-function-display/#findComment-594598 Share on other sites More sharing options...
cooldude832 Posted July 20, 2008 Share Posted July 20, 2008 do u have error reporting even on??? Cause u say the file with the function is loop.php but you include functions.php you should have a call to undeclared function error if that is how it is. Also you echo is outside the while loop in the function meaning that the echoed $name is only the last $name in the series of rows Also you fail to check if there is any rows returned using mysql_num_rows on the query resouce Overall this is poor programing with no really net gain Link to comment https://forums.phpfreaks.com/topic/115664-problem-with-making-a-function-display/#findComment-594600 Share on other sites More sharing options...
kpetsche20 Posted July 20, 2008 Author Share Posted July 20, 2008 Yes error reporting is on. The file name is functions.php sorry. Even when I echo inside the loop nothing displays, the loop works fine without the function just coded on a regular page. Link to comment https://forums.phpfreaks.com/topic/115664-problem-with-making-a-function-display/#findComment-594601 Share on other sites More sharing options...
cooldude832 Posted July 20, 2008 Share Posted July 20, 2008 I honestly don't see a need for this function and I know your sql is error so unless you want to take my suggestions seriously then I suggest you figure it out on your own cause what you wrote is completely pointless. Link to comment https://forums.phpfreaks.com/topic/115664-problem-with-making-a-function-display/#findComment-594603 Share on other sites More sharing options...
kpetsche20 Posted July 20, 2008 Author Share Posted July 20, 2008 if there's an error in mysql why won't it display <?php function mywhile($par) { $sql = "SELECT * FROM ".$par.""; $sql2 = mysql_query($sql2) or die(mysql_error()); while($data = mysql_fetch_array($sql2)) { $name = $data['name']; echo $name; } } ?> Link to comment https://forums.phpfreaks.com/topic/115664-problem-with-making-a-function-display/#findComment-594609 Share on other sites More sharing options...
kpetsche20 Posted July 20, 2008 Author Share Posted July 20, 2008 The point of this is to gain a better understanding of functions Link to comment https://forums.phpfreaks.com/topic/115664-problem-with-making-a-function-display/#findComment-594610 Share on other sites More sharing options...
cooldude832 Posted July 20, 2008 Share Posted July 20, 2008 I love you mysql_query statement running a blank query returns an awesome set of data Link to comment https://forums.phpfreaks.com/topic/115664-problem-with-making-a-function-display/#findComment-594611 Share on other sites More sharing options...
kpetsche20 Posted July 20, 2008 Author Share Posted July 20, 2008 Why won't you just tell me how to fix, if you have nothing positive or helpful, don't post Link to comment https://forums.phpfreaks.com/topic/115664-problem-with-making-a-function-display/#findComment-594614 Share on other sites More sharing options...
cooldude832 Posted July 20, 2008 Share Posted July 20, 2008 better way to write your pointless function <?php function mysql_display($query){ $q = mysql_query(mysql_real_escape_string($q)) or die(mysql_error()."<br /><br />".$q); if(mysql_num_rows($r) >0){ while($row = mysql_fetch_assoc($r)){ print_r($row); } } else{ return FALSE; } } #example mysql_display("Select * from `Users`"); ?> Link to comment https://forums.phpfreaks.com/topic/115664-problem-with-making-a-function-display/#findComment-594620 Share on other sites More sharing options...
unkwntech Posted July 20, 2008 Share Posted July 20, 2008 Since cooldude832 being an ass. <?php function mywhile($par) { $sql = "SELECT * FROM ".$par.""; //make sure if $par comes from a user that at some point you sanitize it to prevent SQL injections $result = mysql_query($sql, $link); if(!$result){return 0;} //doing this so that when you call the function you can do if(!mywhile()){//do if query failed}{//do if query passes} while($data = mysql_fetch_array($sql2)) { $name = $data['name']; echo $name; } } ?> Link to comment https://forums.phpfreaks.com/topic/115664-problem-with-making-a-function-display/#findComment-594722 Share on other sites More sharing options...
cooldude832 Posted July 20, 2008 Share Posted July 20, 2008 obviously you didn't do proper checking so go ahead and think I"m being an ass when I'm trying to tell someone they wasting their time. Link to comment https://forums.phpfreaks.com/topic/115664-problem-with-making-a-function-display/#findComment-594847 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.