0

Hopefully this question doesn't drive anyone to drink. I've been at this for about 9 hours now and cannot get a drop down to populate. I know I'm missing something very simple and I'm reaching out to see if anyone can give me some insight. The table I'm calling from has 1 column and just has names (which are unique)

I have a db class being called in the beginning of all of my pages, in all but 2 cases , its working fine, in the 2 exceptions, it's the pages that require the dropdown. I've deleted and recreated both pages with no change.

Both Pages are named select.php and select_p.php. The initial call is as follows and breaks at the first "->" so begins printing everything after it and up until the "?>"

include("database.class.php");
$database = new database;
$sql_page  = $database->mysqlQuery("SELECT * FROM spec_tables");
$edata_page = $database->mysqlFetchArray($sql_page);
return $edata_page;
?>

Heres the fucntion in the class file that works on 2 other sites and other pages in this same site

function mysqlQuery($qry)

{   

    $rs     =   mysql_query($qry, $this->DatabaseLink);

    return $rs;



    echo mysql_error();

}

Now if I use the code in the page, it doesnt print but the dropdown is blank

<select name='list' value=''><option>Select List</option>
<?
$sql_page  = $database->mysqlQuery("SELECT * FROM spec_tables");
$edata_page = $database->mysqlFetchArray($sql_page);
return $edata_page;
foreach($edata_page as $row){
?>
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>
<? }  ?>
</select>

I've looked at the following (plus about 20 pages that are not this close)

PHP- Fetch from database and store in drop down menu html

How to put table value in a dropdown menu with MYSQL and PHP

http://www.plus2net.com/php_tutorial/list-table.php

http://www.tutorialrepublic.com/faq/how-to-populate-dropdown-list-with-array-values-in-php.php

Here's all the snippets of code I've tried to no avail, some of them actually print the code in the html form, any constructive help would be very much appreciated.

*********************************************************
<select name='list' value=''><option>Select List</option>
<?
$sql_page  = $database->mysqlQuery("SELECT * FROM spec_tables");
$edata_page = $database->mysqlFetchArray($sql_page);
return $edata_page;
foreach($edata_page as $row){
?>
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>
<? }  ?>
</select>
*****************************************************************
<select name='list' value=''><option>Select List</option>
<?
$result = $database->mysqlQuery("select * from spec_tables");
if (!$result) die('Couldn\'t fetch records'); 
$num_fields = mysql_num_fields($result); 
$row = array(); 
for ($i = 0; $i < $num_fields; $i++) 
while ($row = mysql_fetch_row($result)) 
    {
?>
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>
<? }  ?>
</select>
********************************************************************
<select name='list' value=''><option>Select List</option>
<?
$result  = $database->mysqlQuery("select * from spec_tables");
if(mysql_num_rows($result) > 0){
while($row = mysql_fetch_array($result)) {
   echo '<option value=". $row['name'] .">' . $row['name'] . '</option>';
}
}
?>
</select>
****************************************************************************
Placed in top of file
$servername = "localhost";
$username = "uname";
$password = "pword";
$dbname = "db_name";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if ($result = mysqli->query("SELECT * FROM 'spec_tables'")) {
printf("Select returned %d rows.\n", $result->num_rows);

/* free result set */
$result->close();
}


**********************************************************************
 <?
$sql = "select * from spec_tables";
$result = mysql_query($sql);

echo "<select name='list' value=''><option>Select List</option>";

while ($row = mysql_fetch_array($result)) {

echo "<option value='" . $row['name'] ."'>" . $row['name'] ."</option>";
}
echo "</select>";
?>
**************************************************
Community
  • 1
  • 1
Semon
  • 33
  • 1
  • 6
  • 1
    you sure short tags are enabled? sounds like they're not. either enable them or change all `` to ` – Funk Forty Niner Feb 23 '17 at 03:33
  • get rid of mysql_ and use something current/modern/good. – mickmackusa Feb 23 '17 at 03:35
  • why not put some breakpoints in your code, find out if the query is actually returning a resultset or an error. Did you run you query in the database to prove that it works? Doing small, simple checks ends up saving you 9 hours or more in some cases. – mickmackusa Feb 23 '17 at 03:37
  • 1. short tags work in all other php pages.... about 15 of them .... 2. thanks , I'm trying to learn newer programming, but its a process and I have projects Im working on. I have done small simple checks, the code is actually less than 10 lines, all of the above were different attempts to get the dropdown populated 3. Those were examples of different things I tried, thanks but I know it wouldn't run as I tried it and it didnt run, I simply posted it to show the different attempts to get it to work – Semon Feb 23 '17 at 03:48
  • 4. why was it tagged as mysqli? because a couple attempts were trying to use mysqli...refer to number 2 on my list. I can't understand how the question couldn't be clearer – Semon Feb 23 '17 at 03:48
  • Fred, please stop downvoting my questions, I appreciate you don't like them, but I'm trying.I need insight not downvotes – Semon Feb 23 '17 at 03:49
  • Is $row an object? if so, you may need to specify the field.. something like $row->field. or $row['field']... – R. Smith Feb 23 '17 at 03:54
  • Yes R Smith, I agree, however theres a gremlin somewhere and I can't seem to find it . I dont think the sql statement is pulling anything although the data is there and the SELECT statement is a simple one. – Semon Feb 23 '17 at 03:57
  • you sure short tags are enabled? sounds like they're not. either enable them or change all to – Semon Feb 23 '17 at 15:37

1 Answers1

0

Heres the code thats working

//placed in beginning of code
<?php
include("database.class.php");
$database = new Database;

$result  = $database->mysqlQuery("SELECT * FROM spec_tables");

?>
//Placed inside html form
 <?php
echo '<select name="list">';
echo '<option>Select List</option>';
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['name'] .'">' . $row['name'] .'</option>';
}
echo '</select>';

?>
Semon
  • 33
  • 1
  • 6