0

First I would like to know how to display an image in PHP just by using the path of the image? without using BLOB.

AND

I have a table in my database which is layed out this way..

columns : i1 i2 i3 i4 i5 i6 i7 i8

each field hold a value or is empty.

I would basically want to display the values of i5 i6 i7 i8 according to what is in i1 i2 i3 i4.

For e.g:

i1=a i2=b i3=e i4=q i5=g i6=l i7= i8=

For this case i would like g and l to be displayed. But i want the values to be stored in an array.

I tried doing:

$list = array(g,l)
$query = " SELECT i5, i6, i7, i8  FROM Table WHERE i1 AND i2 AND i3 AND i4 IN ('$list') ";
$s = mysql_query($query);
while($data = mysql_fetch_array($s)){
    $r_1 = $data['i5'];
    $r_2 = $data['i6'];
    $r_3 = $data['i7'];
    $r_4 = $data['i8'];         
}
echo "$r_1  $r_2 $r_3  $r_4"`

Which is not working even if i do $list[0]

Help would be appreciated. Thanks

Brendan Long
  • 51,248
  • 19
  • 139
  • 179
StringerB
  • 75
  • 1
  • 5

2 Answers2

2

First I would like to know how to display an image in PHP just by using the path of the image? without using BLOB.

You don't display images in PhP, you display it in html. If you've got the path then simply generate an img tag in html with src attribute set to path :

<img src="whatever your path is" alt="My Image" />

Blobs are used to store images (actually big binary data, images, movies, ...) directly in the DB.

Laurent S.
  • 6,636
  • 2
  • 27
  • 40
0
$s = mysql_query($query);
while($data = mysql_fetch_array($s)){

You're not checking if there's an error during the query. From the documentation for mysql_query:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

So do:

$s = mysql_query($query);
if($s === FALSE) {
    // Do something to handle the error here, possibly calling die()
}

Next problem, what if there's no results? Your variables would all be empty. You can test for that:

if(empty($r_1)) {
    echo "Oops, I guess we didn't get any results";
    // die()?
}

What if we get more than one row of results? I'm not sure how your program should handle this.

And finally, I think your SQL query isn't doing what you think it's doing:

SELECT i5, i6, i7, i8  FROM Table WHERE i1 AND i2 AND i3 AND i4 IN ('$list')

This will give you i5, i6, i7, and i8 when i1 is true, i2 is true, i3 is true, and i4 is equal to the string $list. If you want find results where all of them are in the list, you'll need to do multiple IN statements, and you'll need to pass $list as a list, not a string. You can see this answer for how to construct the query string to pass a list to MySQL.

Also note that the mysql_* functions are deprecated, and you should really learn to use PDO or the mysqli_* functions. It won't take very much time, and secure database access is much easier with them.

Community
  • 1
  • 1
Brendan Long
  • 51,248
  • 19
  • 139
  • 179
  • Thanks, was thinking about using learning PDO, I will soon. I had a look at your link, but when you say i will need multiple `IN`statement do you mean this way `WHERE $i1 IN ($list) AND $i2 IN ($list)...` @BrendanLong – StringerB Mar 26 '13 at 18:15
  • @StringerB Yes. Except just putting $list is incredibly dangerous and probably won't work. – Brendan Long Mar 26 '13 at 19:08