tekrscom Posted September 11, 2009 Share Posted September 11, 2009 Hi everyone... I was just trying to shorten my code a bit by using some shorter code... Can someone tell me what is wrong with this code, what it should be? echo '<h1 style="margin-bottom: 0px;">' . mysql_result("SELECT TopicName FROM ForumTopics WHERE TopicID = '{$_GET['TopicID']}'") . '</h1>'; Quote Link to comment https://forums.phpfreaks.com/topic/173911-echo-mysql_result/ Share on other sites More sharing options...
cbolson Posted September 11, 2009 Share Posted September 11, 2009 Hi, you are not actually getting the data from the database with that, just execting the query. You need to fetch the results from the query something like this: $res=mysql_result("SELECT TopicName FROM ForumTopics WHERE TopicID = '{$_GET['TopicID']}'"); $row=mysql_fetch_assoc($res); echo '<h1 style="margin-bottom: 0px;">' . $row["TopicName"] . '</h1>'; Not as short but certainly easier to understand and therefore see why it isn't working Chris Quote Link to comment https://forums.phpfreaks.com/topic/173911-echo-mysql_result/#findComment-916759 Share on other sites More sharing options...
PFMaBiSmAd Posted September 11, 2009 Share Posted September 11, 2009 Since mysql_result() does not execute a query, there is no way your original code was using mysql_result() that way. When you simplify code, you must retain the same functionality. Quote Link to comment https://forums.phpfreaks.com/topic/173911-echo-mysql_result/#findComment-916761 Share on other sites More sharing options...
tekrscom Posted September 11, 2009 Author Share Posted September 11, 2009 Oh, ok... was hoping anyway.... I was just certain that at one point in time or another someone had shown me a way that you could extract a single result/column/row with one line of code... Quote Link to comment https://forums.phpfreaks.com/topic/173911-echo-mysql_result/#findComment-916768 Share on other sites More sharing options...
cbolson Posted September 11, 2009 Share Posted September 11, 2009 Since mysql_result() does not execute a query opps, sorry, I copy&pasted too quickly Of course that should have been : $res=mysql_query("SELECT TopicName... Chris Quote Link to comment https://forums.phpfreaks.com/topic/173911-echo-mysql_result/#findComment-916782 Share on other sites More sharing options...
tekrscom Posted September 11, 2009 Author Share Posted September 11, 2009 Since mysql_result() does not execute a query opps, sorry, I copy&pasted too quickly Of course that should have been : $res=mysql_query("SELECT TopicName... Chris Don't worry, I knew what you meant... That's the way I usually do my queries... I was just looking for a way to perhaps shorten it a bit... Quote Link to comment https://forums.phpfreaks.com/topic/173911-echo-mysql_result/#findComment-916784 Share on other sites More sharing options...
mikesta707 Posted September 11, 2009 Share Posted September 11, 2009 if you really must have it in 1 line $row = mysql_result(mysql_query("SELECT TopicName FROM ForumTopics WHERE TopicID = '{$_GET['TopicID']}'")); should be around what you need, but I like to store the results of queries, and the queries themselves into variables for error checking, debugging and the like Quote Link to comment https://forums.phpfreaks.com/topic/173911-echo-mysql_result/#findComment-916809 Share on other sites More sharing options...
tekrscom Posted September 11, 2009 Author Share Posted September 11, 2009 if you really must have it in 1 line $row = mysql_result(mysql_query("SELECT TopicName FROM ForumTopics WHERE TopicID = '{$_GET['TopicID']}'")); should be around what you need, but I like to store the results of queries, and the queries themselves into variables for error checking, debugging and the like Yes, thank you very much... I considered trying that but I figured it wouldn't work either... That's exactly what I want... Thank You Quote Link to comment https://forums.phpfreaks.com/topic/173911-echo-mysql_result/#findComment-916821 Share on other sites More sharing options...
tekrscom Posted September 11, 2009 Author Share Posted September 11, 2009 if you really must have it in 1 line $row = mysql_result(mysql_query("SELECT TopicName FROM ForumTopics WHERE TopicID = '{$_GET['TopicID']}'")); should be around what you need, but I like to store the results of queries, and the queries themselves into variables for error checking, debugging and the like Yes, thank you very much... I considered trying that but I figured it wouldn't work either... That's exactly what I want... Thank You Still got the same result... Warning: Wrong parameter count for mysql_result() Quote Link to comment https://forums.phpfreaks.com/topic/173911-echo-mysql_result/#findComment-916831 Share on other sites More sharing options...
PFMaBiSmAd Posted September 11, 2009 Share Posted September 11, 2009 From the documentation - Description string mysql_result ( resource $result , int $row [, mixed $field= 0 ] ) The first two parameters, result and row, are required. Edit: All code you see posted on a forum like this comes with an implied disclaimer that it is just an example of how you would do something and may need modification to actually work in your situation. Also, mysql_result() has the misfortune of being the only mysql_xxxxx function to fetch data that generates a php error when there are zero rows in the result set. Quote Link to comment https://forums.phpfreaks.com/topic/173911-echo-mysql_result/#findComment-916833 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.