php - SQL 查询的语法错误

标签 php mysql

下面是代码,但我收到此错误

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 '' at line 3

任何帮助将不胜感激。

 <?php
 include("../includes/conn.php");
 $sql = "SELECT * 
 FROM  `t_maincontent`
 where uid_page =". $_GET['page'] ;
 $results = mysql_query($sql, $conn) or die(mysql_error());
 $content = mysql_fetch_assoc($results);
 //print $content['nv_content']."¬".$content['nv_title']."¬".$content['nv_meta'];
 ?>

最佳答案

请阅读有关从 PHP 进行数据库查询的基础知识。

您应该检查是否 $_GET['page']通过 isset( $_GET['page'] ); 存在仅发送查询(如果存在)。

include( '../includes/conn.php' );
if( isset( $_GET['page'] ) )
{
    /* cast to int would be enough to cancel SQL injection attacks */
    $sql = 'SELECT * FROM  t_maincontent WHERE uid_page = ' . (int) $_GET['page'] ;
    $results = mysql_query($sql, $conn) or die(mysql_error());
    $content = mysql_fetch_assoc($results);
}

关于php - SQL 查询的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16213710/

相关文章:

php - 当员工登录到特定模块时,我使用( session )变量来回显员工姓名

php - 完成 : for Z companies, 计算 A 在 X 天内的平均值。我现在怎么: return value of B for day X for company with highest average of A?

php - 将第一个单词发送到数组中的最后一个位置

PHP 警告 DOMDocument::loadHTML():第 10 行实体中的标记部分无效

php - 在 MySQL 中存储持续时间的最佳方法是什么

php - mysql_insert_id 和 mysql_pconnect

mysql - SO类消息的DB设计以及如何查询?

php - Laravel 在 Controller 中使用非 Laravel Composer 包

mysql - 有没有办法在没有子选择的情况下执行此查询?

php - Codeigniter - 检查数据库中是否存在数组中的值不起作用