Page 1 of 2

php and mysql

Posted: Wed May 03, 2006 2:01 am
by corillo181
i did a up load file.. and my picture uploaded to my directory and and name and path to the database too..

so now i'm using this code to pull it from my sql but is not working any one knows why?

Code: Select all

<?php
include 'config/db.php';

$getpic=mysql_query("SELECT name FROM upload2");
while($row=mysql_fetch_array($getpic))
{
echo '<img src="xml/$row">';
}
?>

Posted: Wed May 03, 2006 2:14 am
by Ollie Saunders
Assuming everything other than your script it working all your doing wrong is printing the name of the variable rather than what it contains.

Code: Select all

$foo = 'ole';
echo 'my name is $foo'; // outputs just as it looks
echo 'my name is '.$foo; //outputs my name is ole
Another mistake you are making is a missing array key. When you do a query such as mysql_fetch_array an array of columns is returned if you echo $row you will get Array printed out. $row in this case contains one element 'name' which you can access with the ['indexName'] syntax. I've done it for you below.

Code: Select all

$q = 'SELECT some,stuff FROM tablename';
$result = mysql_query($q);
$row = mysql_fetch_array($result);
// to echo stuff:
echo $row['some'];
echo $row['stuff'];
Security Note: You should escape all your database output with htmlentities

Code: Select all

echo htmlentities($row['stuff'],ENT_QUOTES,'UTF-8');

Posted: Wed May 03, 2006 2:37 am
by corillo181
i took some of your code and tryied to work ti out and i came up with kind of the same but a little different and still doesn't works when i try to view the picture on the page it just shows a [x].. and i know the pictures are there because i used the upload thing to put it in the database and directory

Code: Select all

<?php
include 'config/db.php';
$q = 'SELECT name FROM upload2';
$getpic=mysql_query($q);
while($row=mysql_fetch_array($getpic))
{
echo '<img src=xml/'.$row['name'].'>';
}
?>

Posted: Wed May 03, 2006 3:10 am
by Maugrim_The_Reaper
What is the current value of $row['name']? Is this correct or wrong in the html output? If it is wrong, is it definitely on the database in a field called "name"?

Also maybe try mysql_fetch_assoc() to ensure you get one list of results for iteration (the other may return both associative and numerical keys together - i.e. multiple copies).

Posted: Wed May 03, 2006 3:11 am
by corillo181
yes the field name on the database is name..

Posted: Wed May 03, 2006 3:24 am
by Maugrim_The_Reaper
...and the value of $row['name']?

Posted: Wed May 03, 2006 3:30 am
by corillo181
this is the value of $row is fetching for the arrays inside the table..

Code: Select all

while($row=mysql_fetch_array($getpic))
?>
and with this i'm callin the image to show up in the page

Code: Select all

echo '<img src=xml/'.$row['name'].'>';
the 'name' value is the name of the field in the database!

I never workd with directories and database before so i have no clue..as to how it supossed to pull the image and show it..

Posted: Wed May 03, 2006 3:35 am
by Maugrim_The_Reaper
I'll try it another way...

Add the following:

print_r($row);
exit(0);

after you fetch $row, and paste the result to the forum - checking if $row actually holds a value (not clear from your posts).

You should have seen me the first time out with PHP ;). Practice makes perfect - don't lose patience.

Posted: Wed May 03, 2006 3:38 am
by Ollie Saunders
Can you paste in the HTML source that PHP is generating for us please.

Posted: Wed May 03, 2006 3:44 am
by Maugrim_The_Reaper
Basically by printing the variable - we can check it is holding the value we want it to - if it does, then the script works and the problem is something else. If it prints nothing, or a wrong value then we found the problem :). echo, print_r and var_dump are good for checking variable values in finding problems...

Posted: Wed May 03, 2006 4:04 am
by corillo181
i know i'm not loosing my patience i'm a pacient guy.. is that when i get into something i cant rest until i'm done with it.. my be dis not going to se me until i make this work.. :)

so this is what the print_r echos out


Array
(
[0] => Picture141.jpg
[name] => Picture141.jpg
)

Posted: Wed May 03, 2006 4:20 am
by Maugrim_The_Reaper
Array
(
[0] => Picture141.jpg
[name] => Picture141.jpg
)
mysql_fetch_assoc() should get rid of the double entries if not needed.

Okay, so the value is correct. The script works - when the script prints out normally, what is the HTML source it prints? Also try replacing the relative xml/ path with the full path, e.g. http://www.example.com/xml/

Finally - reformat the img tag for XHTML compliance...

echo '<img src="xml/' . $row['name'] . '">';

It's often useful to create a constant for the full route in your scripts - this prevents any relative path errors. For example, if I put a PHP index.php file at http://www.example.com/MyApp/ and I want this uri to be my base URL for all links/image src's I can create a constant for use in all scripts with:

Code: Select all

// clean the PHP_SELF value (prevent XSS via Apache mod_rewrite)
$self = basename(__FILE__);
$_SERVER['PHP_SELF'] = substr($_SERVER['PHP_SELF'], 0, strpos($_SERVER['PHP_SELF'], $self)) . $self;

// calculate the URL (without filename)
$self = $_SERVER['PHP_SELF'];
$parts = explode("/", $self);
$nofilename = array_pop($parts);
$thisurl = implode("/", $parts);

// define the URLROOT with trailing slash
define('URLROOT', $thisurl . '/');

Posted: Wed May 03, 2006 4:31 am
by corillo181
the html says it should be printing out because this how the source page looks like..
is reading the path ..

Posted: Wed May 03, 2006 4:33 am
by Maugrim_The_Reaper
Does the url work when you type into a browser manually?

Posted: Wed May 03, 2006 4:42 am
by corillo181
:-D i guess i'm thinking like a programmer alread, that what i was doing and that was the problem..

when i used the upload form it must upload the picture in another kind of format becuase when i went to chekc it it said that the picture couldn't be view, and i changed it for anothe rone upload it normaly with ftp.. and it worked just fine..

so the problme is in the uploading process..

but thank you.. i like this forum ppl really try to help unlike other forum i bene to.. thye expect newbies to be professional

:|

well if i cna't find the uploading problem you'll seen me as soon a sin minutes :D..

maybe i forogt to define the file type that it was goign to allowed..