Jump to content

Recommended Posts

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>';

Link to comment
https://forums.phpfreaks.com/topic/173911-echo-mysql_result/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/173911-echo-mysql_result/#findComment-916759
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/173911-echo-mysql_result/#findComment-916784
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/173911-echo-mysql_result/#findComment-916809
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/173911-echo-mysql_result/#findComment-916821
Share on other sites

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()

Link to comment
https://forums.phpfreaks.com/topic/173911-echo-mysql_result/#findComment-916831
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/173911-echo-mysql_result/#findComment-916833
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.