Forum Moderators: coopster

Message Too Old, No Replies

PICTURES PHP and MYSQL HELP

mysql,php,pictures,images,help,tutorials,save,how,to,in

         

phil2564

2:45 pm on May 24, 2004 (gmt 0)

10+ Year Member



I want to have one colimn of a my database hold a picture. I think I would like to keep the pictures in mysql. How do I set upe the column and how is the image store script written so I can write in the mysql monitor.And also please let me know how to retrieve it. Below is my retrieve script (PHP) can anyone modify it to show me how to retrieve it.

______________________________________________________
<?php
$conn = mysql_connect("localhost","","");
mysql_select_db("models",$conn);
$person = $_POST['sex'];
$myage = $_POST['dob'];
$myheight = $_POST['size'];
$resultID = mysql_query("SELECT * FROM $person WHERE age in ($myage) and height in ($myheight) ",$conn);
print "<table border = .25><tr><th>id</th>";
print "<th>name_first</th><th>name_last</th>";
print "<th>age</th><th>height</th><th>weight</th>";
while ($row = mysql_fetch_row($resultID))
{
print "<tr>";
foreach ($row as $field)
{
print "<td>$field</td>";
}
print "</tr>";
}
print "</table>";
mysql_close($conn);
?>

LangDesigns

3:42 pm on May 24, 2004 (gmt 0)

10+ Year Member



Well, you can't store images in an MySQL database directly. Have the files uploaded to a location then inside the table have the value = img.jpg

Then;
$path = "yoursite.com/images/";

echo "<img src=\"{$path}{$img}\">";

See where i'm going? Now to output it you'd have to do this:

$result = mysql_query("SELECT * FROM table");

While ( $r = MySQL_fetch_array($result) ) {
$img = $r['table_img'];

echo "<img src=\"{$path}{$img}\">";

}

phil2564

5:50 pm on May 24, 2004 (gmt 0)

10+ Year Member



DO I make the column VARCHAR or Blob or TEXT
What do I use for the column?

phil2564

6:32 pm on May 24, 2004 (gmt 0)

10+ Year Member



I 'm haveing a problem used what you gave me but I keep getting this back

mysql_fetch_array(): supplied argument is not a valid MySQL result resource in

THIS IS MY SCRIPT

<?php
$conn=mysql_connect("localhost","","");
mysql_select_db("models",$conn);
# Variable path to images
$path = "Inetpub\wwwroot";
// The Query
$result = mysql_query("SELECT * FROM pick");

While ( $r = MySQL_fetch_array($result) ) {
$img = $r['table_img'];

echo "<img src=\"{$path}{$img}\">";

}
?>

jatar_k

6:35 pm on May 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that means your query here

$result = mysql_query("SELECT * FROM pick");

isn't working. It is more than likely returning an error. Try to get the error like so

$result = mysql_query("SELECT * FROM pick") or die (mysql_error());

if your query errors out that will return the actual error from mysql and display it to the screen and kill the script. It helps when trying to debug queries.

phil2564

9:45 pm on May 24, 2004 (gmt 0)

10+ Year Member



Ok I started with a whole new database and saved the pick and everything and put in the error message and this is what I am getting back>>>>>>>>>>>>>>>

Undefined index: table_img in c:\inetpub\wwwroot\picture_output.php on line 17

THE SCRIPT I AM USING IS BELOW
By the way how should the column for the picture be setup as **********VARCHAR TEXT BLOB **********
Does it matter?


<?php
$conn=mysql_connect("localhost","","");
mysql_select_db("mytest",$conn);
# Variable path to images
$path = "Inetpub\wwwroot";
// The Query
$result = mysql_query("SELECT * FROM my_kids")or die(mysql_error());

While ( $r = MySQL_fetch_array($result) ) {
$img = $r['table_img'];

echo "<img src=\"{$path}{$img}\">";

}
?>

coopster

3:37 pm on May 25, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



This is telling you that either no records were returned, or there is no column in your table named "table_img". Are you sure the table has rows in it? You can check that next...
$result = mysql_query("SELECT * FROM my_kids")or die(mysql_error()); 
if (mysql_num_rows [php.net]($result) == 0) {
print "the table is empty!";
} else {
While ( $r = MySQL_fetch_array($result) ) {
...

m_shroom

6:44 am on Jun 3, 2004 (gmt 0)

10+ Year Member



Unless you have a very special perpose in mind like useing php's image malipation functions (store as blob ), don't store images in MySql tables.
1 it makes your tables very large very quicly - cost is speed.
2 it makes your tables dynamic - cost is speed.
3 you must convert them to binary to insert them to MySql - cost is speed.
4 you must covert them back after retiving them - cost is speed.

Store your images in sperate folders and store folder_name and file_name in your DB.

m_shroom

7:19 am on Jun 3, 2004 (gmt 0)

10+ Year Member



One more thing I forgot.

5 It creates a very large variable in memory which may have to be carried for a while - out of memory errors or server crashes.

phil2564

12:35 pm on Jun 3, 2004 (gmt 0)

10+ Year Member



Thanks very much I did just that I stored the path to pictures which I stored in a folder and just called for the path. I want to thank you again for your help!

Phil