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.
Results 1 to 8 of 8
  1. #1
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    Unhappy starting PHP - basic help required please

    Here is my php page insert.php
    Code:
    <?php
    
    $con = mysql_connect("localhost","john", "*****");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("my_db", $con);
    
    $sql="INSERT INTO person (FirstName, LastName, Age)
    VALUES
    ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "1 record added";
    
    mysql_close($con)
    ?>
    can someone please help me, my error says: Error: No database selected

    do i need to add databasename into this line:?
    $con = mysql_connect("localhost","john", "*****");

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Check the output after changing your code like
    Code:
    mysql_select_db("my_db", $con) or die(mysql_error());
    Last edited by abduraooft; 05-13-2008 at 12:59 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

  • #4
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    please help!

  • #5
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Try using this code, it's same except it works for me

    PHP Code:
    $username     "root";
    $password     "";
    $host         "localhost";
    $database    "igra";

    $link mysql_connect($host$username$password)
        or die(
    'Cant connect, wrong username or password: ' mysql_error());

    mysql_select_db($database) or die('Error connecting to database'mysql_error());
    ?> 

  • #6
    Regular Coder
    Join Date
    Mar 2008
    Posts
    118
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Have you created a database on your server?

  • #7
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    if you didn't create table then after you connect to database use this code
    PHP Code:
    <?php

    $sql 
    = ("CREATE yourdatabase") ;

    $link mysql_query($sql)or die (mysql_error());

    mysql_use_db("yourdatabase");

        echo 
    "Create table<br />
    "
    ;
        
    mysql_query('CREATE TABLE korisnici
                (id INT,
                name CHAR(20),
                password CHAR(40))'

        or die 
        (
    "Error creating table
    <blockquote style="
    background#cccccc; padding: 5px">". 
        
    mysql_error()
        .
    "</blockquote>");
        
    ?>
    there might be some parsing errors in that code :dunno:

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    Quote Originally Posted by jarv View Post
    please help!
    abduraooft tried but you ignored

    w3c code is valid but does not offer the same debugging as noted by abduraooft

    PHP Code:
    <?php
    mysql_connect
    ("localhost","john""*****") or die(mysql_error());
    mysql_select_db("my_db")or die(mysql_error());
    $q=mysql_query("SELECT * FROM $table")or die(mysql_error());
    print_r(mysql_fetch_assoc($q));

    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

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