Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2004
    Posts
    3

    Unanswered: No Database Selected Error

    The following code is give me an error "No Database Selected"

    Could someone help me out ? Thanks !

    Ajay


    </head>
    <body>

    <?php
    $link = mysql_connect("localhost", "user", "pass")
    or die("Could not connect: " . mysql_error());
    echo "Connected successfully";


    $result = mysql_query('select col1, col2, col3 from mytable');
    if (!$result) {
    die('Invalid query: ' . mysql_error());
    }

    ?>

    <?php
    function mysql_result_all($result) {
    for($i = 0; $i < mysql_num_fields($result); $i++) {
    echo '<th>';
    echo mysql_field_name($result,$i);
    echo '</th>';
    }

    while($row = mysql_fetch_array($result)) {
    echo '<tr>';
    for($i = 0; $i < count($row); $i++) {
    echo '<td>'.$row[$i].'</td>';
    }
    echo '</tr>';
    }
    echo '</table>';
    }

    ?>

    <table>


    <?php

    mysql_result_all($result);
    mysql_close($link);
    ?>

  2. #2
    Join Date
    Oct 2003
    Location
    Slovakia
    Posts
    482

    Re: No Database Selected Error

    Originally posted by ajayc76
    The following code is give me an error "No Database Selected"

    Could someone help me out ? Thanks !

    Ajay


    </head>
    <body>

    <?php
    $link = mysql_connect("localhost", "user", "pass")
    or die("Could not connect: " . mysql_error());
    echo "Connected successfully";


    $result = mysql_query('select col1, col2, col3 from mytable');
    if (!$result) {
    die('Invalid query: ' . mysql_error());
    }

    ?>

    <?php
    function mysql_result_all($result) {
    for($i = 0; $i < mysql_num_fields($result); $i++) {
    echo '<th>';
    echo mysql_field_name($result,$i);
    echo '</th>';
    }

    while($row = mysql_fetch_array($result)) {
    echo '<tr>';
    for($i = 0; $i < count($row); $i++) {
    echo '<td>'.$row[$i].'</td>';
    }
    echo '</tr>';
    }
    echo '</table>';
    }

    ?>

    <table>


    <?php

    mysql_result_all($result);
    mysql_close($link);
    ?>
    you need add following after connection to database is opened:

    mysql_select_db("database_name");

Posting Permissions

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