Morris2 Posted October 4, 2011 Share Posted October 4, 2011 Hi. Below is a working PHP script. How do I echo the variable "artist" the correct way? I've tryed everything I know, but cant get this to work ... (in chronoforms in Joomla 1.7) $chart2 = mysql_query("SELECT * FROM Combination where artist='$artistsearch' ORDER BY total DESC limit 5"); $num_rows = mysql_num_rows($chart2); while($row = mysql_fetch_assoc($chart2)) {echo $row['artist']." ... " .$row['city']." (" .$row['country'].") " .$row['total']." USD<br>";} Best regards Morris Quote Link to comment https://forums.phpfreaks.com/topic/248412-how-do-i-echo-this-variable-the-correct-way/ Share on other sites More sharing options...
mikesta707 Posted October 4, 2011 Share Posted October 4, 2011 well firstly, you are echoing $row['artist'] correctly. I don't see any problems there. You could try substituting $row['artist'] for $artistsearch (since they should technically be the same value as that is what you are looking for in your SELECT query). However, if you want to use $row['artist'] you can try debugging the problem with the following steps: print_r your $row array to see its contents (and verify they have the data you expect them too) check your database to make sure that the rows returned have the correct values in the artist column echo or print your $artistsearch variable to verify it has the correct data. A question: When you run this code, what happens? Is $row['artist'] seemingly blank (IE nothing is echoed where it should be echoed)? Is it the wrong artist? Quote Link to comment https://forums.phpfreaks.com/topic/248412-how-do-i-echo-this-variable-the-correct-way/#findComment-1275666 Share on other sites More sharing options...
Morris2 Posted October 4, 2011 Author Share Posted October 4, 2011 Hi. Thanks for your very quick answer. The values inside are all correct and echo'ing out the right way trough the script. But I want to isolate the variable "artist" from the table, and echo it out in a separate line of code, something like this: echo $row['artist'] (you said this was correct?) But no matter how I try to change the code, it will not echo out. What is the right notation in this case? Best regards Morris Quote Link to comment https://forums.phpfreaks.com/topic/248412-how-do-i-echo-this-variable-the-correct-way/#findComment-1275677 Share on other sites More sharing options...
litebearer Posted October 4, 2011 Share Posted October 4, 2011 You state - "The values inside are all correct and echo'ing out the right way trough the script." show us the the code that accomplishes that. Quote Link to comment https://forums.phpfreaks.com/topic/248412-how-do-i-echo-this-variable-the-correct-way/#findComment-1275683 Share on other sites More sharing options...
Drummin Posted October 4, 2011 Share Posted October 4, 2011 row value is only available withing the array bracket. Make variables for each item you wish to use outside this array. $chart2 = mysql_query("SELECT * FROM Combination where artist='$artistsearch' ORDER BY total DESC limit 5"); $num_rows = mysql_num_rows($chart2); while($row = mysql_fetch_assoc($chart2)) { $city=$row['city']; $country=$row['country']; $total=$row['total']; echo $row['artist']." ... " .$row['city']." (" .$row['country'].") " .$row['total']." USD<br>";} //You can then use the Variables outside the array echo "City: $city<br />Country: $country<br />Total: $total"; Quote Link to comment https://forums.phpfreaks.com/topic/248412-how-do-i-echo-this-variable-the-correct-way/#findComment-1275699 Share on other sites More sharing options...
the182guy Posted October 4, 2011 Share Posted October 4, 2011 Also try keeping your code tidy with proper indentation, this will help you spot these sort of problems quicker. Quote Link to comment https://forums.phpfreaks.com/topic/248412-how-do-i-echo-this-variable-the-correct-way/#findComment-1275708 Share on other sites More sharing options...
Morris2 Posted October 4, 2011 Author Share Posted October 4, 2011 Thanks a million! Now it works. Incredible fast and helpful response:-) Best regards Morris Quote Link to comment https://forums.phpfreaks.com/topic/248412-how-do-i-echo-this-variable-the-correct-way/#findComment-1275713 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.