1

I have 4 columns in my database. Atk,Str,Dex,and Con. Each being INT and then I have a PHP function get_level that takes those 4 columns and determines the correct level for them. I'm trying to select all users in a given range based on these "levels". The get_combat function takes those 4 values and finds the users combat level. This is what I have, which doesn't work.

<?PHP 
$query = "SELECT get_level(attack) as atk,
    get_level(strength) as str,
    get_level(dexterity) as dex,
    get_level(constitution) as con,
    get_combat(atk,str,dex,con) as level 
    FROM `users` 
    WHERE level > 5 AND level < 10"; 
?>

Is there a way to do this?

2 Answers 2

3

What you are trying to do is impossible. MySQL doesn't even know what PHP is or which language is talking to it.

Why not fetch the values and call your function on in in the loop where you iterate over the results?

Depending on how complex the function is you could also create a stored procedure in your database. You cannot write PHP code in that function though.

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

8 Comments

If there are thousands of users to loop through. Then to find which is the in the range after that is kinda crazy isn't it? Can I not build my own functions in MySql?
Umm but you apply your function on the fetched values anyway, not on the WHERE criterion. So the number of results is not related to your function... But as I said, you can create stored procedures in MySQL.
Ah, I didn't notice that level is not a column... well, in that case use a stored procedure. We might be able to help you in a better way if you posted the code of your get_combat function.
Its just a simple formula of the four other columns.
Then you can even put it directly in your WHERE statement instead of using a function.
|
1

I think you can't do something like this in SQL. After executing the query you can use the functions. Assuming you are using the old mysql_* functions:

<?php
$result = mysql_query($query);
$newResults = array();
$i = 0;

while( $row = mysql_fetch_assoc($result) ) {
  $newResults[$i]['atk'] = get_level($row['attack']);
  $newResults[$i]['strength'] = get_level($row['strength']);
  /** ect. **/
  $newResults[$i]['level'] = get_combat($newResults['atk'],$newResults['str'],$newResults['dex'],$newResults['con']);
}

1 Comment

But making a new array for all users in a database is redundant isn't it? Just sayin.

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.