Jump to content

Archived

This topic is now archived and is closed to further replies.

uha666

Query SELECT problem

Recommended Posts

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. =/

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. =/

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[!--quoteo(post=383316:date=Jun 13 2006, 10:22 AM:name=radalin)--][div class=\'quotetop\']QUOTE(radalin @ Jun 13 2006, 10:22 AM) [snapback]383316[/snapback][/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.

Share this post


Link to post
Share on other sites
[!--quoteo(post=384254:date=Jun 15 2006, 12:19 PM:name=Dex)--][div class=\'quotetop\']QUOTE(Dex @ Jun 15 2006, 12:19 PM) [snapback]384254[/snapback][/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.

Share this post


Link to post
Share on other sites

×

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.