...

View Full Version : Get single column value from defined row using primary key.



martynball
01-18-2010, 03:36 PM
is there a way I can get a database row, then get a value from that row and put it into a variable?

E.g.
-----Column1-----Column2-----Column3-----Column4-----PrimaryKey
Row: Value1-----Value2--------Value3-----Value4--------123------

$variable = Value2;

shadowmaniac
01-18-2010, 03:45 PM
Something like below should do the trick (untested):


// Connect to DB and select database here
// change "example" to your table's name
$result = mysql_query("SELECT Value2 FROM example
WHERE PrimaryKey ='123'") or die(mysql_error());

$row = mysql_fetch_array( $result );
echo $row['Value2'];

MattF
01-18-2010, 05:38 PM
The primary key sets the column value(s) as being unique. Just query the respective column(s) and fetch the result.

martynball
01-18-2010, 06:58 PM
Apparently there is an unexpected "}" on line 55, but I see no problem with it :/

I am just trying to check if the correct value is being pulled in.


<?php
//Delete
if (isset($_POST['deleteID'])) {

$id = mysql_real_escape_string($_POST['deleteID']);
$deleteFile = mysql_query("SELECT Nfilename FROM uploaded_files
WHERE ID ='$id'") or die(mysql_error());
$row = mysql_fetch_array( $deleteFile );
echo $row['DeleteFile'];
if (!deleteFile)
{
die('Error: ' .mysql_error())
}}
$delete = "DELETE FROM uploaded_files WHERE ID='$id'";
$result = mysql_query ($delete);
if (!$result)
{
die('Error: ' .mysql_error());
}
echo "<center><div style=\"background-color:#999999; border:0px; color:#339900; font-family:Calibri; font-weight:bold;\">Deleted entry number: $id. And Deleted image related to that entry.</div></center>";
}
?>

martynball
01-18-2010, 07:33 PM
Fixed that error.

MattF
01-19-2010, 02:27 AM
Indent your code, and put braces on their own lines. It makes it a lot easier when you have to visually inspect your code for errors such as those. For example:



if (isset($_POST['deleteID']))
{
$id = mysql_real_escape_string($_POST['deleteID']);
$deleteFile = mysql_query("SELECT Nfilename FROM uploaded_files
WHERE ID ='$id'") or die(mysql_error());
[the rest of the code here]

if ([something here])
{
[some more code here]
}
}


Using an extra tab per level works wonders for ease of inspection.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum