Jump to content


Photo

Query SELECT problem


  • Please log in to reply
5 replies to this topic

#1 uha666

uha666
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 13 June 2006 - 04:20 AM

I have a simples code a wanted to implement that as been giving me some headaches

I have this query:



$result=mysql_query("
select * from char
where ". getStat('name') ." = 1 ");


< (char and name are table names) >



In wich i'm trying to call out this function:


function getStat($pname){

$query = "SELECT `data` FROM `char` WHERE `name`='$pname'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$pdata = mysql_fetch_array($result);

$pinfo = explode(" ", $pdata[0]);
return $pinfo[34];
}


The function works perfect in other sections of my site. I just can't seem to get the correct syntax on the query so that it does the compare with the value 1 correctly. Each time i run the query it never returns any valid rows although there are some. Is it because i do a function with a query inside another query? Is there any way i can make this work?

Thanks in advance. =/

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 13 June 2006 - 05:15 AM

I guess the first question is why you're not doing the whole thing in MySQL -- no need to get PHP involved. Second question -- [34]? Are you sure that's what you mean -- you want the 33rd field? I'm very confused.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 uha666

uha666
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 13 June 2006 - 01:38 PM

Don't mind the function cause that's working fine. I'm using php cause this is part of a website so i need it to be displayed in a browser. my only problem is that i must have some error in the way i do the query cause it never returns values from the function, i think i might have the syntax wrong. =/

#4 radalin

radalin
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 13 June 2006 - 03:22 PM

What is the thing that works ok? I didn't get it because you mean that everything is fine with the code at other parts but there is problem with this one? So it's more then a variable problem than a syntax or anything else problem. Because syntax errors returns errors. put an echo command for your qeury just before sending them to the db. You will probably remark that a variable is missing somewhere. Probably you will see:

SELECT `data` FROM `char` WHERE `name`=''

but I'm not very sure.
Roy Simkes
Yet Another Parkyeri Developer

#5 uha666

uha666
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 15 June 2006 - 04:19 PM

[!--quoteo(post=383316:date=Jun 13 2006, 10:22 AM:name=radalin)--][div class=\'quotetop\']QUOTE(radalin @ Jun 13 2006, 10:22 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
What is the thing that works ok? I didn't get it because you mean that everything is fine with the code at other parts but there is problem with this one? So it's more then a variable problem than a syntax or anything else problem. Because syntax errors returns errors. put an echo command for your qeury just before sending them to the db. You will probably remark that a variable is missing somewhere. Probably you will see:

SELECT `data` FROM `char` WHERE `name`=''

but I'm not very sure.
[/quote]


I just want to know what's the correct syntax to use a function inside a query so i can compare the return value of that function with the value 1.

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 15 June 2006 - 05:20 PM

[!--quoteo(post=384254:date=Jun 15 2006, 12:19 PM:name=Dex)--][div class=\'quotetop\']QUOTE(Dex @ Jun 15 2006, 12:19 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I just want to know what's the correct syntax to use a function inside a query so i can compare the return value of that function with the value 1.
[/quote]
There's no "correct" syntax -- you're breaking out a string and concatenating the result of a PHP function, no different than in any other part of PHP. MySQL doesn't "know" how you're put the query together. I don't understand your question. Echo of values of both $query variables before you issue them, and you'll see the problem.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users