Page 1 of 1

How Do I Store Data From One Column Of One Row In A Var.?

Posted: Fri Apr 25, 2008 3:29 am
by mirra1515
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?

Re: How Do I Store Data From One Column Of One Row In A Var.?

Posted: Fri Apr 25, 2008 4:09 am
by mattcooper
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:

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

Re: How Do I Store Data From One Column Of One Row In A Var.?

Posted: Fri Apr 25, 2008 4:13 am
by Kieran Huggins
Where you have:

Code: Select all

$row = mysql_fetch_array( $result );
you really want:

Code: Select all

$row = mysql_fetch_assoc( $result );

Re: How Do I Store Data From One Column Of One Row In A Var.?

Posted: Fri Apr 25, 2008 4:40 am
by mattcooper
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.?

Posted: Fri Apr 25, 2008 12:54 pm
by mirra1515
Thank you all for your posts, they have been very helpful! :D 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;