New to PHP - need help

Discussion in 'PHP' started by DarrenC, Aug 11, 2006.

  1. #1
    If I have a mySQL database, and I want to call the data from a specific field in the database, i.e. <? echo $field1 ?> what code should I be adding to the .php file to call the data?

    I have created a .inc.php file with database username, password etc. but I'm not sure where to go from here.

    This is my first attempt at PHP.

    Darren
     
    DarrenC, Aug 11, 2006 IP
  2. sandossu

    sandossu Guest

    Messages:
    2,274
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    0
    #2
    we have the file that connects the database: db.inc.php
    the database table:
    test
    ->id
    ->name

    the script that gets 'name' field:
    
    <?php
    $sql = "select * from test where id=1"; //the query
    $query = mysql_query($sql) or die("Error: ".mysql_error());
    $row = mysql_fetch_assoc($query);
    echo $row['name']; //name is the specific field
    
    ?>
    
    PHP:
     
    sandossu, Aug 11, 2006 IP
  3. DarrenC

    DarrenC Peon

    Messages:
    3,386
    Likes Received:
    154
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, but I don't want to run a query.

    I want to call up the mysql database, and then call up the contents of a field in the database. Simple example:

    <title>My favourite DVD - <? echo $film ?></title>

    The field is film within a table called dvds and the table is within a mysql database. I want the title to be <My favourite DVD - Pirates of the Caribbean</title>

    Pirates of the Caribbean is the default value in the field 'film'

    I know this sounds a silly scenario but I am trying to learn mysql and php so at the moment its just to test and play around. Hope this is understandable.
     
    DarrenC, Aug 11, 2006 IP
  4. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ehm... You HAVE to do a query before you can show anything. You might want to do a few tutorials first.
     
    T0PS3O, Aug 11, 2006 IP
  5. ablaye

    ablaye Well-Known Member

    Messages:
    4,024
    Likes Received:
    97
    Best Answers:
    0
    Trophy Points:
    150
    #5
    True. The title "$film" that you are trying to display has to be queried (i.e. retrieved) from the database.
     
    ablaye, Aug 11, 2006 IP
  6. DarrenC

    DarrenC Peon

    Messages:
    3,386
    Likes Received:
    154
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok, off to buy a PHP book I go :)
     
    DarrenC, Aug 11, 2006 IP
  7. Gamer Unlimited

    Gamer Unlimited Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You'll find that once you get started with PHP, it takes on a snowball effect and only gets easier from there. :)
     
    Gamer Unlimited, Aug 11, 2006 IP
  8. casper

    casper Guest

    Messages:
    181
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    One small hint they usually tend to forget in PHP manuals; Put the database-connection-code into an include file so you won't have to code it everytime. When you have created the include file, simply call it before a query, like:

    
    include('inc.dbconnect.php');
    $result = mysql_query("SELECT * FROM tablename;");
    
    Code (markup):
    mod.dbconnect.php:
    
    <?php
    $db = mysql_connect("localhost","root","password here");
    mysql_select_db("dbname",$db) or die("error!");
    ?>
    
    Code (markup):
    Never name an include file *.inc! Most books say you should use the .inc extension but the php server doesn't recognize .inc as a php file and will parse the file as a normal textfile, thereby exposing your code. Of course you can use htaccess to ignore it but you are just adding an extra security risk.
     
    casper, Aug 12, 2006 IP
  9. Jordash

    Jordash Peon

    Messages:
    181
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    http://www.webmonkey.com/webmonkey/programming/php/tutorials/tutorial4.html

    That tutorial got me started in PHP, really great, I suggest anyone trying to learn PHP to read it. Just skip the first 2-3 pages that talk about how to setup PHP on the server and go to the actual PHP coding. It also teaches you how to interact with the MySQL server without PHPMyAdmin, You can do all the MySQL stuff he talks about in PHPMyAdmin though just in the MySQL query box.
     
    Jordash, Aug 12, 2006 IP
  10. Jordash

    Jordash Peon

    Messages:
    181
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    This is very true unless your host has configured the .inc file to be recognized as a .php, otherwise someone could easily find your Database username and password. :eek:
     
    Jordash, Aug 12, 2006 IP
  11. Mayhem Design

    Mayhem Design Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Never a truer word sopken!! I used to use .inc files until I met a guy who "as a friend" hacked a system of mine. Luckily he was a friend and he told me about it straight away and helped me fix the security issues, but as Casper said NEVER name a file .inc!!
     
    Mayhem Design, Aug 14, 2006 IP
  12. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #12

    The other posters have it right. Just because your host has configured this does not mean that you should do it. Always code in a manner which will work as expected on any machine and under any OS.

    You never know when you need to move or when your host may change their policies to reflect a more "standards compliant" approach to using PHP and other programs.
     
    clancey, Aug 14, 2006 IP
  13. netaddict

    netaddict Peon

    Messages:
    640
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #13
    netaddict, Aug 15, 2006 IP
  14. raziel

    raziel Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    After that you might want to get pre-coded classes which will make life easier.
    phpclasses.com
     
    raziel, Aug 18, 2006 IP
  15. georgiecasey

    georgiecasey Member

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #15
    U probably mean http://www.phpclasses.org
    Good resource but the amount of ads is shocking. So annoying. You come to expect ad free sites with the open source movement but I suppose they have to pay for hosting
     
    georgiecasey, Aug 19, 2006 IP