There are a number of misconceptions in your DoQuery class code. I'll try to outline them below:
1) A class should represent a concept, not an action. Functions and methods represent actions e.g. doQuery. Also 'do' is a vague word, you should try to be more specific. An improvement would be class 'Query' and function 'print', leading to: $query = new Query; $query->print();
2) You are using a class in a roundabout way to return a value, it would be better just to use a function.
3) $sql[$qid] is an array however you are using it like a variable. Only one value is ever stored in $sql because its deleted at the end of the function call. Its the same for $result[$qid], $a[0] and $num[0]
4) A switch statement is designed to select between multiple sections of code based on an input. You should be using an if statement to conditionally select the code (presuming that is your intention).
5) You don't need to break from a switch statement, you only need break to prematurely exit from loops (for, while, foreach, etc.)
Here's my version of your code, it does exactly the same thing:
Code: Select all
function displayQuery($qid, $table, $row, $where) {
$result = mysql_query("SELECT * FROM " .$table. " WHERE " .$row. " = '$where'");
if ($qid == 0) {
for ($a = 0; $a < mysql_numrows($result); $a++) {
$playerId = mysql_result($result,$a,"playerid");
}
return $playerId;
}
}
//Example usage
$playerid = displayquery(0, "players", "player", $myplayername);
It will get the result. Then if $qid equals 0 it will iterate through each result and store playerId. Only the last playerId is stored (it is overwritten each time). Somehow I don't think this is what you wish to achieve.