SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member KoRn's Avatar
    Join Date
    Feb 2004
    Location
    FL
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    newbie questions on PHP/MySQL

    I'm still in the "newbie to PHP" area, so if someone could explain...

    I'm trying to use PHP to do the following, but I'm stuck...
    1) Open a connection to the MySQL Server and select the DB
    2) Create a new table
    3) Upload text-based info to the table
    4) Close the connection to the MySQL Server
    5) At any point, can come back & open a connection and read the text from the table

    Here's the coding I'm using to try and connect to MySQL
    and then proceede to create a table:

    PHP Code:
    <?php
    $dbh
    =mysql_connect ("localhost""user_name""user_password") or die ('I cannot connect to the database because: ' 

    mysql_error());
    mysql_select_db ("db_name"); 
    $sql 'CREATE TABLE `test` ( `myself` TEXT NOT NULL );';
    mysql_close($dbh);
    ?>
    I got the mysql_connect() part from in cPanel (MySQL section);
    and the $sql part from in phpMyAdmin (clicked "Create Code" after made a table).

    When I run the script I get a blank page, so as far as I know
    no errors...yet going into phpMyAdmin I see no new table by the name of 'test'

    Can somebody help w/ this problem?
    I'm really stuck; also if someone can explain what functions/variables
    to look up, so I can learn how to do what I listed in 1 and 5 (above)?

    Thnkx 4 any/all help

  2. #2
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe like this.

    PHP Code:
    $dbh mysql_connect ('localhost''user_name''user_password"') or die (mysql_error('I cannot connect to the database because'));
    mysql_select_db ('db_name'$dbh);
    mysql_query('CREATE TABLE `test` (`myself` TEXT NOT NULL )'$dbh); 

  3. #3
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You've assigned your SQL to $sql but not submitted it.
    After the line where you assign it, add the following:
    PHP Code:
    mysql_query($sql
    You could submit the SQL without assigning it to a variable in the following way:
    PHP Code:
    mysql_query("CREATE TABLE 'test' ( 'myself' TEXT NOT NULL );"
    but I think that it's better practice to use the method you are doing as it's clearer what's going on when your query becomes more complex.

    Other things to be aware of:
    • You're using a literal values of user_name, user_password and db_name to log on. Did you mean them to be variables? I assume that they're OK otherwise I'd have expected you to get errors.
    • Be careful with your quotes. I'm not sure what those things around 'test' and 'myself' are in your query, but I wouldn't expect them to work.

    Andy
    From the English nation to a US location.

  4. #4
    SitePoint Member KoRn's Avatar
    Join Date
    Feb 2004
    Location
    FL
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankx guys
    I decided to first try your suggestion, Andy,
    and I added mysql_query($sql) below the $sql line.
    It worked just great ~ thnkx I'd been
    struggling w/ this for a few days b4 this.

  5. #5
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's a while since I touched on PHP but I like an excuse to keep my hand in.

    PHP is quite simple once you get your head round it, however it's often hard to know where to begin when you're writing a PHP page that calls itself. After all, you're coding a bit of a mobius strip


    Good luck,

    Andy
    From the English nation to a US location.

  6. #6
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by awestmoreland
    Be careful with your quotes. I'm not sure what those things around 'test' and 'myself' are in your query, but I wouldn't expect them to work.
    Andy
    They work fine. It's what you get when you have "Enclose table and field names with backquotes" checked in phpmyadmin when exporting. They are also useful when using a non standard table name. Which of course is not recomended but will work with backquotes arround them.

  7. #7
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, I've not seen that before.
    I'm surprised that it doesn't simply utilise single quotes whilst surrounding the whole SQL string with double quotes (or vice-versa)

    Cheers for the info.

    Andy
    From the English nation to a US location.

  8. #8
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One of the best things you can do is to write a class to manage your database. In the long run it'll make your life as a programmer in PHP so much easier. There's so many available online if you're not up to it. Just google for 'php mysql class'.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •