Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: create database

  1. #1
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts

    create database

    Hello I'm pretty new to php... I downloaded the phpdev5 version. I used php to code up to create a database
    Code:
    <?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'mypass';
    
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
    
    $query = "Create DATABASE phpcake";
    $result = mysql_query($query);
    
    mysql_select_db($dbname);
    
    mysql_close($conn);
    ?>
    I saved this as data.php in the www folder, then I ran it but I don't know where the database goes to? I cannot find a database/folder called phpcake. Any idea about this? Also I created a folder called php in that www folder, but then I cannot access it, why? Please help!

  • #2
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Thats because its MySQL and stored in MySQL. You need to use phpMyAdmin installed on your server (if it is) and edit from there, also its better to create columns in your table while creating it.

  • #3
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have phpmyadmin ... so you are saying I should save that data.php in that phpmyadmin folder? but then when I typed localhost on the web browser, I cannot see the folder phpmyadmin, then how am I going to run that data.php?

  • #4
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    No... you use phpMyAdmin to access MySQL database, PHP only uses MySQL.

    Consider PHP your handle to open MySQL data and output it.

  • #5
    Regular Coder
    Join Date
    Sep 2005
    Location
    Liverpool
    Posts
    226
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Element
    PHP only uses MySQL.
    *cough*
    my mind is on a permanent tangent

  • #6
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Yeah, not the best way to put it.

    More like, when you make a database its not like fopen() where you'll find mytable.db or something. All the information is stored privately by MySQL where it can't be accessed on the public side.

  • #7
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh I kinda get it... but how can I get the data from the database to print to the php page? I have this code:

    Code:
    <?php
    include 'config.php';
    include 'opendb.php';
    
    $query  = "SELECT name, subject, message FROM contact";
    $result = mysql_query($query);
    
    while($row = mysql_fetch_assoc($result))
    {
        echo "Name :{$row['name']} <br>" .
             "Subject : {$row['subject']} <br>" . 
             "Message : {$row['message']} <br><br>";
    } 
    
    include 'closedb.php';
    ?>
    Where shall I save it to? I mean which folder?

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Folder? You want to cache it now?
    That code should work.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #9
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah I have that codes, say I call it read.php then where shall I save it to? is it to the www directory folder? Or in mysql folder? OR in phpmyadmin folder? by the way where can I find the password of the user of database?

  • #10
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Call the server admin. If you are the admin... use phpmyadmin to add a user/change your password.
    A MySQL installation comes with a user called root with no password.

    You can put the file where you want... as long as it's in the servers web root. You should move the included files aswell
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #11
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I went into the phpmyadmin and created a table called university

    University (table)
    Name (row)
    David (row)

    Then I coded

    Code:
    <?php
    $dbhost = 'localhost';
    $dbuser = 'phpbie';
    $dbpass = 'mypass';
    
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
    
    $query  = "SELECT * FROM University";
    $result = mysql_query($query);
    
    while($row = mysql_fetch_assoc($result))
    {
        echo "Name: {$row['name']} <br>";
    } 
    
    mysql_close($conn);
    ?>
    I put this data.php file in the root directory, when I ran it , it gives me nothing not even error message.

    Please help!

  • #12
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Blank page?
    Add error_reporting(E_ALL); at the top and add "print 'hello';" to check if the PHP is really being executed. Also change
    $result = mysql_query($query);
    to
    $result = mysql_query($query) or die(mysql_error());
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #13
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It says

    hello No database selected

    Did I write the code wrong? There's a database there.

  • #14
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh forgot to put the database name there... well it prints

    hello name:

    I want to print that name David out

    Code:
    <?php
    error_reporting(E_ALL); 
    
    $dbhost = 'localhost';
    $dbuser = 'phpbie';
    $dbpass = 'mypass';
    $dbname = 'phpbie';
    
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
    
    mysql_select_db($dbname);
    $query  = "SELECT name FROM university";
    $result = mysql_query($query) or die(mysql_error());
    
    while($row = mysql_fetch_assoc($result))
    {
        echo "Name: {$row[0]} <br>";
    } 
    
    mysql_close($conn);
    ?>

  • #15
    Regular Coder
    Join Date
    Aug 2005
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Problem solved

    Cheers!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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