Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/134438-solved-basic-function-question/
Share on other sites

<?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

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;
}

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;
}
?>

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)

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  ???

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)

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.