1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP for dummies

Discussion in 'PHP' started by Betoneira, Nov 2, 2006.

  1. #1
    Hi there,

    i've a database created in MySQL, and many doubs are comming coz i'm a beginner, hope you can all help me:

    1. How do i show the data in the database fields?
    I know that i have to connect to server 1st but cant create the <table> tag after.

    2. How can i create a htm form thats deals text and image (file) fields and post them on the db?
    I have the text form working. but when i add the file upload on the php script nothing works.

    Hope to recxeive an answer soon coz its very urgent

    regards and many thanks to all
     
    Betoneira, Nov 2, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    You have to be more specific. A database structure would also be helpful. And which fields you want to display...

    Here a very basic example
    
    $query = mysql_query("SELECT * FROM tabe WHERE foo = 'bar'") OR die( mysql_error() );
    $data = mysql_fetch_array($query);
    
    echo $data['foo'];
    
    PHP:

    As for the uploader, I would not store the images in the database, I would upload them to a separate folder and only save the filename in the database.

    It would help if you could post your code so far.
     
    nico_swd, Nov 2, 2006 IP
  3. Betoneira

    Betoneira Peon

    Messages:
    15
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Nico for answer it:

    I have as fields:
    data
    name
    client
    description
    image (jpg file)

    please ask more doubts that i will aswer fast
     
    Betoneira, Nov 2, 2006 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Okay, so this are the fields. And how do we know which row you want to pull? Or do you want to pull all rows and show them in a table?
     
    nico_swd, Nov 2, 2006 IP
  5. Betoneira

    Betoneira Peon

    Messages:
    15
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I would like to pull all the rows.
    Each time i add a record, he will show at fist.

    for example in the database:

    data inserted in jan 2006
    data inserted in fev 2006
    data inserted in may 2006

    he will show in this order

    may
    fev
    jan
     
    Betoneira, Nov 2, 2006 IP
  6. Betoneira

    Betoneira Peon

    Messages:
    15
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    if you think its better please add me

    nuno0silva@hotmail.com
     
    Betoneira, Nov 2, 2006 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    We need an ID or time field in order to order the rows like you wish. Here an example for the unordered table.

    (You have to edit the highlighted strings)
    
    <?php
    
    $con = mysql_connect('localhost', 'username', 'password');
    mysql_select_db('databasename', $con);
    
    $query = mysql_query("SELECT * FROM tablename") OR die( mysql_error() );
    ?>
    
    <table border="1" width="80%">
     <tr>
    		<td>data</td>
    		<td>name</td>
    		<td>client</td>
    		<td>description</td>
    		<td>image</td>
    	</tr>
    <?php
    
    while ($row = mysql_fetch_array($query))
    {
    	?> <tr>
    		<td><?php echo $row['data']; ?></td>
    		<td><?php echo $row['name']; ?></td>
    		<td><?php echo $row['client']; ?></td>
    		<td><?php echo $row['description']; ?></td>
    		<td><?php echo $row['image']; ?></td>
    	</tr>
    	
    	<?php
    }
    ?>
    
    </table>
    
    Code (markup):
    Does every row have a unique ID or something that would allow us to order it like you want to?
     
    nico_swd, Nov 2, 2006 IP
    streety likes this.
  8. Betoneira

    Betoneira Peon

    Messages:
    15
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi,

    I dindt have any id field.
    I've created one Id field but what must be the type of it?

    Thanks for everything
     
    Betoneira, Nov 3, 2006 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    Make it INT (auto_increment)
     
    nico_swd, Nov 3, 2006 IP
  10. Betoneira

    Betoneira Peon

    Messages:
    15
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Nico, thanks very much!

    Its works perfectly. Now my only problem is the image part.

    The image on the data base is defined as blob.
    The images are stored in the images directory on the server.
    My question is how can i "load" the image on the page?
    I read that if the image file is called 01.jpg and if i upload her directly in the MySQL admin how can i show her?
     
    Betoneira, Nov 3, 2006 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    If you have the image name in the DB, then do following.

    
    <img src="/path/to/folder/<?php echo $row['image']; ?>" />
    
    Code (markup):
     
    nico_swd, Nov 3, 2006 IP
  12. Jasonb

    Jasonb Well-Known Member

    Messages:
    4,486
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    195
    #12
    Isnt PHP for dummies Perl/C+/C++?

    :)
     
    Jasonb, Nov 3, 2006 IP
  13. Betoneira

    Betoneira Peon

    Messages:
    15
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    the image field on the database is a blob field.

    If i only have the name of the file, what type must be? txt?
     
    Betoneira, Nov 3, 2006 IP
  14. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #14
    Hm, wait, are the images stored in a folder or in the database? If it's stored in the database, then you can load them via an external file like this.

    
    
    <?php
    
    $query = mysql_query("SELECT * FROM images WHERE name = '". mysql_real_escape_string($_GET['name']) ."'");
    $image = mysql_fetch_array($query);
    
    header('Content-type: image/jpg');
    echo $image['content'];
    
    ?>
    
    Code (markup):
    And load it like this
    
    <img src="image.php?name=<?php echo $name; ?>" />
    
    Code (markup):
     
    nico_swd, Nov 3, 2006 IP
  15. Betoneira

    Betoneira Peon

    Messages:
    15
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Hi there,

    What is the most easy? The images stored in database or in a directory?

    The last post did not work with the images stored in the database,
    and it turned "error" in the last part of the loader

    no image icon + " />
     
    Betoneira, Nov 3, 2006 IP
  16. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #16
    I personally would say that saving the image in a directory is the easiest way to go. Less messing about with file headers, less files and in all likelihood faster as well.
     
    streety, Nov 3, 2006 IP
    nico_swd likes this.
  17. Betoneira

    Betoneira Peon

    Messages:
    15
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #17
    And how do i do that?

    What type should be the image field on the database? txt?

    I have the images stored in a directory how do i display them with the rest of the records?
     
    Betoneira, Nov 3, 2006 IP
    nico_swd likes this.
  18. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #18
    Varchar (100) for example.

    As I said earlier, like this if the images are in a directory, and the image names in the database.

    
    <img src="/path/to/folder/<?php echo $row['image']; ?>" />
    
    Code (markup):
     
    nico_swd, Nov 3, 2006 IP
  19. Betoneira

    Betoneira Peon

    Messages:
    15
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #19
    $query = mysql_query("SELECT * FROM prpa") OR die( mysql_error() );
    ?>
    <?php

    while ($row = mysql_fetch_array($query)) :(
    {
    ?>
    <tr>
    <td width="115" rowspan="4" valign="top"><div align="center"><font color="#000000" size="2" face="Arial, Helvetica, sans-serif"><?php echo $row['data']; ?></font></div></td>
    <td width="275" height="42" bgcolor="#CC0000"><font color="#FFFFFF" size="+2" face="Arial, Helvetica, sans-serif">
    &nbsp;<?php echo $row['nome']; ?></font></td>
    <td width="310" height="40" bgcolor="#FFFFFF"><font size="5" face="Times New Roman, Times, serif"><em>
    &nbsp;&nbsp;<?php echo $row['cliente']; ?></em></font></td>
    <td width="300" height="58" rowspan="2" valign="top" bgcolor="#FFFFFF"><font color="#000000" size="1" face="Arial, Helvetica, sans-serif">
    <?php echo $row['desc']; ?></font></td>
    </tr>
    <tr>
    <td width="275" height="18" bgcolor="#666666"><font color="#FFFFFF" size="1" face="Arial, Helvetica, sans-serif">
    &nbsp;&nbsp;<?php echo $row['url']; ?></font></td>
    <td width="310" height="18" bgcolor="#FFFFFF">&nbsp;</td>
    </tr>
    <tr>
    <td colspan="3">

    <?php

    $query = mysql_query("SELECT * FROM img WHERE name = '". mysql_real_escape_string($_GET['name']) ."'");
    $image = mysql_fetch_array($query); :(

    echo $image['content'];

    ?>

    <img src="/img/<?php echo $row['img']; ?>" /></td>
    </tr>
    <tr>
    <td height="29" colspan="3" align="right"><img src="images/linha.gif" width="885" height="29"></td>
    </tr>
    <?php
    }
    ?>
     
    Betoneira, Nov 4, 2006 IP
  20. Betoneira

    Betoneira Peon

    Messages:
    15
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #20
    $query = mysql_query("SELECT * FROM prpa") OR die( mysql_error() );
    $query1 = mysql_query("SELECT * FROM img WHERE name = '". mysql_real_escape_string($_GET['name']) ."'");
    $image = mysql_fetch_array($query1); :( <- this is line 64

    echo $image['content'];
    ?>

    ERROR:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/www/final.php on line 64
     
    Betoneira, Nov 4, 2006 IP