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 Link to comment https://forums.phpfreaks.com/topic/85659-solved-how-do-i-take-only-one-result-from-db/ 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 ? Link to comment https://forums.phpfreaks.com/topic/85659-solved-how-do-i-take-only-one-result-from-db/#findComment-437139 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? Link to comment https://forums.phpfreaks.com/topic/85659-solved-how-do-i-take-only-one-result-from-db/#findComment-437144 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) Link to comment https://forums.phpfreaks.com/topic/85659-solved-how-do-i-take-only-one-result-from-db/#findComment-437146 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? Link to comment https://forums.phpfreaks.com/topic/85659-solved-how-do-i-take-only-one-result-from-db/#findComment-437147 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 Link to comment https://forums.phpfreaks.com/topic/85659-solved-how-do-i-take-only-one-result-from-db/#findComment-437149 Share on other sites More sharing options...
asmith Posted January 12, 2008 Share Posted January 12, 2008 post your final code Link to comment https://forums.phpfreaks.com/topic/85659-solved-how-do-i-take-only-one-result-from-db/#findComment-437151 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. =] Link to comment https://forums.phpfreaks.com/topic/85659-solved-how-do-i-take-only-one-result-from-db/#findComment-437153 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. Link to comment https://forums.phpfreaks.com/topic/85659-solved-how-do-i-take-only-one-result-from-db/#findComment-437154 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 Link to comment https://forums.phpfreaks.com/topic/85659-solved-how-do-i-take-only-one-result-from-db/#findComment-437158 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. Link to comment https://forums.phpfreaks.com/topic/85659-solved-how-do-i-take-only-one-result-from-db/#findComment-437159 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.