Hello.
Please guide me about how to get the options of select tab from mYSQL table.
Where to insert php code and how to?

Have a look on the code.

<td><select name="tby" size="1">
              </select></td>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td colspan="4"><label>Issue/ Tests :
                  <textarea name="issue" wrap="physical"></textarea>
              </label></td>

Regards.
Aamir Karim.

You could do something like this:

$q = mysql_query("SELECT * FROM tablename");
$options = '';
while($r = mysql_fetch_array($q))
{
  $options .= '<option value="'.$r['id'].'">'.$r['columnValue'].'</option>';
}

Then for HTML, you can do

<select name="tby" size="1">
<?php echo $options; ?>
</select>

Write select tag and between select start and end tag, write sql statement to get data from table. Iterate it and add value in option tag.

IIM
Please explain a bit with An exapmle?

Asprin.
The where to write the first 6 lines?
In script tag between head tag?

^ You can place that anywhere. But make sure you keep it before the select tag. Also, it's not a javascript code. It's pure PHP code. So don't put it between script tags. Put it inside <?php ?> tags.

Asprin.
When executing the code, Now iam recieving this error messages on run time

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\patient.php on line 96

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\patient.php on line 96

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\patient.php on line 98

however, I have tried to supply user info but still same errors.

Any Idea?

You first need to connect to your database.

mysql_connect('localhost', 'username', 'password') or die('Connection could not be established');
mysql_select_db('database_name') or die('Database is not present');

Put this right at the top of the page inside <?php ?> tags. Replace the username, password, and database_name accordingly.

I have used the code like this,

<?php 

                 $q = mysql_query("SELECT * FROM doctor_t");
    $options = '';
    while($r = mysql_fetch_array($q))
    {
    $options .= '<option value="'.$r['id'].'">'.$r['dname'].'</option>';
    }

    ?>
                  <select name="tby" size="1">
    <?php 

    echo $options; 

    ?>
    </select>

Try with this

<?php
        mysql_connect('localhost', 'username', 'password') or die('Connection could not be established');
        mysql_select_db('database_name') or die('Database is not present');
        $q = mysql_query("SELECT * FROM doctor_t");
        $options = '';
        while($r = mysql_fetch_array($q))
        {
            $options .= '<option value="'.$r['id'].'">'.$r['dname'].'</option>';
        }
        ?>
        <select name="tby" size="1">
        <?php
        echo $options;
        ?>
        </select>

Nope. Not working. One more this there is no password on my db

<?php
                     //Server= localhost, Username= root, pasword = "there is no password" and db name = bsc_db.
    mysql_connect('localhost', 'root', '') or die('Connection could not be established');
    mysql_select_db('bsc_db') or die('Database is not present');
    $q = mysql_query("SELECT * FROM doctor_t");
    $options = '';
    while($r = mysql_fetch_array($q))
    {
    $options .= '<option value="'.$r['id'].'">'.$r['dname'].'</option>';
    }
    ?>
    <select name="tby" size="1">
    <?php
    echo $options;
    ?>
    </select>

Its showing message that Database is not present however i have just checked DB in localhost/xampp. Its showing there...

That means that the connection is successful, but it can't find the database named bsc_db for the user root with no password. Are you sure you're spelling bsc_db correctly? For debugging purpose, what is the output for the below code?

mysql_connect("localhost", "root", "");
$q = mysql_query("SHOW DATABASES");

while ($row = mysql_fetch_assoc($q)) {
    echo $row['Database'] . "<br />";
}

Same error.
"Database is not present"
I don't know why this is generating this error?
While I am able to work with the same database using xampp. Means localhost/xampp

And the spelling I am using are correct. I have checked again.

Its now working perfectly. The connection code was inserted twice mistakenly. I gop through the whole code and removed the one which was not needed. Not it is working fine.
For the refrence, I am adding the working code.

<?php
                     //Server= localhost, Username= root, pasword = "there is no password" and db name = bsc_db.
    mysql_connect('localhost', 'root', '') or die('Connection could not be established');
    mysql_select_db('bsc_db') or die('Database is not present');
    $q = mysql_query("SELECT * FROM doctor_t");
    $options = '';
    while($r = mysql_fetch_array($q))
    {
    $options .= '<option value="'.$r['id'].'">'.$r['dname'].'</option>';
    }
    ?>
    <select name="tby" size="1">
    <?php
   echo $options;
    ?>

Thanks alot asprine and IIM.

GOD bless you.

Aamir Karim.

Create a new php file and only use the below code:

 mysql_connect("localhost", "root", "");
    $q = mysql_query("SHOW DATABASES");
    while ($row = mysql_fetch_assoc($q)) {
    echo $row['Database'] . "<br />";
    }

The reason you got "Database is not present" is because you had die() at the top of the current page. So put only the above code in a new php file and run and see what output you get.

The following result is displayed when i put the above code on a fresh blank page....

information_schema
bsc_db
cdcol
mysql
phpmyadmin
test
webauth

As I told you in my last message, Mistakenly I put the connection code twice. Then I find out the extra one and delete that. Now its working fine...

Thanks alot.
I am learning alot of new things. My intrest towards php is increasing minute by minute...

Once gain Thanks.

You're welcome. I saw your resovled post after I made the last post so that's why didn't knew that it was solved.

Haha. I think we posted our reply at the very same time that's why missed each others replies.
Nevermind. However, my code is executing very fine now and its fetching data from the table very fine and perfectly.
Regards.
Aamir.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.