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"...

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.