开发者

Output MySQL Query Results to Combo Box

I have the following snippet wherein I'm wanting to grab all entries from SMSGateway and place them in a combobox in my page:

<?php 
$querycarrier="SELECT * from 'SMSGateway'";
$resultcarrier=mysql_query($querycarrier);
if (!$resultcarrier) {
die('Invalid query: ' . mysql_error());
}
while($rowcarrier = mysql_fetch_array($resultcarrier)){
    echo "<option value=$rowcarrier[GatewayName]</option>";
}
开发者_如何学Go?>

Currently, the SMSGateway table is empty. So, when I view the page in question, I receive the following error:

Invalid query: You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near ''SMSGateway'' at line 1

How can I be sure that the error is being thrown because the table is empty?

Thanks much for yuour help and guidance.


$querycarrier="SELECT * from SMSGateway";

remove single quotes - if you need to something use ` char left from 1 on keyboard


Remove single quotes around table name.

edit. Beware when you don't use quotes around option value and options contain spaces. Text would be truncated to the first word.

This is safer and a more correct syntax.

echo "<option value=\"$rowcarrier[GatewayName]\"</option>";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜