Jump to content

[SOLVED] How to select a single value from a table using another value in it's row?


Obsession

Recommended Posts

Say for instance, I want to pull out the path of an image stored in the database, by inputting said image's name in the search form? I know I can pull the whole line out with SELECT * FROM but, if I replace the * with any of the database column names I get answer "Resource ID #3" up to "Resource ID #5" (I want to pull out 3 fields, using the name to get the right row.

 

Possible?

 

I know my English isn't perfect, not my mothertongue ;)

 

This si the code-

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  };
mysql_select_db("CriminalDatabase", $con);
$CrimName=$_POST['CrimName'];

echo $CrimName;

$CrimRes = mysql_query("SELECT * FROM criminals WHERE CrimName='$CrimName'");

if (!$CrimRes)
  {
  die('Error: ' .mysql_error());
  }
  else
  echo "Successful Query!";
  

echo "<div id='CrimImg'>";
echo $CrimRes;
echo "</div>"; 

mysql_close($con);
?>

 

Now, the echo "Successful Query!" prints when I run this code...Anbd where I told it to print the value of the $CrimNameit prints, yet  the result of the query is "Resource id #3"...

 

 

thats because you're not processing the results properly. I am assumming you're doing something like this

 

$query = mysql_query("SELECT img_path FROM your_images WHERE image_name='$YOUR_IMAGE_NAME'");

echo $query;

Which is incorrect. What you should do

$query = mysql_query("SELECT img_path FROM your_images WHERE image_name='$YOUR_IMAGE_NAME'");

// process the result resource
$row = mysql_fetch_assoc($query);
echo $row['img_path'];

 

The above is a very cut down version.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.