php - Compare equality and count how many times the same string appears in a returned sql query -


i coding similar game cards against humanity , bit stuck on collating results using php, i'm new php have hit stumbling block.

so have game data in sql table on localhost , looks roughly: these column headers , input in table:

+------------+-----------------+------------+-----------------+ | firebaseid |     votes       | gamenumber |    submission   | +------------+-----------------+------------+-----------------+ |     id1    | option number 6 |   game 9   | option number 6 | |     id2    | option number 6 |   game 9   | option number 1 | |     id3    | option number 6 |   game 9   | option number 6 | |     id4    | option number 1 |   game 9   | option number 1 | |     id5    | option number 6 |   game 9   | option number 4 | +------------+-----------------+------------+-----------------+ 

where votes answer vote best , submission own submission card game. know values don't mean @ moment once thing working i'll enter different values each card.

my code this:

$conn = new mysqli($servername, $username, $password, $db);  // check connection if ($conn->connect_error) {     die("database connection failed: " . $conn->connect_error); }  $q = $_get['q'];  $sql="select votes, firebaseid cauusers gamenumber = '".$q."'";  $result = mysqli_query($conn,$sql);  $length = $result->num_rows;  if ($result->num_rows > 0) { // output data of each row     $myresarray = [];     $count = 0;      while($row = $result->fetch_assoc()) {         $myresarray[$count] = $row["votes"];         $count++;     }      //var_dump($myresarray);     $max = 0;     $winner = "";     $index = 1;     //$length = count($myresarray) (this prints 5);      for($i = 0; $i < $length;$i++){         for($j = 0; $j < $length; $j++){             if($myresarray[$i]===$myresarray[$j]){                 $max += 1;                 $winner = $myresarray[$i];              }         }     }     echo $winner  . "<br>";      $sqlsel="select firebaseid cauusers               gamenumber = '".$q."' , submission = '".$winner."'";      $submission = mysqli_query($conn,$sql);     echo var_dump($submission);      if ($submission->num_rows > 0) {         while($row = $submission->fetch_assoc()) {             echo $row["firebaseid"] . "<br>";         }     }  } else {      echo "0 results";  }  mysqli_close($conn);  ?> 

when print $q get:

game 9

when var_dump $myresarray get;

 array(5) {      [0] => string(15) "option number 6"     [1] => string(15) "option number 6"      [2] => string(15) "option number 6"      [3] => string(15) "option number 1"      [4] => string(15) "option number 6"  } 

so trying gather card voted , based on find out person (or people) submitted cards id's can award winners coins later use in game. can see retrieving data need can't seem access each individual string compare against each other. @ moment if there 2 answers voted same amount of times 1 answer returned.

it prints correct winning answer prints 5 firebase id's instead of the

any massively appreciated.

cheers

instead of coding logic in php, in sql. following query determine voted for, , return records match vote:

select *   cauusers  votes = (     select   votes         cauusers         gamenumber = 'game 9'     group votes     order count(*) desc     limit    1); 

see sql fiddle.

integrate in php, , you'll (not tested):

$sql = "select *           cauusers          votes = (             select   votes                 cauusers                 gamenumber = gamenumber = '$q'             group votes             order count(*) desc             limit    1)"; $result = mysqli_query($conn, $sql); if ($result->num_rows > 0) {     // output winners     $winner = "";     while($row = $result->fetch_assoc()) {         if ($winner=="") {             $winner = $row['votes'];             echo "winner: $winner<br>";         }         echo $row["firebaseid"] . "<br>";     } } else {     echo "0 results"; } 

one thing should still improve: better not inject $q in sql string, not when value provided user. vulnerable sql injection. better use prepared statements , parameters.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -