0

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.

3
Contributors
17
Replies
44
Views
4 Years
Discussion Span
Last Post by akkbkht
Featured Replies
  • 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> Read More

  • 1
    IIM 163   4 Years Ago

    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. Read More

  • ^ 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. Read More

  • 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. Read More

  • 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> Read More

1

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>

Edited by asprin

1

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.

0

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

1

^ 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.

Edited by asprin

0

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?

1

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.

0

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>
1

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>

Edited by asprin

0

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...

1

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 />";
}

Edited by asprin

0

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.

0

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.

1

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.

0

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.

1

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.

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.