gray8110 Posted November 27, 2008 Share Posted November 27, 2008 So, first, let me apologize for being the annoying class of PHP noob that knows just enough to be stupid instead of merely ignorant. I have a MySQL query that I need to include in a function. When I call the function, I'd like to be able to declare the value of a variable inside the function. Here's a snippet of the code: function newsQuery() $result = mysql_query("SELECT * FROM sv_newsbrief ORDER BY briefID DESC LIMIT $limitValue,1", $connection); I'm leaving out some of the query, but the relevent stuff is there. When I call newsQuery, I need to be able to the value of $limitValue (for that instance of the function only). So, how do I do that? I'm usually pretty good about searching for this sort of help, but I don't know what to search for in this case. Quote Link to comment https://forums.phpfreaks.com/topic/134438-solved-basic-function-question/ Share on other sites More sharing options...
genericnumber1 Posted November 27, 2008 Share Posted November 27, 2008 <?php function newsQuery($limitValue, $connection) { return mysql_query("SELECT * FROM sv_newsbrief ORDER BY briefID DESC LIMIT $limitValue,1", $connection); } $result = newsQuery($limitValue, $connection); functions can't access variables outside of their block of code, and the main script can't access values inside of the function unless they are returned. http://us.php.net/functions Quote Link to comment https://forums.phpfreaks.com/topic/134438-solved-basic-function-question/#findComment-699895 Share on other sites More sharing options...
gray8110 Posted November 27, 2008 Author Share Posted November 27, 2008 Hmm, that code makes sense, but it doesn't return a value. Ok, maybe if I look at it this way... I need to execute this block of code a bunch of times (12 to be exact.) If I were to repeatedly use this block, the only value that would change from one instance to another would be the Limit Value of 2 in the query. How can change that value without repeating the whole block every time? <?php $result = mysql_query("SELECT * FROM sv_newsbrief ORDER BY briefID DESC LIMIT 2,1", $connection); if (!result) { die("Database query failed: " . mysql_error()); } while ($row = mysql_fetch_array($result)) { $briefID = $row["ID"]; $open = $row["open"]; $in_issue = $row["in_issue"]; $item1 = $row["item1"]; $item2 = $row["item2"]; $item3 = $row["item3"]; $close = $row["close"]; $brief = $item1 . $item2 . $item3; echo $brief; } Quote Link to comment https://forums.phpfreaks.com/topic/134438-solved-basic-function-question/#findComment-699940 Share on other sites More sharing options...
genericnumber1 Posted November 27, 2008 Share Posted November 27, 2008 Why would you need to execute it that many times? Quote Link to comment https://forums.phpfreaks.com/topic/134438-solved-basic-function-question/#findComment-699943 Share on other sites More sharing options...
DeanWhitehouse Posted November 27, 2008 Share Posted November 27, 2008 could use a loop to run it 12 times. Quote Link to comment https://forums.phpfreaks.com/topic/134438-solved-basic-function-question/#findComment-699947 Share on other sites More sharing options...
Maq Posted November 27, 2008 Share Posted November 27, 2008 Put it in a function and use the for loop like blade suggested: $connection = //whatever your connect string is...; for($x=0; $x{ doQ($x, $connection); } function doQ($limit, $connection) { $result = mysql_query("SELECT * FROM sv_newsbrief ORDER BY briefID DESC $limit,1", $connection); if (!result) { die("Database query failed: " . mysql_error()); } while ($row = mysql_fetch_array($result)) { $briefID = $row["ID"]; $open = $row["open"]; $in_issue = $row["in_issue"]; $item1 = $row["item1"]; $item2 = $row["item2"]; $item3 = $row["item3"]; $close = $row["close"]; $brief = $item1 . $item2 . $item3; echo $brief; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/134438-solved-basic-function-question/#findComment-699968 Share on other sites More sharing options...
gray8110 Posted November 27, 2008 Author Share Posted November 27, 2008 Why would you need to execute it that many times? I don't... I just had an epiphany... I was so focussed on the individual result of the query, that wasn't thinking about how I could use a bigger range of results. Thanks (for helping me think through it more clearly) Quote Link to comment https://forums.phpfreaks.com/topic/134438-solved-basic-function-question/#findComment-699971 Share on other sites More sharing options...
Maq Posted November 27, 2008 Share Posted November 27, 2008 Why would you need to execute it that many times? I don't... I just had an epiphany... I was so focussed on the individual result of the query, that wasn't thinking about how I could use a bigger range of results. Thanks (for helping me think through it more clearly) I'm confused ??? Quote Link to comment https://forums.phpfreaks.com/topic/134438-solved-basic-function-question/#findComment-699972 Share on other sites More sharing options...
DeanWhitehouse Posted November 27, 2008 Share Posted November 27, 2008 I think he is saying he doesn't have a clue why he wanted to run it that many times :s Topic Solved? Bottom Left Quote Link to comment https://forums.phpfreaks.com/topic/134438-solved-basic-function-question/#findComment-699973 Share on other sites More sharing options...
gray8110 Posted November 27, 2008 Author Share Posted November 27, 2008 Yes, Topic solved.. at least until I have another brain fart. FWIW, I know WHY I was thinking I needed to run it 12 times - I need to manipulate the 12 most recent entries in a table. For some reason my mind was stuck on getting each result individually instead of querying for the range. (AKA I feel like an idiot) Quote Link to comment https://forums.phpfreaks.com/topic/134438-solved-basic-function-question/#findComment-699985 Share on other sites More sharing options...
Maq Posted November 27, 2008 Share Posted November 27, 2008 I need to manipulate the 12 most recent entries in a table. Lol, that's the whole point of ORDER BY DESC and LIMIT. You should have said that from the beginning, at least you realized how to solve this problem. Quote Link to comment https://forums.phpfreaks.com/topic/134438-solved-basic-function-question/#findComment-699989 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.