如何正确mysql查询这个

时间:2014-09-09 13:52:15

标签: php mysql mysqli

由于一些奇怪的原因,这是行不通的。

<?php 
include 'core/init.php';
include 'core/classes/functions.php';
$rank = htmlentities($user['rank']);
if(isset($_GET['snip']) && empty($_GET['snip']) === false) { // Putting everything in this if block.

    $snip   = htmlentities($_GET['snip']); // sanitizing the snip inputed data (in the Url)
    $snipid  = htmlentities($_GET['snip']); // sanitizing the snip inputed data (in the Url)
    if ($snip === '') { // If the snip doesn't exist
        //nothing
    }else{
$con=mysqli_connect("localhost","root","","ezsnip");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT ID, Snip, Language, UniqueID, IP, Title, UserID, Added, Views FROM snips WHERE ID=$snip");

if (!$result) {
    die(mysqli_error($result));
}

$row = mysqli_fetch_array($result);

$datetime = $row['Added'];
$views = $row['Views'];
$userid = $row['UserID'];
$ID = $row['ID'];
$snip = $row['Snip'];
$language = $row['Language'];
$ip = $row['IP'];
$UniqueID = $row['UniqueID'];
$title = $row['Title'];
$snip = str_replace('<?php', '&lt;?php', $snip);
$snip = str_replace('?>', '?&gt;', $snip);
$time = strtotime($datetime);
$time = date("m/d/y g:i A", $time);
    }
}

?>

当我将view.php?snip = 1或2或任何数字放入时,它可以正常工作,但不是在我这样做时; view.php?snip = dGUE或其他东西,即使它存在于数据库中。它说:警告:mysqli_error()期望参数1为mysqli,第21行的C:\ xampp \ htdocs \ view.php中给出布尔值

第21行:

我不确定如何从数据库查询中提取诸如dGUR或dGUE之类的Id。

2 个答案:

答案 0 :(得分:1)

我认为它与SQL的语法有关。试试这个

$result = mysqli_query($con,"SELECT ID, Snip, Language, UniqueID, IP, Title, UserID, Added, Views FROM snips WHERE ID='$snip'");

请注意$ snip

周围的单引号

答案 1 :(得分:0)

您的查询应如下所示:

$result = mysqli_query($con,"SELECT ID, Snip, Language, UniqueID, IP, Title, UserID, Added, Views FROM snips WHERE ID='{$snip}'");

注意WHERE子句(WHERE ID='{$snip}')中的引号。

希望这有帮助!