Finding the right db connections

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nomad
    Recognized Expert Contributor
    • Mar 2007
    • 664

    Finding the right db connections

    Hello everyone:
    I'm trying to learn PHP and I'm using this book to help me "PHP, MYSQL and Apache all in one" They have an example which is listed below. I'm using Dreamweaver to write the PHP and XAMPP to write the db.
    I have set up the db connection with Dreamweaver.
    My db is located in the default folder under XAMPP.

    I get this error:
    Incorrect database name 'storefront_db '


    [PHP] <?php
    session_start() ;
    //connect to database
    $conn = mysql_connect(" localhost", "nomad", "nomad") or die(mysql_error ());
    mysql_select_db ("storefront _db ",$conn) or die(mysql_error ());
    if ( POST[sel_item_id] != "") {
    //validate item and get title and price
    $get_iteminfo = "select item_title from store_items where id = POST[sel_item_id]";
    $get_iteminfo_r es = mysql_query($ge t_iteminfo) or die(mysql_error ());
    if (mysql_num_rows ($get_iteminfo_ res) < 1) {
    //invalid id, send away
    header("Locatio n: seestore.php");
    exit;
    } else {
    //get info
    $item_title = mysql_result($g et_iteminfo_res ,0,'item_title' );
    //add info to cart table
    $addtocart = "insert into store_shoppertr ack values ('', '$PHPSESSID', ' POST[sel_item_id]', ' POST[sel_item_qty]', ' POST[sel_item_size]', ' POST[sel_item_color]', now())";
    mysql_query($ad dtocart);
    //redirect to showcart page
    header("Locatio n: showcart.php");
    exit;
    }
    } else {
    //send them somewhere else
    header("Locatio n: seestore.php");
    exit;
    }
    ?>
    [/PHP]

    Any help would be great
    nomad
  • ronnil
    Recognized Expert New Member
    • Jun 2007
    • 134

    #2
    have you setup a database in mysql yet?
    if you have, please check for misspelling, if you haven't created a database yet, this is the cause of your problem :)

    Comment

    • ronnil
      Recognized Expert New Member
      • Jun 2007
      • 134

      #3
      uhhh just noticed you have a space in the database name on line 5

      [CODE=php]mysql_select_db ("storefront _db ",$conn) or die(mysql_error ());[/CODE]

      this is probably why, erase the space so it looks like:

      [CODE=php]mysql_select_db ("storefront_db ",$conn) or die(mysql_error ());[/CODE]

      Comment

      • nomad
        Recognized Expert Contributor
        • Mar 2007
        • 664

        #4
        Originally posted by ronnil
        uhhh just noticed you have a space in the database name on line 5

        [CODE=php]mysql_select_db ("storefront _db ",$conn) or die(mysql_error ());[/CODE]

        this is probably why, erase the space so it looks like:

        [CODE=php]mysql_select_db ("storefront_db ",$conn) or die(mysql_error ());[/CODE]
        The correction you suggested did not work. I get this error now
        Object not found!

        The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

        If you think this is a server error, please contact the webmaster

        Error 404
        localhost

        06/27/07 08:59:52
        Apache/2.2.4 (Win32) DAV/2 mod_ssl/2.2.4 OpenSSL/0.9.8e mod_autoindex_c olor PHP/5.2.1

        You're other question about the db being made. Yes it is created which is called storefront_db.D o I have to call up the directory to get to the storefront_db?
        Nomad

        Comment

        • ronnil
          Recognized Expert New Member
          • Jun 2007
          • 134

          #5
          Originally posted by nomad
          The correction you suggested did not work. I get this error now
          Object not found!

          The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

          If you think this is a server error, please contact the webmaster

          Error 404
          localhost

          06/27/07 08:59:52
          Apache/2.2.4 (Win32) DAV/2 mod_ssl/2.2.4 OpenSSL/0.9.8e mod_autoindex_c olor PHP/5.2.1

          You're other question about the db being made. Yes it is created which is called storefront_db.D o I have to call up the directory to get to the storefront_db?
          Nomad
          This means it's working :) cause you're being redirected to another page, which probably doesn't exist yet :)

          (a 404 error means exactly that the document you are trying to see cannot be found on the server, it has nothing to do with database connectivity)

          try commenting out your header('Locatio n .....); statements and make it echo something like 'redirect to thispage', 'redirect to thatpage' instead. So you can see where it's going :)

          Comment

          • nomad
            Recognized Expert Contributor
            • Mar 2007
            • 664

            #6
            Originally posted by ronnil
            This means it's working :) cause you're being redirected to another page, which probably doesn't exist yet :)

            (a 404 error means exactly that the document you are trying to see cannot be found on the server, it has nothing to do with database connectivity)

            try commenting out your header('Locatio n .....); statements and make it echo something like 'redirect to thispage', 'redirect to thatpage' instead. So you can see where it's going :)
            Thanks for the help
            I found out that one of my php files was in the wrong directory.
            I have another problem when I click a link on the main page it get this error
            Unknown column 'id' in 'field list'
            Here is the code

            [code=php]
            <?php
            //connect to database
            $conn = mysql_connect(" localhost", "nomad", "nomad") or die(mysql_error ());
            mysql_select_db ("storefront_db ",$conn) or die(mysql_error ());
            $display_block = "<h1>My Categories</h1>
            <P>Select a category to see its items.</p>";
            //show categories first
            $get_cats = "select id, cat_title, cat_desc from store_categorie s order by cat_title";
            $get_cats_res = mysql_query($ge t_cats) or die(mysql_error ());
            if (mysql_num_rows ($get_cats_res) < 1) {
            $display_block = "<P><em>Sor ry, no categories to browse.</em></p>";
            } else {
            while ($cats = mysql_fetch_arr ay($get_cats_re s)) {
            $cat_id = $cats[id];
            $cat_title = strtoupper(stri pslashes($cats[cat_title]));
            $cat_desc = stripslashes($c ats[cat_desc]);
            $display_block .= "<p><strong ><a href=\" [PHP]%4$s[/PHP] SERVER[PHP_SELF]?cat_id=$cat_id \">$cat_titl e</a></strong><br>$cat _desc</p>";
            if ( [PHP]%4$s[/PHP] GET[cat_id] == $cat_id) {
            //get items
            $get_items = "select id, item_title, item_price from store_items where cat_id = $cat_id order by item_title";
            $get_items_res = mysql_query($ge t_items) or die(mysql_error ());
            if (mysql_num_rows ($get_items_res ) < 1) {
            $display_block = "<P><em>Sor ry, no items in this category.</em></p>";
            } else {
            $display_block .= "<ul>";
            while ($items = mysql_fetch_arr ay($get_items_r es)) {
            $item_id = $items[id];
            $item_title = stripslashes($i tems[item_title]);
            $item_price = $items[item_price];
            $display_block .= "<li><a href=\"showitem .php?item_id=$i tem_id\">$item_ title</a></strong> (\$item_price)" ;
            }
            $display_block .= "</ul>";
            }
            }
            }
            }
            ?>
            <HTML>
            <HEAD>
            <TITLE>My Categories</TITLE>
            </HEAD>
            <BODY>
            <?php echo $display_block; ?>
            </BODY>
            </HTML>
            [/code]

            I looked at my fields in my db and them seemed to be same as my php.
            code.

            nomad

            Comment

            • ronnil
              Recognized Expert New Member
              • Jun 2007
              • 134

              #7
              this is from your sql query

              $get_cats = "select id, cat_title, cat_desc from store_categorie s order by cat_title";

              again make sure that no spaces have sneaked in to your code, this time it might be in the database setup.

              It's pretty dangerous to use just "id", i would prefer calling it cat_id. with multiple tables it can be confusing to have a column named id in each table, if it's not foreign keys.

              you could try using backqoutes in the statement making it look like

              $get_cats = "select `id`, `cat_title`, `cat_desc` from `store_categori es` order by `cat_title`";

              but this probably won't help, check out the database and see if you haven't accidentally sneaked a space before or after id

              Comment

              • nomad
                Recognized Expert Contributor
                • Mar 2007
                • 664

                #8
                Originally posted by ronnil
                this is from your sql query

                $get_cats = "select id, cat_title, cat_desc from store_categorie s order by cat_title";

                again make sure that no spaces have sneaked in to your code, this time it might be in the database setup.

                It's pretty dangerous to use just "id", i would prefer calling it cat_id. with multiple tables it can be confusing to have a column named id in each table, if it's not foreign keys.

                you could try using backqoutes in the statement making it look like

                $get_cats = "select `id`, `cat_title`, `cat_desc` from `store_categori es` order by `cat_title`";

                but this probably won't help, check out the database and see if you haven't accidentally sneaked a space before or after id
                Thanks for the all help.
                and I will try to rewrite the code, this is an exercise out of the book.
                The id are PK with auto_increments with not null.

                nomad

                Comment

                • nomad
                  Recognized Expert Contributor
                  • Mar 2007
                  • 664

                  #9
                  Originally posted by ronnil
                  this is from your sql query

                  $get_cats = "select id, cat_title, cat_desc from store_categorie s order by cat_title";

                  again make sure that no spaces have sneaked in to your code, this time it might be in the database setup.

                  It's pretty dangerous to use just "id", i would prefer calling it cat_id. with multiple tables it can be confusing to have a column named id in each table, if it's not foreign keys.

                  you could try using backqoutes in the statement making it look like

                  $get_cats = "select `id`, `cat_title`, `cat_desc` from `store_categori es` order by `cat_title`";

                  but this probably won't help, check out the database and see if you haven't accidentally sneaked a space before or after id
                  well I'm still getting the same error code
                  Unknown column 'id' in 'field list'
                  I think it has to deal with these line of code
                  while ($cats = mysql_fetch_arr ay($get_cats_re s)) {
                  $cat_id = $cats[id];
                  the next lines below that seem to work.

                  I even tried draging the fields into dreamweaver so I know I got the right fields names. It might be the id field itself because there are least 4 tables that use the same name.

                  nomad

                  Comment

                  • nomad
                    Recognized Expert Contributor
                    • Mar 2007
                    • 664

                    #10
                    Originally posted by nomad
                    well I'm still getting the same error code
                    Unknown column 'id' in 'field list'
                    I think it has to deal with these line of code
                    while ($cats = mysql_fetch_arr ay($get_cats_re s)) {
                    $cat_id = $cats[id];
                    the next lines below that seem to work.

                    I even tried draging the fields into dreamweaver so I know I got the right fields names. It might be the id field itself because there are least 4 tables that use the same name.

                    nomad
                    found the problem. Boy do I feel dumb
                    I had a field called int when it should have been id.
                    Thanks for the tip ronnil.
                    nomad

                    Comment

                    Working...