php - select * from channel_list where ch=".$ch. doesn't work

标签 php mysql select

这个问题在这里已经有了答案:





mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource

(31 个回答)


7年前关闭。




我要选择 channel 1

我的 table 看起来像这样

ch_id      ch      ch_name
1          ch1     channel 1
2          ch2     channel 2
3          ch3     channel 3
4          ch4     channel 4

我尝试使用 url 选择 channel1
exemple.com/channel.php?ch=ch1

当我使用这个
<?php
include('dbconfig.php');
$ch = $_GET['ch'];
$qry="select * from channel_list where ch=".$ch.
$row = mysql_fetch_array(mysql_query($qry));
 ?>

它告诉我这个错误

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/.../public_html/channel.php on line 5



提前致谢

最佳答案

最好使用 mysqli :) 这会起作用。 Official docs

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3";
$result = $mysqli->query($query);

/* numeric array */
$row = $result->fetch_array(MYSQLI_NUM);
printf ("%s (%s)\n", $row[0], $row[1]);

/* associative array */
$row = $result->fetch_array(MYSQLI_ASSOC);
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);

/* associative and numeric array */
$row = $result->fetch_array(MYSQLI_BOTH);
printf ("%s (%s)\n", $row[0], $row["CountryCode"]);

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

/* close connection */
$mysqli->close();
?>

关于php - select * from channel_list where ch=".$ch. doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28201025/

相关文章:

php - 分析 PHP 应用程序,50% 的时间忙于构建类——这样可以吗?

php - 意外转换为 bool 值?

Mysql count vs mysql SELECT,哪个更快?

mysql - 选择子表的子查询

php - 加载和处理 PHP 网站中的设置

php - Laravel 绑定(bind)替换变量中的问号

java - 如何授予 Java Web 应用程序对 Digital Ocean Droplet 的访问权限

mysql - 无法安排从 MYSQL 检索数据的 ssis 包

javascript - 无法过滤jcombo中的字符

mysql - 如何使mysql LIKE仅搜索单个单词