I am trying to display the contents of one specific row and column from a MySQL database. My table has two columns: 'DataName' and 'DataContent'. I would like to simply display the contents of column DataContent only where DataName equals a value.
This is what I have so far...the problem is that it displays both DataName and DataContent, I only want to display DataContent. I have tried just taking out the portion of the echo command that displays DataName, but I only get an error.
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test_db", $con);
// Get a specific result from the "data" table
$result = mysql_query("SELECT * FROM data
WHERE DataName='dyk1'") or die(mysql_error());
// get the first (and hopefully only) entry from the result
$row = mysql_fetch_array( $result );
// Print out the contents of each row into a table
echo $row['DataName']." - ".$row['DataContent'];
mysql_close($con);
?>
How can I display just one set of data from the column DataContent?
How Do I Store Data From One Column Of One Row In A Var.?
Moderator: General Moderators
- mattcooper
- Forum Contributor
- Posts: 210
- Joined: Thu Mar 17, 2005 5:51 am
- Location: London, UK
Re: How Do I Store Data From One Column Of One Row In A Var.?
Not sure exactly what you mean here, but I think you mean "Limiting a query to returning just one row"?
If this is the case, use this:
You say "hopefully the only entry from the result" in your comment... to avoid a duplicate entry in the DataName column, make sure that column has a unique index and also that you run a check against stored values before you attempt to insert a new row 
If this is the case, use this:
Code: Select all
/ Get a specific result from the "data" table
$result = mysql_query("SELECT * FROM data WHERE DataName='dyk1' LIMIT 1") or die(mysql_error());
// get the first (and hopefully only) entry from the result
$row = mysql_fetch_array( $result );
// Print out the contents of each row into a table
echo $row['DataName']." - ".$row['DataContent'];
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
Re: How Do I Store Data From One Column Of One Row In A Var.?
Where you have:
you really want:
Code: Select all
$row = mysql_fetch_array( $result );Code: Select all
$row = mysql_fetch_assoc( $result );- mattcooper
- Forum Contributor
- Posts: 210
- Joined: Thu Mar 17, 2005 5:51 am
- Location: London, UK
Re: How Do I Store Data From One Column Of One Row In A Var.?
Well spotted... been using DB abstraction for too long I think 
Re: How Do I Store Data From One Column Of One Row In A Var.?
Thank you all for your posts, they have been very helpful!
I was able to slightly modify the code provided to come up with what I needed:
// Get a specific result from the "data" table
$result = mysql_query("SELECT * FROM data WHERE DataName='dyk1' LIMIT 1") or die(mysql_error());
// get the first (and hopefully only) entry from the result
$row = mysql_fetch_assoc( $result );
// Print out the contents of each row into a table
$testdata = $row['DataContent'];
echo $testdata;
// Get a specific result from the "data" table
$result = mysql_query("SELECT * FROM data WHERE DataName='dyk1' LIMIT 1") or die(mysql_error());
// get the first (and hopefully only) entry from the result
$row = mysql_fetch_assoc( $result );
// Print out the contents of each row into a table
$testdata = $row['DataContent'];
echo $testdata;