0

Now this is the scenario I have voting system in php, so I want to display the candidates based on the organization so after that if the voters log in to the system based on there organization. Just comparison of org_id.

This is my sample mysql query.

$result = mysql_query("SELECT * FROM candidates WHERE position='President' and org_name='IntOrg'");

Now the question is how to loop a query to select and depends on the org_id of candidates.

VeeeneX
  • 1,556
  • 18
  • 26
AiX En
  • 31
  • 1
  • 8
  • possible duplicate of [How to loop through a mysql result set](http://stackoverflow.com/questions/1756743/how-to-loop-through-a-mysql-result-set) – VeeeneX Feb 07 '15 at 10:52

2 Answers2

2

That's not how you usually do SQL. You would simply get all the candidates, order them by org_name and then iterate through the result.

But maybe I'm misunderstanding you and your question is actually "how to do a loop in PHP", in which case I'd say "read a PHP tutorial".

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
0

Example (MySQLi Object-oriented) source

And if you want to prevent 'hacking' in the DB, you should read about Mysql Injection and use PDO or Mysqli insted of Mysql, which is depreciated.

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM candidates WHERE position='President' and org_name='IntOrg'";
$result = $conn->query($sql);
/* Check if you have result */
if ($result->num_rows > 0) {
    // output data of each row
    /* This is a loop */
    while($row = $result->fetch_assoc()) {
        echo  $row["column name"]."<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>
Community
  • 1
  • 1
VeeeneX
  • 1,556
  • 18
  • 26