Jump to content

pass variable to mysql statement in foreach loop


phillyrob0817

Recommended Posts

Is there a way to use another variable  in a mysql select where statement for inside a foreach loop.

 

Ex:

 

<?php

 

This works fine.

 

    foreach ($range as $b)

            {

 

     $result = $conn->query("select count(*) as mycount1  from $table where date >= '$date' and (Ball1  = $b or Ball2  = $b or Ball3  = $b or Ball4  = $b or Ball5 = $b)");

 

      while ($row = $result->fetch_object()) {

       $count  = $row->mycount1;

       echo  $b." ".$count."<br>";

       }

 

      }

 

 

 

 

But I want to be able to use this variable string  below* in my statement so I can change what comes after the "and" in the statement.

Is there a way to do that?

 

 

    *$ballsB5 = ("Ball1  = $b or Ball2  = $b or Ball3  = $b or Ball4  = $b or Ball5 = $b");

 

 

    foreach ($range as $b)

            {

 

       $result = $conn->query("select count(*) as mycount1  from $table where date >= '$date' and $ballsB5");

 

      while ($row = $result->fetch_object()) {

       $count  = $row->mycount1;

       echo  $b." ".$count."<br>";

       }

 

      }

 

 

 

 

?>

Link to comment
Share on other sites

You shouldn't have columns ball1, ball2, ... ballN. You should normalize the data correctly so each ball has its own row - then you have a single column to search.

 

See this thread http://forums.phpfreaks.com/topic/295145-cannot-get-these-variables-to-add-up/?do=findComment&comment=1507842

Edited by Barand
Link to comment
Share on other sites

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.