alconebay Posted July 7, 2009 Share Posted July 7, 2009 I have the following code that works fine: <?php include 'dbconnect.php'; $query = "SELECT * FROM users"; $result = mysql_query($query) or die(mysql_error()); $stng = mysql_fetch_array($result) or die(mysql_error()); echo $stng['name']; ?> This will output a name just fine. But, if I put the query into a function like this: <?php include 'dbconnect.php'; function pb_stng_selectall() { $query = "SELECT * FROM users"; $result = mysql_query($query) or die(mysql_error()); $stng = mysql_fetch_array($result) or die(mysql_error()); } ?> <?php pb_stng_selectall(); echo $stng['name']; ?> I will get a "Undefined variable" error instead of the name. Why is this? Quote Link to comment https://forums.phpfreaks.com/topic/165075-solved-putting-a-query-inside-a-function/ Share on other sites More sharing options...
p2grace Posted July 7, 2009 Share Posted July 7, 2009 Try this: <?php include 'dbconnect.php'; function pb_stng_selectall() { $query = "SELECT * FROM users"; $result = mysql_query($query) or die(mysql_error()); return = mysql_fetch_array($result) or die(mysql_error()); } ?> <?php echo pb_stng_selectall(); ?> Variables within functions are only accessible via the scope of the function. Values must be returned to allow external access. Quote Link to comment https://forums.phpfreaks.com/topic/165075-solved-putting-a-query-inside-a-function/#findComment-870460 Share on other sites More sharing options...
Mark Baker Posted July 7, 2009 Share Posted July 7, 2009 It helps if the function actually returns something Quote Link to comment https://forums.phpfreaks.com/topic/165075-solved-putting-a-query-inside-a-function/#findComment-870461 Share on other sites More sharing options...
Psycho Posted July 7, 2009 Share Posted July 7, 2009 Correction to p2grace's code. The function will return an array and you cannot echo an array: <?php include 'dbconnect.php'; function pb_stng_selectall() { $query = "SELECT * FROM users"; $result = mysql_query($query) or die(mysql_error()); $stng = mysql_fetch_array($result) or die(mysql_error()); return $stng; } ?> <?php $user = pb_stng_selectall(); echo $user['name']; ?> However, the functionality makes no sense. Why are you querying the entire table but only using the first record? You're not even using a sort to ensure you get a specific record. What are you wanting the function to return? It would make more sense to me if you were passing an ID to the function to get a specific record or at least returning a multidimensional array of all the records returned. A lot of inefficiency in the the above format. Quote Link to comment https://forums.phpfreaks.com/topic/165075-solved-putting-a-query-inside-a-function/#findComment-870463 Share on other sites More sharing options...
p2grace Posted July 7, 2009 Share Posted July 7, 2009 Thanks mjdamato for the fix, wasn't paying close enough attention mjdamato is also right in terms of the logic here, you want to only the values necessary for the output. Quote Link to comment https://forums.phpfreaks.com/topic/165075-solved-putting-a-query-inside-a-function/#findComment-870464 Share on other sites More sharing options...
chokies12 Posted July 7, 2009 Share Posted July 7, 2009 <?php include 'dbconnect.php'; function pb_stng_selectall($users) { $query = "SELECT * FROM users='$users'"; $result = mysql_query($query) or die(mysql_error()); $stng = mysql_fetch_array($result) or die(mysql_error()); echo = $stng['users']; } ?> <?php pb_stng_selectall(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/165075-solved-putting-a-query-inside-a-function/#findComment-870467 Share on other sites More sharing options...
alconebay Posted July 7, 2009 Author Share Posted July 7, 2009 I am writing a bunch of crud functions into one page that I will need to use across the site. I want to put them all in one page so I will not be writing the same queries over and over. That random name I'm trying to echo is just for testing purposes. I tried using this: $user = pb_stng_selectall(); echo $user['name']; and I also put a "hello world" inside the function but I just get a blank page with no errors. (I also turned error reporting on) Quote Link to comment https://forums.phpfreaks.com/topic/165075-solved-putting-a-query-inside-a-function/#findComment-870470 Share on other sites More sharing options...
p2grace Posted July 7, 2009 Share Posted July 7, 2009 Could you post your code? Quote Link to comment https://forums.phpfreaks.com/topic/165075-solved-putting-a-query-inside-a-function/#findComment-870472 Share on other sites More sharing options...
wildteen88 Posted July 7, 2009 Share Posted July 7, 2009 Look at mjdamato's post above. That is the correct syntax to use. Quote Link to comment https://forums.phpfreaks.com/topic/165075-solved-putting-a-query-inside-a-function/#findComment-870473 Share on other sites More sharing options...
alconebay Posted July 7, 2009 Author Share Posted July 7, 2009 ahhh, I missed the "return $stng;" in mjdamato code. That got me up and running. Thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/165075-solved-putting-a-query-inside-a-function/#findComment-870475 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.