BoJack Horseman
BoJack Horseman

Reputation: 169

SQL QUERY SELECT INSIDE A FUNCTION

I am trying to work with a voting database system. I have a form to display all the candidates per candidate type. I'm still trying to to explore that. But this time, I want to try one candidate type, lets say for Chairperson, I want to display all candidate names for that type in the ballot form. However, there's an error with the line where i declare $query and made query statements, can somebody know what it is. I am very certain that my syntax is correct.

  function returnAllFromTable($table) {
          include 'connect.php';
          $data = array ();
          $query = 'SELECT * FROM ' . $table. 'WHERE candidateId=1'; //ERROR
          $mysql_query = mysql_query ( $query, $conn );

          if (! $mysql_query) {
            die ( '<a href="../../">Go Back</a><br>Unable to retrieve data from table ' . $table );
          } else {
            while ( $row = mysql_fetch_array ( $mysql_query ) ) {
              $data [] = $row;
            }
          }

          return $data;
        }

Upvotes: 1

Views: 612

Answers (1)

Adrian Cid Almaguer
Adrian Cid Almaguer

Reputation: 7791

As @Darwin von Corax says, I sure that you have a problem between $table and WHERE

Your query:

$query = 'SELECT * FROM ' . $table. 'WHERE candidateId=1';

If $table = 'Chairperson';

You have:

'SELECT * FROM ChairpersonWHERE candidateId=1';

The your query should be:

 $query = 'SELECT * FROM ' . $table. ' WHERE candidateId=1';

Upvotes: 1

Related Questions