Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP: echoing variables and displaying output through selectbox
    primarykey
    data
    text
    <p>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?</p> <p>here is the info in my database: </p> <pre><code>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 &gt;'); INSERT INTO `category` (`c_id`, `category`) VALUES ('2', 'Humans &gt;'); INSERT INTO `category` (`c_id`, `category`) VALUES ('3', 'Technology &gt;'); INSERT INTO `category` (`c_id`, `category`) VALUES ('4', 'Oceans &gt;'); INSERT INTO `category` (`c_id`, `category`) VALUES ('5', 'Desert &gt;'); INSERT INTO `category` (`c_id`, `category`) VALUES ('6', 'Jungles &gt;'); </code></pre> <p>Here is my php: </p> <pre><code>&lt;select name="category" size="6" onChange="autoSubmit();"&gt; &lt;?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 ("&lt;option value=\"$row[c_id]\" " . ($category == $row["c_id"] ? " selected" : "") . "&gt;$row[category]&lt;/option&gt;"); } ?&gt; &lt;option value="1" &lt;?php if($category == 1) echo " selected"; ?&gt;&gt;&lt;? echo $category; ?&gt;&lt;/option&gt; &lt;option value="2" &lt;?php if($category == 2) echo " selected"; ?&gt;&gt;&lt;? echo $category; ?&gt;&lt;/option&gt; &lt;option value="3" &lt;?php if($category == 3) echo " selected"; ?&gt;&gt;&lt;? echo $category; ?&gt;&lt;/option&gt; &lt;option value="4" &lt;?php if($category == 4) echo " selected"; ?&gt;&gt;&lt;? echo $category; ?&gt;&lt;/option&gt; &lt;option value="5" &lt;?php if($category == 5) echo " selected"; ?&gt;&gt;&lt;? echo $category; ?&gt;&lt;/option&gt; &lt;option value="6" &lt;?php if($category == 6) echo " selected"; ?&gt;&gt;&lt;? echo $category; ?&gt;&lt;/option&gt; &lt;/select&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload