tsilenzio Posted August 3, 2007 Share Posted August 3, 2007 I know this is suppose to be about php code but I wasnt sure if i posted in the MySQL section if ppl there could answer the php code part anyway i have the following code and i am trying to reduce the amount of Querys i do to a minimum is there a way to make it into one query and do the php statments after? // <?php // <-- Enable Text Highlighting -- // $query = "SELECT RaceID FROM races"; $result = processQuery($query); $max_RaceID = mysql_field_len($result); $query = "SELECT FormID FROM forms"; $result = processQuery($query); $max_FormID = mysql_field_len($result); NOTE: processQuery is a function that connects to the Database, outputs any errors in the procces if there is any and executes the code if it makes it that far with no errors Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/ Share on other sites More sharing options...
Link Posted August 3, 2007 Share Posted August 3, 2007 I believe it goes like: $query = "SELECT races.RaceID, forms.FormID FROM races, forms"; $result = proccessQuery($query); $max_RaceID = mysql_field_len($result); although I am not sure what you are trying to accomplish but doing something like the above and then calling mysql_fetch_array(mysql_query($query)) will give you an array containing RaceID and FormID. It simplifies the query into one. Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314682 Share on other sites More sharing options...
teng84 Posted August 3, 2007 Share Posted August 3, 2007 I believe it goes like: $query = "SELECT races.RaceID, forms.FormID FROM races, forms"; $result = proccessQuery($query); $max_RaceID = mysql_field_len($result); although I am not sure what you are trying to accomplish but doing something like the above and then calling mysql_fetch_array(mysql_query($query)) will give you an array containing RaceID and FormID. It simplifies the query into one. your realy not sure with that thats wrong ^^^^ you wont get an error message with that but the output is not what is expected k tell us the relationship of that table if any Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314688 Share on other sites More sharing options...
Link Posted August 3, 2007 Share Posted August 3, 2007 It depends on what the functions do (processQuery) and what you are trying to accomplish. If you can be a bit more specific as to what you are trying to accomplish, I could perhaps give you code that will work. Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314690 Share on other sites More sharing options...
teng84 Posted August 3, 2007 Share Posted August 3, 2007 It depends on what the functions do (processQuery) and what you are trying to accomplish. If you can be a bit more specific as to what you are trying to accomplish, I could perhaps give you code that will work. but i should say this once a gain thats wrong theres no such thing as $query = "SELECT races.RaceID, forms.FormID FROM races, forms"; ok try to interpret the query you have and tell me where you got it wrong Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314691 Share on other sites More sharing options...
tsilenzio Posted August 3, 2007 Author Share Posted August 3, 2007 Okay i am making a php game with a few friends but at the time i am the only programmer. Anyway the game is based on Dragonball Z if you are fimilar with it. What your looking at is the transformation part (forms) and the race type (race - Humans, Demons.. so on) if you are unfimilar with Dragonball Z then all you need to know is race is what kind of [EDIT] creature [/EDIT] you are, Human, Monster, Semi-Human, so on.. and Transformation is what you can kind of evolve into. As for the reason of this is to find out how many entrys there are in the [EDIT] table [/EDIT] so i can perform a formula For instance right now there are 11 races, and 77 total transformations so 77 / 11 = 7 transformations per race however i made it possible to add races and transformations (each race has to have an equal amount of transformations as all the other races) so thats what this snippet does tel me how many there are of each I am sure i confused you all and lost you up there if i did let me know and ill try to explain it better thank you by the way for helping! Edit: Anything enclsoed in edits is typos i fixed Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314692 Share on other sites More sharing options...
Link Posted August 3, 2007 Share Posted August 3, 2007 Haha, I am a little confused (even though I used to watch Dragonball Z...I have to say it got too slow for my patience). Let's talk more code. So are you just looking to divide two numbers you pull from the database? Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314693 Share on other sites More sharing options...
teng84 Posted August 3, 2007 Share Posted August 3, 2007 PHP Statment and Querys - Possible To Sum Up? what do you mean by that ???? Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314695 Share on other sites More sharing options...
tsilenzio Posted August 3, 2007 Author Share Posted August 3, 2007 Yes but i am tryign to do it in a single query not 2 querys, this will also help me understand joins too kuz i suck at them and i cant fidn a good tutorial that shwos JOINS --AND-- PHP togather Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314696 Share on other sites More sharing options...
Link Posted August 3, 2007 Share Posted August 3, 2007 Ok, so what numbers exactly (in terms of MySQL) do you want to divide? Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314697 Share on other sites More sharing options...
tsilenzio Posted August 3, 2007 Author Share Posted August 3, 2007 Okay what i meant and wasnt able to fix due to modify time is I have 2 querys and i was wodnerign if i can make it into one query and still get the info from it Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314698 Share on other sites More sharing options...
tsilenzio Posted August 3, 2007 Author Share Posted August 3, 2007 I want to divide the number of rows in Forms by the number of rows in Races Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314699 Share on other sites More sharing options...
teng84 Posted August 3, 2007 Share Posted August 3, 2007 you can join table if it has a relationship read basic http://w3schools.com/sql/sql_join.asp Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314700 Share on other sites More sharing options...
tsilenzio Posted August 3, 2007 Author Share Posted August 3, 2007 thanx for the tut that saved me from what i was abotu to ask but still i dont know how to get the number of all the total rows in a table Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314702 Share on other sites More sharing options...
tsilenzio Posted August 3, 2007 Author Share Posted August 3, 2007 I am pretty sure now that i used the wrong function uptop.. Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314703 Share on other sites More sharing options...
teng84 Posted August 3, 2007 Share Posted August 3, 2007 you can have something like this $query = "SELECT count(FormID) as tottla_number FROM forms"; or you can use mysql_num_result sample $query = "SELECT RaceID FROM races"; echo mysql_num_result(mysq_query($query)); Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314705 Share on other sites More sharing options...
Barand Posted August 3, 2007 Share Posted August 3, 2007 but i should say this once a gain thats wrong theres no such thing as $query = "SELECT races.RaceID, forms.FormID FROM races, forms"; Yes there is. It's called a "cartesian join". Because no join condition is specified it joins every row in races with every row in forms. So if you 50 rows in races and 100 rows in forms you get all 5,000 combinations returned. This probably isn't the most efficient query ever written, but <?php $query = "SELECT COUNT(DISTINCT races.RaceID) as numraces, COUNT(DISTINCT forms.FormID) as numforms FROM races, forms"; $res = mysql_query($query) or die (mysql_error()."<p>$sql</p>"); echo "Transforms per race = ", mysql_result($res, 0, 'numforms') / mysql_result($res, 0, 'numraces'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314823 Share on other sites More sharing options...
chocopi Posted August 3, 2007 Share Posted August 3, 2007 EDIT: Looks like I was beaten to it, twice seeing as i missed the second page. My bad Quote Link to comment https://forums.phpfreaks.com/topic/63146-php-statment-and-querys-possible-to-sum-up/#findComment-314856 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.