9 Replies - 605 Views - Last Post: 30 June 2010 - 05:16 AM Rate Topic: -----

#1 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

echoing into a mysql statement ?

Posted 30 June 2010 - 04:45 AM

I have tried two Google this be for i posted on here but couldn't find nothing


i am doing a select two get there users pokemon id

		$sql = "SELECT * FROM user_pokemon WHERE username='$_SESSION[username]'";
$result = mysql_query($sql) or die(mysql_error());
$battle_get = mysql_fetch_array($result);

 echo $battle_get['pok1']; 





This works fine and prints it out
but now i wanna match there poke1 id up with the id of the pokemon in the main pokemon table

$sql1 = "SELECT pokeID FROM pokemon WHERE pokeID=='$battle_get['pok1']'";
$result1 = mysql_query($sql) or die(mysql_error());
$battle_get111 = mysql_fetch_array($result);


 echo $battle_get11['pokeID']; 




Why don't this work ? i think i need two post the data into the were ?

Is This A Good Question/Topic? -1
  • +

Replies To: echoing into a mysql statement ?

#2 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5246
  • View blog
  • Posts: 27,062
  • Joined: 10-May 07

Re: echoing into a mysql statement ?

Posted 30 June 2010 - 04:47 AM

It doesn't work because of the variable array in the where statement. The single quotes for the array are interfering with the single quotes marking the where value. Just remove them from the array.

$sql1 = "SELECT pokeID FROM pokemon WHERE pokeID='$battle_get[pok1]'";
$result1 = mysql_query($sql) or die(mysql_error());
$battle_get111 = mysql_fetch_array($result);



You only need them if your array name has a space. Also the double equals was wrong...
Was This Post Helpful? 2
  • +
  • -

#3 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: echoing into a mysql statement ?

Posted 30 June 2010 - 04:50 AM

Didn't you say you'd stop posting here a long time ago?

Anyway, you don't use logical equal signs in MySQL. Also, reading your query makes my eyes hurt because of the lack of separation.

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#4 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: echoing into a mysql statement ?

Posted 30 June 2010 - 04:51 AM

View Postno2pencil, on 30 June 2010 - 03:47 AM, said:

It doesn't work because of the variable array in the where statement. The single quotes for the array are interfering with the single quotes marking the where value. Just remove them from the array.

$sql1 = "SELECT pokeID FROM pokemon WHERE pokeID='$battle_get[pok1]'";
$result1 = mysql_query($sql) or die(mysql_error());
$battle_get111 = mysql_fetch_array($result);



You only need them if your array name has a space. Also the double equals was wrong...



well it is only showing the first echo and not the second

$sql1 = "SELECT pokeID FROM pokemon WHERE pokeID=='$battle_get[pok1]'";
	$result1 = mysql_query($sql) or die(mysql_error());
	$battle_get111 = mysql_fetch_array($result);

 echo $battle_get111['pokeID']; 



That's what i got for the second that should echo out the pokeID ?
Was This Post Helpful? -1
  • +
  • -

#5 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3521
  • View blog
  • Posts: 10,161
  • Joined: 08-June 10

Re: echoing into a mysql statement ?

Posted 30 June 2010 - 04:52 AM

your SQL beats any logic, why would you get the exact same field, that you use in the WHERE clause? you wouldn’t need the SQL at all (unless you fetch more data).

your SQL is susceptible to SQL Injection.

I recommend using a JOIN, so you can directly query the main table with the user name.
# this involves some guessing on my side since I don’t know the table structures
SELECT pm.pokeID FROM pokemon AS pm JOIN user_pokemon AS upm ON upm.pok1 = pm.pokeID AND upm.username = ?

This post has been edited by Dormilich: 30 June 2010 - 04:53 AM

Was This Post Helpful? 1
  • +
  • -

#6 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: echoing into a mysql statement ?

Posted 30 June 2010 - 04:52 AM

Your first code might work, but that doesn't mean it's right. His code would have fixed its structure and its problem. Once again, you DO NOT use logical equal signs in MySQL.

Use:

Quote

=

NOT:

Quote

==


Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#7 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: echoing into a mysql statement ?

Posted 30 June 2010 - 04:56 AM

i just wanna get the pok1 id from the user_pokemon table

then match it with the pokeID from the pokemon table two get what pokemon they got etc ...


		$sql = "SELECT * FROM user_pokemon WHERE username='$_SESSION[username]'";
$result = mysql_query($sql) or die(mysql_error());
$battle_get = mysql_fetch_array($result);

 echo $battle_get['pok1']; 
 

$sql1 = "SELECT * FROM pokemon WHERE pokeID=='$battle_get[pok1]'";
	$result1 = mysql_query($sql) or die(mysql_error());
	$battle_get111 = mysql_fetch_array($result);

 echo $battle_get111['pokeNAME']; 



ive edited it a bit but the 2nd sql ent working any resosn why ?? no white page or nothing tho

This post has been edited by nick1200: 30 June 2010 - 05:11 AM

Was This Post Helpful? -2
  • +
  • -

#8 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: echoing into a mysql statement ?

Posted 30 June 2010 - 04:58 AM

Did you even try to change your code?
I seriously doubt it. Like always you just want somebody to do your work for you. They even did and it doesn't even look like you attempted to give it a shot.

I wish you stayed away like you said you would.

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5246
  • View blog
  • Posts: 27,062
  • Joined: 10-May 07

Re: echoing into a mysql statement ?

Posted 30 June 2010 - 05:11 AM

For the life of me, I don't know what is so difficult about reading & then using copy & paste for the suggested code.

You always do this, you post a question, along with example code, we show you what's wrong & you insist on using the code that we explain why it's wrong.

View Postnick1200, on 30 June 2010 - 05:56 AM, said:

Don't worry i new this was the wrong place two post :whistling:


We can't fix stupid.

You ask, we answer, you don't do as we suggest. Not our fault.

Also it's "to", not "two".
Was This Post Helpful? 1
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2927
  • View blog
  • Posts: 10,114
  • Joined: 08-August 08

Re: echoing into a mysql statement ?

Posted 30 June 2010 - 05:16 AM

...and it's "knew", not "new", as well as "want to", not "wanna", but who's counting? :whistling:

Nick, you need to pay attention to what you're doing, and especially to the advice you're given. Your poor grammar/spelling is just the tip of the iceberg.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1