English 中文(简体)
人口名单箱,表格名称
原标题:populate listbox with table names
  • 时间:2010-11-15 13:21:39
  •  标签:
  • php
  • html

i 希望在数据库中填入一个表号的清单箱。 这部法律为之写,但似乎并未奏效。

<select id="arrays" name="arrays" style="width: 403px;" class="Fieldcell">
<?php
$dbname =  myBase ;

if (!mysql_connect( localhost ,  root ,   )) {
    echo  Could not connect to mysql ;
    exit;
}

$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);

if (!$result) {
    echo "DB Error, could not list tables
";
    echo  MySQL Error:   . mysql_error();
    exit;
}

$num_tables = mysql_num_rows($result);


for($i=0;$i<$num_tables;$i++)
{

echo "<option value="$row[i]">$row[i]</option>";

}

/*while ($row = mysql_fetch_row($result)) {

echo <option value="$row[0]">$row[0]</option>";

}*/


mysql_free_result($result);
?>
</select>
最佳回答

The commented out section is the section that will make it work. You are using a for loop where you are trying to loop through a variable $row that is never defined. You need to use mysql_fetch_row() to actually grab the data from the result set. Which it looks like you had, but then commented out. Nuke the for lop and uncomment the while loop. Although looks like you have a syntax error in your while loop (missing quotes). Here is what it should look like:

while ($row = mysql_fetch_row($result)) {
    echo "<option value= {$row[0]} >{$row[0]}</option>";
}

页: 1

你们可以保持现在的道路,但除了你需要补充的休息时间外,还要补充。

$row = mysql_fetch_all($result);
问题回答
<?php
echo  <select id="arrays" style="width: 403px;" class="Fieldcell"> ;
mysql_connect( localhost , root ,  ) or die( Could not connect to mysql );
$result = mysql_query("SHOW TABLES FROM $dbname") or die("DB error, could not list tables<br />MySQL error: ".mysql_error());
while( $r = mysql_fetch_row($result)) {
    echo  <option value=" .$r[0]. "> .$[0]. </option> ;
}
echo  </select> ;
?>

引证。

尝试:

mysql_connect( localhost , root , pass ); //dont forget the correct password
$dbname=???; // insert the database name here
$thequery=mysql_query("SELECT * FROM $dbname");
echo  <select id="arrays" name="arrays" style="width: 403px;" class="Fieldcell"> ;
while($currow=mysql_fetch_array($thequery)) {
  echo  <option value=" .$currow(COLUMN NAME HERE). "> .$currow(COLUMN NAME HERE). </option> ; //insert the column name (the one that has the values inside)
}
echo  </select> ;

一方面,它不是把数据库作为根基的良好想法。 使用根基并非安全。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签