2

I see several questions relating to my problem, but I'm very new to programming and cant figure out half the responses. Below is the only thing I have gotten working for getting data out of my table, it is however doing way more than I need it to, and still giving me an undesired result.

I simply want to take the string from the first row of my table and save into a php variable called $news1.

$connection = mysql_connect("localhost", "USERNAME", "PW");

if(!$connection)
{
    die("<p>no connection to database</p>");   
}

if(!mysql_select_db("akron11_db", $connection))
{
    die("<p>Kunne ikke finde databasen</p>");
}

$result = mysql_query("SELECT news1 FROM TurenTilDannmark", $connection);   

if(!$result)
{
    die("<p>Efterspørgslen slog fejl " . mysql_error() . "</p>");   
}

$rows = mysql_num_rows($result);

for ($item = 0; $item < $rows; $item++)
{
    echo "<li>" . mysql_result($result, $item) . "</li>";  
} 

echo "</p>"; 
1
  • 1
    "an undesiered result." Can you be a bit more specific please? What is the result you get, and what were you hoping for? Commented Dec 10, 2011 at 18:44

2 Answers 2

2

You can use limit to fetch just one result:

$result = mysql_query('SELECT news1 FROM TurenTilDannmark LIMIT 1', $connection);

Then you you can use mysql_fetch_assoc:

$row = mysql_fetch_assoc($result);
echo $row['news1']; // ta-da!

Or mysql_result:

$news1 = mysql_result($result, 0); // 0 is the index of the field, not the row
echo $news1;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks middus, that was so simple! You guys rock.
1

Try:

$result = mysql_query("SELECT FIRST(news1) FROM TurenTilDannmark", $connection);

FIRST() is equivalent to LIMIT 1.

That should give you only one row when you do next:

$rows = mysql_num_rows($result);

3 Comments

No error, but echoing the result gives gives no output. $news1 = mysql_query("SELECT TOP 1 news1 FROM TurenTilDannmark", $connection); echo $news1;
@user1091537: It's not $result that you need to echo. You need to do $row = mysql_fetch_assoc($result); then echo $row['news1']; just like middus wrote in his answer.
Sorry site moved too fast for me xD it works now! Thanks all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.