Author Topic: Simple noob question  (Read 369 times)

0 Members and 1 Guest are viewing this topic.

Offline s3recapTopic starter

  • Irregular
  • Posts: 1
    • View Profile
Simple noob question
« on: May 23, 2010, 01:39:38 PM »
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.
« Last Edit: May 23, 2010, 01:45:36 PM by s3recap »

Offline jd307

  • Enthusiast
  • Posts: 83
    • View Profile
Re: Simple noob question
« Reply #1 on: June 09, 2010, 10:13:18 PM »
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.