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.

1

2 Answers 2

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".

Sign up to request clarification or add additional context in comments.

Comments

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();
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.