0

I'm trying to remove redundancy in one of my files. I have 30 to 40 queries---of 2 different types---that run based on the date. I tried to avoid repeating the same (of 2 different) query strings by putting them in a function and calling the function based on the date. It doesn't seem to work as I expected/hoped. I'm new to PHP and maybe I've overlooked something or don't understand how PHP functions work.

Here' a sample of the code:

function queryONE() { 
    $stmt = $pdo->query("SELECT `rand` FROM `setGallery` WHERE `$category` = '$dayList' ORDER BY RAND() LIMIT $limit"); 
    $fh = fopen("galleryRand_PDO.txt", "w"); 
    while ($row = $stmt->fetchObject()) {
        fwrite($fh, $row->rand);
    } 

    fclose($fh); 

    exit();
}

function queryTWO() { 
    $stmt = $pdo->query("(SELECT `rand` FROM `jukebox2014` WHERE `$category` = '$dayList' ORDER BY RAND() LIMIT $limit) ORDER BY `rand` DESC"; 
    $fh = fopen("galleryRand_PDO.txt", "w"); 

    while ($row = $stmt->fetchObject()) {
        fwrite($fh, $row->rand);
    }

    fclose($fh); 

    exit(); 
}

if ($date == Jun 13)
{
    $category = "galleryDir"; 
    $dayList = "Grids";
    queryONE();
} 

if ($date == Nov 16) {
    $category = "class"; 
    $dayList = "Grids"; 
    queryTWO();
}

Note: If I put an --- echo "Hello world!" --- statement in the function it does display; so the if() statement is firing the function, but the function isn't running the query, but the fopen code DOES create the expected text file.

Obviously there are a lot more if() statements or I wouldn't bother. Any ideas why this won't work?

Thanks.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Pavlis
  • 3
  • 2
  • Please format your code properly – Rizier123 Nov 15 '14 at 22:38
  • Enable proper [error reporting](http://php.net/manual/en/errorfunc.configuration.php#ini.error-reporting); read up on [variable scope](http://php.net/manual/en/language.variables.scope.php). – CBroe Nov 15 '14 at 22:38
  • How do you know the query isn't run? What exactly do you mean? Is the result set empty? – 11684 Nov 15 '14 at 22:42
  • And read up on function parameters. http://php.net/manual/en/functions.arguments.php – 11684 Nov 15 '14 at 22:44
  • I assume it's empty because nothing gets output to the text file. When I run either of the queries outside the functions, the text file is populated---so I know the query code is working. – Pavlis Nov 15 '14 at 22:44
  • I think the query is run, but the `$category` and `$dayList` aren't inserted. This has to do with variable scope, as CBroe already suggested, you should read about that. If you give your functions parameters instead, this will work fine. I'll leave it to CBroe to write an answer (and gain reputation points) since he pointed out the issue first. – 11684 Nov 15 '14 at 22:48

3 Answers3

0

Try passing the date as a parameter to your functions, something like this:

function queryONE($category, $dayList) { 
    $stmt = $pdo->query("SELECT `rand` FROM `setGallery` WHERE `{$category}` = '{$dayList}' ORDER BY RAND() LIMIT $limit"); 
    $fh = fopen("galleryRand_PDO.txt", "w"); 
    while ($row = $stmt->fetchObject()) {
        fwrite($fh, $row->rand);
    } 

    fclose($fh); 

    exit();
}

then call the method:

 if ($date == Jun 13)
{
    queryONE("galleryDir", "Grids");
} 
11684
  • 7,356
  • 12
  • 48
  • 71
Jorge
  • 227
  • 1
  • 3
  • Category doesn't make it to the function this way. Also, there is no point in setting `$dayList` in the if. (This goes for `$category` too.) – 11684 Nov 15 '14 at 22:49
0

The queries fail because the variables don't have data. If you echo the contents of the query before you execute it, you will see something like:

"(SELECT `rand` FROM `jukebox2014` WHERE `` = '' ORDER BY RAND() LIMIT ) ORDER BY `rand` DESC"

This is because the functions queryOne and queryTwo know nothing about those variables; they are out of scope.

To make the variables in scope for those functions, you need to pass them as parameters. First, modify the function definitions to accept parameters:

function queryONE($category, $daylist, $limit) { ...

and

function queryTWO($category, $daylist, $limit) { ...

Then modify the line that calls the functions to pass the parameters:

queryONE($category, $dayList, $limit);
queryTWO($category, $dayList, $limit);

Now the variables will be available to the functions ("in scope"). Your code doesn't show where you define $limit, but make sure that it is in scope when you call the new functions.

Finally, consider turning on error reporting. The PHP interpreter will warn you about these types of things if you let it.

Community
  • 1
  • 1
George Cummins
  • 28,485
  • 8
  • 71
  • 90
  • 1
    I'm not sure why this answer has a downvote (since all answers have one I suspect a troll), but this answer 1) explains the issue 2) gives a fix and 3) shows how to prevent it in the future. +1. – 11684 Nov 16 '14 at 22:32
-1

You want to pass the category and day list into the functions:

queryONE("galleryDir", "Grids");
queryONE("class", "Grids");

and define the functions accordingly:

function queryONE($category, $dayList) { 
    $stmt = $pdo->query("SELECT `rand` FROM `setGallery` WHERE `$category` = '$dayList' ORDER BY RAND() LIMIT $limit"); 
    $fh = fopen("galleryRand_PDO.txt", "w"); 
    while ($row = $stmt->fetchObject()) {
        fwrite($fh, $row->rand);
    } 

    fclose($fh); 

    exit();
}

function queryTWO($category, $dayList) { 
    $stmt = $pdo->query("(SELECT `rand` FROM `jukebox2014` WHERE `$category` = '$dayList' ORDER BY RAND() LIMIT $limit) ORDER BY `rand` DESC"; 
    $fh = fopen("galleryRand_PDO.txt", "w"); 

    while ($row = $stmt->fetchObject()) {
        fwrite($fh, $row->rand);
    }

    fclose($fh); 

    exit(); 
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • I THINK I've implemented your suggestions. But they don't seem to work. if ($today == "Nov 15"){ $category = "images"; $dayList = "roses"; $limit = 1; queryONE($category, $dayList, $limit); } function queryONE($category, $dayList, $limit) { echo "help!"; $stmt = $pdo->query("SELECT `rand` FROM `setGallery` WHERE `$category` = '$dayList' ORDER BY RAND() LIMIT $limit"); $fh = fopen("galleryRand_PDO.txt", "w"); while ($row = $stmt->fetchObject()) { fwrite($fh, $row->rand); } fclose($fh); exit(); } Without the function-related code, it works.Thanks. – Pavlis Nov 16 '14 at 00:22