snk Posted January 12, 2008 Share Posted January 12, 2008 Hello, What code do I use when I know that a query returns just ONE record? for many I know that i fetch an array and I use do{...}while I want to write better code, I dont want to use do / while. This is what im doing now, and it looks stupid :s Quote Link to comment Share on other sites More sharing options...
asmith Posted January 12, 2008 Share Posted January 12, 2008 what exactly do you want to do ? your fetch result has more than one row , but you want to show only one of them ? Quote Link to comment Share on other sites More sharing options...
snk Posted January 12, 2008 Author Share Posted January 12, 2008 thx for replying, I use a query that returns just one result. lets say $nameQ = "select name from people where key= '$value'"; key is unique.. so I know that only one result will come back The question is how do I assign the name to a variable? I used to use mysql_fetch_array($nameQ); and then do/while. Is there any way to place it a variable, but not array? Quote Link to comment Share on other sites More sharing options...
asmith Posted January 12, 2008 Share Posted January 12, 2008 i see your point . well i don't know! just letting you know mysql_fetch_array do return an array that each value is repeated 2 times ! for example the mysql_fetch_array for you sql line will be : array([0] => "the_value_in_name_field" , [name] => "the_value_in_name_field"); however it is not important, you don't have to use do while ... do this : <?php $nameQ = "select name from people where key= '$value'"; $result = mysql_query($nameQ); $name_array = mysql_fetch_array($result); // now you name is into $name_array[name] and $name_array[0] $name = $name_array[name]; ?> as you see no do while . you can use mysql_fetch_assoc too : http://ir2.php.net/manual/en/function.mysql-fetch-assoc.php i guess it won't return a repeated values . (not sure) Quote Link to comment Share on other sites More sharing options...
twostars Posted January 12, 2008 Share Posted January 12, 2008 Add a "LIMIT 1" to the end of your query. Anyhow, $query = "SELECT * FROM people WHERE key='$value' LIMIT 1"; # Define query $result = mysql_query($query); # Use query to return a result $row = mysql_fetch_array($result); # Return a $row array, with keys matching the table's column names $name = $row['name']; # Set the value of $name to the value of name column in the database (you could use $row['name'] itself though) $other = $row['other']; # Set the value of $other to the value of other column in the database ^ $more = $row['more']; # Set the value of $more to the value of more column in the database ^ Simple, no? Quote Link to comment Share on other sites More sharing options...
snk Posted January 12, 2008 Author Share Posted January 12, 2008 @Asmith I have tried and doesnt work @twostars I dont need LIMIT 1 I said that the query returns 1 result anyway Quote Link to comment Share on other sites More sharing options...
asmith Posted January 12, 2008 Share Posted January 12, 2008 post your final code Quote Link to comment Share on other sites More sharing options...
twostars Posted January 12, 2008 Share Posted January 12, 2008 @Asmith I have tried and doesnt work @twostars I dont need LIMIT 1 I said that the query returns 1 result anyway May not be needed, but its also helpful to ensure you're getting what you want. =] Quote Link to comment Share on other sites More sharing options...
Lukela Posted January 12, 2008 Share Posted January 12, 2008 $sql="SELECT `column` FROM `table` WHERE `column`='value'" or die(mysql_error()); $query=mysql_query($sql); $results=mysql_fetch_assoc($query); Just use this. Quote Link to comment Share on other sites More sharing options...
snk Posted January 12, 2008 Author Share Posted January 12, 2008 @lukela assoc is almost the same with array, according the official php manual the answer is here http://uk.php.net/manual/en/function.mysql-fetch-row.php there is a very good example Quote Link to comment Share on other sites More sharing options...
twostars Posted January 12, 2008 Share Posted January 12, 2008 $sql="SELECT `column` FROM `table` WHERE `column`='value'" or die(mysql_error()); $query=mysql_query($sql); $results=mysql_fetch_assoc($query); Just use this. Your "or die" is in the wrong bit. You're questioning a string declaration. You want to move it to the mysql_query($sql) line. Quote Link to comment 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.