SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question help me please !!!!!!!!

    hey guys,

    I created a table in the mySQL database. Now, i want to insert values into it via php coding. I'm new to php. This is the code I typed :


    PHP Code:
    <html> 
    <body> 
    <?php 
    $db 
    mysql_connect("localhost""root","sqlserver"); 
    mysql_select_db("student",$db); 
    $sql "INSERT INTO ece_students VALUES ('dilip',20,'dil2dilip@yahoo.co.in','GVP Engg. Collg.',10); 
    ?> 
    <body> 
    <html>


    I want to get clarified about certain basic things :

    1.) Should I save the above page as .html or .php? Please explain why ?
    2.) How does the above code ensure that it has established a connection with mySQL server ?
    3.) Does the process of getting connected to mySQL server involve ODBC bridge or it's likes. If yes, please explain how's it configured ?
    4.) What should I type in the address bar of iexplore, to view the above page ?
    5.) How does a php code connect to mySQL database server(I want to know the underlying process) ?

  2. #2
    Non-Member betterpc's Avatar
    Join Date
    Dec 2004
    Location
    EU
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Save as php.

    To be sure that connection is made type:
    mysql_connect("localhost", "root","sqlserver") or die(mysql_error());

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    153 Post(s)
    Tagged
    3 Thread(s)
    1.) Should I save the above page as .html or .php? Please explain why ?
    The page has to be saved as php to tell the server how to parse (process) the code on the page.
    2.) How does the above code ensure that it has established a connection with mySQL server ?
    This code wont. You need to tell the server what to do if a connection fails.
    Use betterpc's code.
    3.) Does the process of getting connected to mySQL server involve ODBC bridge or it's likes. If yes, please explain how's it configured ?
    No. The code does it all.
    4.) What should I type in the address bar of iexplore, to view the above page ?
    If you have saved it as mypage.php for example, it would be http://www.mysite.com/mypage.php

    5.) How does a php code connect to mySQL database server(I want to know the underlying process) ?
    From the manual.
    resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]])


    Returns a MySQL link identifier on success, or FALSE on failure.

    mysql_connect() establishes a connection to a MySQL server. The following defaults are assumed for missing optional parameters: server = 'localhost:3306', username = name of the user that owns the server process and password = empty password.

    The server parameter can also include a port number. eg. "hostname:port" or a path to a local socket eg. ":/path/to/socket" for the localhost.

    Note: Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as mysql.default_host in your PHP configuration and leave the server field blank.

    Support for ":port" was added in PHP 3.0B4.

    Support for ":/path/to/socket" was added in PHP 3.0.10.

    You can suppress the error message on failure by prepending a @ to the function name.


    If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. The client_flags parameter can be a combination of the constants MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE or MYSQL_CLIENT_INTERACTIVE.

    Note: The new_link parameter became available in PHP 4.2.0

    The client_flags parameter became available in PHP 4.3.0

    The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close().
    HTH

    SpikeZ
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi spikeZ,
    In response to the 4th query, you asked me to type http://www.mysite.com/mypage.php in order to see my page in the iexplore.

    However, since, my server here is the localhost itself, I typed http://localhost/test.php.

    Then "Page Cannot be displayed" is the error. What to do ???????

  5. #5
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Staffordshire, UK & Florida, USA
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is the php file located in your web server's document root and have you got PHP installed and configured correctly and is your webserver setup and serving pages?

    Have you got any PHP pages to run? You could try a simple file first to ensure that all is well. Make a php file containing the following: <?php phpinfo(); ?> saved as somefile.php and put it in your webserver directory.

    I noticed that in your code example you have set a variable called $sql with your query but you're not actually executing the query. As you are not producing any output you will get a blank HTML page.

    What webserver/OS are you using?

  6. #6
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    695
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by djones
    What webserver/OS are you using?
    I was wondering if there was any webserver running!

    PHP Code:
    $sql "INSERT INTO ece_students VALUES ('dilip',20,'dil2dilip@yahoo.co.in','GVP Engg. Collg.',10); 
    Don't you want to put '20' instead of 20??? and again around the 10, make it '10' (or is that just me being in a bad practice cos I ALWAYS do that!)

  7. #7
    - Michael - Bytecon's Avatar
    Join Date
    Mar 2004
    Location
    Ireland
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <html> 
    <body> 
    <?php 
    $db 
    mysql_connect("localhost""root","sqlserver"); 
    mysql_select_db("student",$db); 
    $sql "INSERT INTO ece_students VALUES ('dilip','20','dil2dilip@yahoo.co.in','GVP Engg. Collg.','10')";
    mysql_query($sql);
    ?> 
    <body> 
    <html>
    Is that right ?? I always thought you needed to do the column names in the query..

  8. #8
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you use INSERT INTO table_name VALUES (...), then you MUST specifiy data for every single column defined in table_name in the correct order.

    Better would be INSERT INTO table_name(col1, col2...) VALUES (val1, val2...)

    You should never put single quotes around numeric data. Single quotes should be used for textual and data data only.
    MySQL lets you get away with using single quotes around numeric data, but it is non-standard.

    In the original post

    • the $sql line is missing a closing double quote. This is fixed in Mickster's post (#7)
    • the query ($sql) is never executed. This is fixed in Mickster's post (#7)

  9. #9
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    695
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by swdev
    You should never put single quotes around numeric data. Single quotes should be used for textual and data data only.
    MySQL lets you get away with using single quotes around numeric data, but it is non-standard.
    Never? Is there a reason for this? I'm still relatively new to PHP and use only basic functions, so I'm not very 'up' on what you do and don't do, I tend to always do it because it works, that's all!

  10. #10
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    guys, my webserver is running ofcourse. u suggested me to put the file in the webserver's document directory. where is that ?? what should I type in iexplore to see this page via webserver. My webserver is mySQL server having hostname - localhost, and username - root and password - sqlserver. Isn't this info (that I typed in php code) sufficient to connect to the server via php pages ? If no, please explain what should I do to see my page display in the iexplore.

    Note: MySQL server is installed in d:\program files\mysql\mysql server 4.1\

  11. #11
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    695
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your webserver is NOT MySQL, that is a Database Server! You should be running something like Apache or IIS (I assume you're running Wndows from the location of MySQL?) If you install Apache, you will find a folder within it called HTDOCS. This is the default location for websites. Drop your file in there (Assuming you have PHP installed too?)

    What do you see if you just go to http://localhost/ ???

    If you're not sure what you're doing with servers etc, use a pre-made package such as WAMP, which installs Apache, MySQL and PHP on a Windows PC. It also gives you some nice tools on your local machine such as PHPMyAdmin.


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
  •