SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Resource id #31 ? Connecting to database?

    I am a complete newbee and i am trying to connect to my sqldatabase.
    Previously the connection to sql worked but when i try to connect to sql and my database & echo a confirm i get the following appear on my screen.
    Resource id #31

    What is this?

    My code is below,

    <?php
    $connect_to_sql = mysql_connect("localhost", "username", "password")or die
    ("Could not connect");
    if ($connect_to_sql)
    {$sql_confirmed = "SQL Connected";
    }

    $connect_to_database = mysql_select_db("tablename") or die
    ("Could not connect to the database");
    if ($connect_to_database)
    {$database_confirmed = "Database Connected";
    }
    ?>
    <html>
    <head>
    <title>Untitled</title>
    </head>
    <body>
    <?php
    echo "$connect_to_sql";
    echo "$connect_to_database";
    mysql_close();
    ?>
    </body>
    </html>

  2. #2
    SitePoint Zealot Servyces's Avatar
    Join Date
    Jun 2007
    Location
    The Netherlands
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are trying to echo the connect commands. This won't work, here's what you should be doing instead:

    PHP Code:
    <?php
    $connect_to_sql 
    mysql_connect("localhost""username""password") or die("Could not connect");
    if (
    $connect_to_sql) {
        echo 
    "SQL Connected<br>";
    }

    $connect_to_database mysql_select_db("tablename") or die("Could not connect to the database");
    if (
    $connect_to_database) {
        echo 
    "Database Connected<br>";
    }
    ?>
    If it connects, it echo's the OK message. If it doesn't connect it dies with the die message you already specified.
    Servyces.com
    Where itís all about you.
    Your partner in online solutions.
    Visit our website at http://www.servyces.com/

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,036
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    resource

    Hi lord_of_the_ring, welcome to the forums,
    mysql_connect returns a resource http://us2.php.net/mysql_connect
    Return Values
    Returns a MySQL link identifier on success, or FALSE on failure.
    if you echo your var and see "resource" that means the cnx was successful.

  4. #4
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all, the function mysql_connect() returns a resource, which is the connection to the database server. And the function mysql_select_db() returns a boolean to check if the database can be used or not to extract, alter or add data to the database.

    Secondly, mysql_select_db() selects a database, not a table.
    To get data from the database, you'll need to query the database.
    For example:
    Code PHP:
    <?php
        // This query gets all rows and columns from the table TableName
        $result = mysql_query('Select * from TableName');
    ?>

    The function mysql_query() returns another resource, which contains all the rows and columns returned by the database that meet your query.
    To then use the returned results, you'll have to walk through the returned resource:
    PHP Code:
    <?php
        
    // This is the easiest way to learn
        
    for ($row mysql_fetch_array($result)) {
           echo 
    $row['FieldName1'];
            echo 
    $row['FieldName2'];
        }
    ?>
    The function mysql_fetch_array() returns an array with the fieldnames as keys of the array -you can get the values by their corresponding numeric keys too- and the values as of the fields as the value linked to their corresponding array key.
    Every time you call mysql_fetch_array($result), you'll get one row from the resource the database returned. If there are no more rows available in the resource, the for-loop will be ended.
    FOR SALE: 1 set of morals, never used, will sell cheap

  5. #5
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys... i managed to get it working but have run into a problem trying to call the data from the table...

    I followed hexburners advice but i get the following error,

    Parse error: syntax error, unexpected T_STRING in db_connect_new.php on line 25

    line 25 starts with "$result = mysql_query('SELECT *
    FROM `tablename`);"

    My code is below... any ideas what i have done wrong this time?

    PHP Code:
    <html>
    <head>
    <title>Untitled</title>
    </head>
    <body>
    <?php
    $connect_to_sql 
    mysql_connect("localhost""username""password")or die
    (
    "Could not connect");
    if (
    $connect_to_sql)
    {echo 
    "SQL Connected";
    }

    $connect_to_database mysql_select_db("tablename") or die
    (
    "Could not connect to the database");
    if (
    $connect_to_database)
    {echo 
    "Database Connected";
    }
    $result mysql_query('SELECT *
    FROM `tablename`);
    for ($row = mysql_fetch_array($result)) {
           echo $row['
    FieldName1'];
            echo $row['
    FieldName2'];
        }
    ?>
    </body>
    </html>
    Last edited by lord_of_the_ring; Jun 28, 2007 at 13:13.

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,036
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    delimiters

    The quote delimiters should be in pairs.
    PHP Code:
    $result mysql_query('SELECT * 
    FROM `tablename`); 

  7. #7
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    The quote delimiters should be in pairs.
    PHP Code:
    $result mysql_query('SELECT * 
    FROM `tablename`); 
    I don't think that's enough info for lotr

    PHP Code:
    // Some examples:
    $result mysql_query('Select * from tablename');
    $result mysql_query('Select * from \'tablename\'');
    $result mysql_query("Select * from 'tablename'"
    FOR SALE: 1 set of morals, never used, will sell cheap

  8. #8
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    The quote delimiters should be in pairs.
    PHP Code:
    $result mysql_query('SELECT * 
    FROM `tablename`); 
    Your forgot a closing quote:
    PHP Code:
    $result mysql_query('SELECT * 
    FROM `tablename`'
    ); 

  9. #9
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried all 3 variations of that but i still get the same error...
    Parse error: syntax error, unexpected ')', expecting ';' in db_connect_new.php on line 23

    My code is now,

    PHP Code:

    <html>

    <head>

    <title>Untitled</title>

    </head>

    <body>

    <?php

    $connect_to_sql 
    mysql_connect("localhost""username""password")or die

    (
    "Could not connect");

    if (
    $connect_to_sql)

    {echo 
    "SQL Connected";

    }



    $connect_to_database mysql_select_db("tablename") or die

    (
    "Could not connect to the database");

    if (
    $connect_to_database)

    {echo 
    "Database Connected";

    }

    $result mysql_query("SELECT *

    FROM 'tablename'"
    );

    for (
    $row mysql_fetch_array($result)) {

           echo 
    $row['FieldName1'];

            echo 
    $row['FieldName2'];

        }

    ?>

    </body>

    </html>
    Last edited by lord_of_the_ring; Jun 28, 2007 at 13:13.

  10. #10
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    while ($row mysql_fetch_array($result)) {
           echo 
    $row['FieldName1'];
           echo 
    $row['FieldName2'];
     } 
    Use a while loop, a for each loop is expecting an array.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  11. #11
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did try that but i now i get the following error.. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in db_connect_new.php on line 23
    Last edited by lord_of_the_ring; Jun 28, 2007 at 13:11.

  12. #12
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That message means your sql query is not returning a valid recordset. Is 'tablename' the name of the actual table you are retrieving data from ?
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  13. #13
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is defintely the correct name... i can even insert into it but just can't seem to list the results using this code.

  14. #14
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its ok.. i got it working, it was to do with the line above.

    Thanks

  15. #15
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is your database named 'tablename' also ?
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.


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
  •