s3recap Posted May 23, 2010 Share Posted May 23, 2010 Im new to php trying to edit scripts and what not. Well basically Im trying to pull a certain number thats stored in my database. id name image game pet_desc pet_strength 12144 Werewolf 1 Rawr 0 12143 Zombie 1 Living dead 0 12142 Human 1 The average human. 0 12145 Vampire 1 I like blood 0 12146 Wizard 1 magic! 0 12147 Doggy Test 1 dgdfgdfgdgf 255 Im trying to get the specific number of 255. So when a user creates this character their strenght would be 255. <?php /* Process adoption (getpet.pro.php) */ ob_start(); $rank_check = 1; include "global.inc.php"; $findGetFirsts = fetch("SELECT * FROM pets2 WHERE game = '$game' AND id = '$species'"); $pet_str = fetch("SELECT $pet_strength FROM pets2 WHERE name = '$species'"); if (!$findGetFirsts[id]) { die(header(error("getpet.php?game=$game","Select a real pet."))); } if ($findGetFirsts[starter] == 0) { die(header(error("getpet.php?game=$game","Select a starter pet."))); } $num_pets = mysql_num_rows(mysql_query("SELECT * FROM user_pets2 WHERE owner = '$userid' AND game = '$game'")); if ($num_pets != 0) { die(header(error("index.php?game=$game","You already have a pet."))); } $check = fetch("SELECT * FROM user_pets2 WHERE name = '$petname' AND game = '$game'"); if ($check[name]) { die(header(error("getpet.php?game=$game&pet=$species","There is already a pet with that name. Try another name."))); } if ($petname == "") { die(header(error("getpet.php?game=$game&pet=$species","Don't leave the name blank."))); } if (preg_match('/^[a-zA-Z0-9_]*$/UD',$petname)) { mysql_query("INSERT INTO user_pets2 (name,species,owner,level,strength,intelligence,speed,hunger,timestamp,defense,current_hp,max_hp,game) VALUES ('$petname','$species','$userid','1','$pet_str','7','7','7','$timestamp','7','100','100','$game')"); } else { die(header("Location: $base_url/getpet.php?game=$game&error=" . space2plus("Your pet's name can only contain a-z, A-Z, 0-9 and underscores. Please try again."))); } die(header(error("mypets.php?game=$game","Congratulations, your new pet is named $petname."))); ?> $pet_str = fetch("SELECT $pet_strength FROM pets2 WHERE name = '$species'" i know its probably wrong but thats what i added. Quote Link to comment https://forums.phpfreaks.com/topic/202659-simple-noob-question/ Share on other sites More sharing options...
jd307 Posted June 10, 2010 Share Posted June 10, 2010 Maybe I am not understanding correctly, but from what I think I understand: A user creates a character. When that user creates a character, an SQL query looks up the "species" that they have chosen to find its strength and then inserts that into an SQL INSERT query. This is how I would do the SQL statement that you have given: $getPetData = mysql_query("SELECT * FROM pets2 WHERE name = '$species'") or die(mysql_error()); $petData = mysql_fetch_array($getPetData); You can now do one of two things, you could replace every occurance of $pet_str with $petData['pet_strength'] or you could write: $pet_str = $petData['pet_strength']; This would mean you wouldn't have to change any variables (so this might be the better option). Also, looking at your code; your SQL query that you have entered is almost identical to the one above it: $findGetFirsts = fetch("SELECT * FROM pets2 WHERE game = '$game' AND id = '$species'"); $pet_str = fetch("SELECT $pet_strength FROM pets2 WHERE name = '$species'"); Instead of creating your own SQL statement, can you not just do something like: $pet_str = $findGetFirsts[pet_strength]; On a final note, if you do decide to go down the route of using a second SQL statement, I notice that your statement ends with "WHERE name ='$species')" and the line above it uses "AND id = '$species'")". Looking at the table you have provided, the columns 'name' and 'id' contain completely different information (as expected) but you are using the same variable ($species) for both. The problem with this is if this variable contains an ID number for the id column, you will never find any results containing this information under the 'name' column, and vice versa if it contains a name, it will never find any information under the ID column. Quote Link to comment https://forums.phpfreaks.com/topic/202659-simple-noob-question/#findComment-1070195 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.