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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mirra1515
Forum Commoner
Posts: 29
Joined: Fri Apr 25, 2008 3:17 am

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

Post 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?
User avatar
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.?

Post 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 ;)
User avatar
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.?

Post 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 );
User avatar
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.?

Post by mattcooper »

Well spotted... been using DB abstraction for too long I think ;)
User avatar
mirra1515
Forum Commoner
Posts: 29
Joined: Fri Apr 25, 2008 3:17 am

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

Post 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;
Post Reply