Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7992873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:41:41+00:00 2026-06-04T13:41:41+00:00

I am having difficulties grasping the concept of echoing a variable from a database

  • 0

I am having difficulties grasping the concept of echoing a variable from a database through php. Instead getting a variable in string format i get an integer. How can i display the category string variable through the select box?

here is the info in my database:

CREATE TABLE `category` (
   `c_id` tinyint(4) not null auto_increment,
   `category` varchar(30) not null,
   PRIMARY KEY (`c_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5;

INSERT INTO `category` (`c_id`, `category`) VALUES ('1', 'Animals >');
INSERT INTO `category` (`c_id`, `category`) VALUES ('2', 'Humans >');
INSERT INTO `category` (`c_id`, `category`) VALUES ('3', 'Technology >');
INSERT INTO `category` (`c_id`, `category`) VALUES ('4', 'Oceans >');
INSERT INTO `category` (`c_id`, `category`) VALUES ('5', 'Desert >');
INSERT INTO `category` (`c_id`, `category`) VALUES ('6', 'Jungles >');

Here is my php:

<select name="category" size="6" onChange="autoSubmit();">
        <?php

        //POPULATE DROP DOWN MENU WITH CATEGORIES
        $conn = new PDO($DSN, $USERNAME, $PASSWORD);
        $sql = "SELECT * FROM category ORDER BY c_id";

        while($row = mysql_fetch_array($categories))
        {        
            echo ("<option value=\"$row[c_id]\" " . ($category == $row["c_id"] ? " selected" : "") . ">$row[category]</option>");        
        }

        ?>


    <option value="1" <?php if($category == 1) echo " selected"; ?>><? echo $category; ?></option>
    <option value="2" <?php if($category == 2) echo " selected"; ?>><? echo $category; ?></option>
    <option value="3" <?php if($category == 3) echo " selected"; ?>><? echo $category; ?></option>
    <option value="4" <?php if($category == 4) echo " selected"; ?>><? echo $category; ?></option>
    <option value="5" <?php if($category == 5) echo " selected"; ?>><? echo $category; ?></option>
    <option value="6" <?php if($category == 6) echo " selected"; ?>><? echo $category; ?></option>
    </select>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:41:43+00:00Added an answer on June 4, 2026 at 1:41 pm

    (Upgrading to an answer)

    1. The <select></select> tags should be on either side of your PHP codeblock;
    2. You should remove the WHERE clause from your SQL; and
    3. You do not need to manually include the <option>..</option> code because that is all constructed/output by the PHP.

    For example, using PDO:

    <select name="category" size="6" onChange="autoSubmit();">
    <?php
      $dbh = new PDO($DSN, $USERNAME, $PASSWORD);
      $qry = $dbh->query('SELECT * FROM category ORDER BY c_id');
    
      while ($row = $qry->fetch()) {
        echo '<option value="' . $row['c_id'] . '"';
        if ($row['c_id'] == $category) echo ' selected';
        echo '>' . htmlentities($row['category']) . '</option>'
      }
    ?>
    </select>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having difficulties to display data from mysql database to PHP generated table. Interesting
I am having difficulties sending the UDID from the iPhone to a PHP script
Im having difficulties trying to get a connection to my sqlserver database. The database
I am having difficulties when trying to insert files into a SQL Server database.
I am having difficulties getting a span tag to work properly inside a table.
I'm having difficulties getting XslCompiledTransform.Load method to take a server path. I googled around
I am having difficulties with forms, specifically ModelMultipleChoiceField. I've pieced together this code from
I'm having difficulties grasping how to do this. I am making a product options
I am having difficulties getting a map / reduce operation to work. I'm a
I am having difficulties because of the database structure on a game website similar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.