Jump to content

[SOLVED] Displaying a picture which is being pulled from MySQL


PdVortex

Recommended Posts

Hello wondering if anyone can help me with this problem i got.

The script below should grab a picture from the database depending on what a person has input into a form.

All i get is a blank page with nothing on it :(  Can anyone help me out i been puzzling away at this for hours now.

 

Thanks for any help in advance.

 

$password = "*****";

$hostname = "*********";

$database = "*************";

$username = "***********";

 

$conn = mysql_connect($hostname, $username, $password)

or die("Connecting to MySQL failed");

 

mysql_select_db($database, $conn)

or die("Selecting MySQL database failed");

 

$location = $_POST['location'];

$destination = $_POST['destination'];

$img = mysql_query("SELECT img FROM routes WHERE location='$location' AND destination='$destination'");

if (!$img) {

    die('Could not query:' . mysql_error());

}

echo mysql_result($img, 0);

Link to comment
Share on other sites

p2grace, his syntax is fine.

 

do something like:

 

<?php

$img = mysql_query("SELECT `img` FROM `routes` WHERE `location` = '".mysql_real_escape_string($location)."' AND `destination` = '".mysql_real_escape_string($destination)."'") or die(mysql_error());

if(mysql_num_rows($img) > 0){
$row = mysql_fetch_assoc($img);
echo $row['img'];
}else {
echo "Doesn't exist";
}

?>

Link to comment
Share on other sites

Ok i tried mgallforever suggest but it still just shows a blank screen. But Thanks

 

So i went to phpmyadmin and typed this :

 

SELECT img FROM routes WHERE location AND destination

 

And its response was :

 

MySQL returned an empty result set (i.e. zero rows). (Query took 0.0002 sec)

 

Was that what you ment?

 

Once again thanks for any help in advance.

Link to comment
Share on other sites

OK cool done that :

 

SELECT `img` FROM `routes` WHERE `location` = 'New York'  AND `destination` = 'Omicron Alpha'

 

Returned with :

 

Showing rows 0 - 0 (1 total, Query took 0.0354 sec)

 

So it works on there.

 

So must be something with my code? ?

Link to comment
Share on other sites

Are you sure that your post variables are correct?  I'd echo them and make sure they're identical to what's in the database.  Also adding trim() to your post variables is a good idea to remove any extra spaces that might throw off the query.  I always make sure I trim() and mysql_real_escape_string() all variables before they going into an sql statement.

Link to comment
Share on other sites

OK cool done that :

 

SELECT `img` FROM `routes` WHERE `location` = 'New York'  AND `destination` = 'Omicron Alpha'

 

Returned with :

 

Showing rows 0 - 0 (1 total, Query took 0.0354 sec)

 

So it works on there.

 

So must be something with my code? ?

 

No, your query isn't returning any rows, so nothing will be shown. Are there any rows in your table? If so, what are the values of location/destination for them?

Link to comment
Share on other sites

Ok this is the form im using.

 

<form id="form1" name="form1" method="POST" action="search/new-search.php">

  <div align="center"><span class="style1">Current Location</span>

    <input type="text" name="Current Location" id="location" />

  </div>

  <div align="center"><span class="style1">Disierd Destination </span>

    <input type="text" name="Destination" id="destination" />

  </div>

  <div align="center"></div>

  <div align="center">

    <input type="submit" name="submit" id="submit" value="Submit" />

  </div>

  <div align="center"></div>

</form>

<p align="center" class="style1"> </p>

</body>

</html>

 

As for the trim things im sorry but can show me how because you lost me at trim()

 

Thanks again for any help.

Link to comment
Share on other sites

There's your problem, your post variables aren't named correctly.  Change the form to this:

 


<form id="form1" name="form1" method="POST" action="search/new-search.php">
  <div align="center"><span class="style1">Current Location</span>
    <input type="text" name="location" id="location" />
  </div>
  <div align="center"><span class="style1">Desired Destination </span>
    <input type="text" name="destination" id="destination" />
  </div>
  <div align="center"></div>
  <div align="center">
    <input type="submit" name="submit" id="submit" value="Submit" />
  </div>
  <div align="center"></div>
</form>
<p align="center" class="style1"> </p>
</body>
</html>

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.