create a database with php

Discussion in 'MySQL' started by iphetamine, Jun 22, 2008.

  1. #1
    I would like to create a database using MySQL and php. But because of php, i can only access through my website, not a stand alone database?

    What I want is to have a database that I could type in the generic or scientific name of the product and comes up with the details of that product. How can I do that? I just started learning mysql and php.

    Thanks
     
    iphetamine, Jun 22, 2008 IP
  2. catapop

    catapop Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    most of hosting have cpanel and there is a option when you can create a db. ( i don't think that you'll have the privilege of doing that from php)
    but if you can you can find the syntax here dev.mysql.com/doc/refman/5.0/en/create-database.html
    :)

    but i think that you are talking about a table :) try mysql.com for more documentation
     
    catapop, Jun 22, 2008 IP
  3. harley

    harley Member

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Assuming you have rights to create a database and create a table, try..

    
    <?
    
    $connect = mysql_connect("host","username","password");
    mysql_query("CREATE DATABASE somename",$connect) or die(mysql_error());
    
    mysql_query("CREATE TABLE products (
      id INT(11) default NULL auto_increment,
      productGenericName char(120) default NULL,
      productScientificName char(120) default NULL,
      productDescription text default NULL,
      PRIMARY KEY (id)
    ) ENGINE=MyISAM") or die(mysql_error());
    
    mysql_close($connect);
    
    ?>
    
    PHP:
     
    harley, Jun 22, 2008 IP
  4. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #4

    If you want to do this on your local machine you will need to install Apache and mysql. You will still have to use your browser but it will all be hosted on your machine. Check out Xampp or any of the othe complete packages.

    More questions?
     
    Colbyt, Jun 22, 2008 IP
  5. iphetamine

    iphetamine Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    <snip>

    nvm, fixed it.
     
    iphetamine, Jun 23, 2008 IP
  6. iphetamine

    iphetamine Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    OK ppl, i have wamp installed, played around with mysql console and phpmyadmin, created db's and tables.

    Cool..

    Now, I'm trying to connect php to mysql, but I get "download index.php" when I load the file in IE.
    Code I used:

    <?php
        mysql_connect("localhost", "root", "xxxx") or die(mysql_error());
        echo "Connected to MySQL<br />";        
    ?>
    PHP:

    If it worked, it was supposed to say "Connected to MySQL ", according to a tutorial.

    Anything I'm missing?
     
    iphetamine, Jun 24, 2008 IP
  7. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #7
    PHP is not configured properly.
     
    mwasif, Jun 24, 2008 IP
  8. iphetamine

    iphetamine Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    ^like what?
     
    iphetamine, Jun 24, 2008 IP
  9. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #9
    Create a simple page with the following code and save in the document root and access it through browser. If you see any output it means your PHP is configured properly.

    <?php
    phpinfo();
    ?>
    Code (markup):
     
    mwasif, Jun 24, 2008 IP
  10. sitehost

    sitehost Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    If you are using some form of wamp, the most common reason for the server downloading files instead of rendering them is that your configuration file (httpd.conf) is missing the line that tells it to parse PHP files. Try adding this line to the Apache configuration file if it's not already there.

    AddType application/x-httpd-php .php
     
    sitehost, Jun 25, 2008 IP
  11. iphetamine

    iphetamine Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I have created a database and a table, was able to use php and mysql display the information onto a webpage using query, fetch_array, and loops.

    Now I want to have a field (or search box?) so the user can enter the name of the product and it's related details appear.
     
    iphetamine, Jun 26, 2008 IP
  12. Riverofrhyme

    Riverofrhyme Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #12
    A quick show:

    
    <?php
    
    if($_POST['submit']){
    $user = $_POST['user']; // YOU MUST SANITIZE THIS
    $qu = mysql_query("SELECT * FROM tblname WHERE username='$user'")or die(mysql_error());
    echo 'There were '.mysql_num_rows($qu).' matches!';
    }else{
    echo '
    <form method="post">
    Username to search: <input type="text" name="user"/>
    <input type="submit" name="submit" value="Search"/>
    </form>
    ';
    }
    
    PHP:
     
    Riverofrhyme, Jun 26, 2008 IP
  13. iphetamine

    iphetamine Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Hey man, thanks but it seems not work? :S
     
    iphetamine, Jun 29, 2008 IP
  14. xfreex

    xfreex Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    because action is empty
    <form method="post" action="">
     
    xfreex, Jun 29, 2008 IP
  15. Riverofrhyme

    Riverofrhyme Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #15
    xfreex, do you need action there? I've never had action in any of my forms, but it seems to work.

    I did miss out the closing php tag though, here you go:

    <?php
    
    if($_POST['submit']){
    $user = $_POST['user']; // YOU MUST SANITIZE THIS
    $qu = mysql_query("SELECT * FROM tblname WHERE username='$user'")or die(mysql_error());
    echo 'There were '.mysql_num_rows($qu).' matches!';
    }else{
    echo '
    <form method="post">
    Username to search: <input type="text" name="user"/>
    <input type="submit" name="submit" value="Search"/>
    </form>
    ';
    }
    
    ?>
    
    
    PHP:
     
    Riverofrhyme, Jun 29, 2008 IP
  16. iphetamine

    iphetamine Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    ^it wasn't about the closing tag, i had it included, but it sitll didn't work. And the action didn't make a difference.

    I'm getting this: Notice: Undefined index: submit in D:\wamp\www\php_sandbox\index.php on line 3

    Any ideas?



    And in place of "user" I put the username I have for the database, correct?
    I did that, and when I search for a pdt, I get "No database selected". There's no place to add the database name in the code, only the table name.
     
    iphetamine, Jun 29, 2008 IP