Jump to content

PHP Statment and Querys - Possible To Sum Up?


tsilenzio

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.