...

View Full Version : echo single mysql result



DJJama
03-22-2010, 05:06 PM
Hi guys.

One problem to the next.
I can now read from the session however I can not convert the output from the runid to runname.

I want the database to store a runid however show the user a name.
Below is the whole code and then the section which is specific to the problem.

Jama


<?php
require("includes/sesh.inc");
require("includes/header.inc");
require("includes/db.inc");
require("includes/menu.inc");

$currentuser = $_SESSION['uid'];


echo "<h1>Run Selection</h1>";



if(isset($_GET['add']))
{
$i = $_POST['rid'];
$r = $_POST['rname'];

$query= "UPDATE userdetails
SET racetrainingfor = $r
WHERE userdetails.userid = $currentuser";
mysql_query($query);
echo "<p>Race Selected</p>";
echo $r;
$query2 = "SELECT runname FROM rundetails WHERE runid=$r";
echo mysql_query($query2);

}



echo "<form action='selectrun.php?add=yes' method='post'>";
echo "<select name='rname'>";

$query1 = "SELECT * FROM rundetails";
$result = mysql_query($query1);

while ($row = mysql_fetch_array($result))
{
extract($row);

echo"<option value= '".$runid."'> ".$runname." </option>";

}
echo "</select>";
echo "<input type='submit' value='Select Race'></p>";
echo "</form>";



require("includes/footer.inc");
?>




$query2 = "SELECT runname FROM rundetails WHERE runid=$r";
echo mysql_query($query2);

tomws
03-22-2010, 06:03 PM
The return value of mysql_query (http://php.net/mysql_query) is a mysql resource, not text output. You need to fetch (http://php.net/mysql_fetch_array) the data from the result in order to display it. This information should be shown on any beginner level tutorial found through a search engine.

masterofollies
03-22-2010, 09:56 PM
Yes you don't echo out a query like Tom said. This is one way of doing it.


$query2 = mysql_query("SELECT runname FROM rundetails WHERE runid=$r");
$row = mysql_fetch_array($query2);

echo "".$row['runname']."";

MattF
03-22-2010, 09:59 PM
$query2 = mysql_query("SELECT runname FROM rundetails WHERE runid=$r");
$row = mysql_fetch_array($query2);

echo "".$row['runname']."";

You could condense that to two lines:



$query2 = mysql_query("SELECT runname FROM rundetails WHERE runid=$r");
print(mysql_fetch_result($query2));

DJJama
03-22-2010, 10:49 PM
Thanks very much guys.

Sorry if i did seem obvious, i knew it was wrong but was not sure what to look for on how to output this.

I'm gradually trying to teach myself php through this project :)

masterofollies
03-23-2010, 01:17 AM
No prob, we are here to help. It's nearly impossible to learn it all on your own. It's best to ask questions.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum