0

运行 mysqli_num_rows 查询时出现一个奇怪的错误,提示我没有连接到数据库。这是代码:

    <?php include("php/functions.php"); ?>
<?php
if(isset($_GET['verification']) && !empty($_GET['verification'])){  
    // Verify data  
    $hash = mysqli_real_escape_string($con, $_GET['verification']); // Set hash variable  

    $search_sql = "SELECT 'hash', active FROM members WHERE hash='".$verification."' AND active='0'";   
    $search_res = mysqli_query($con, $search_sql);
    $match = mysqli_num_rows($search_res);

任何想法为什么这不起作用?

4

6 回答 6

1

我在您的代码中更改了几处,请查看。

如果您正在使用mysqli该类,那么您的类 instatiation 之后的任何内容都应该类似于:

$con = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
$con->exampleClassFunction()

使用对象运算符->

要获取num_rows您的对象运算符,将在查询变量之后,如下所示:

$search_res = $con->mysqli_query($con, $search_sql);
$match = $search_res->mysqli_num_rows($search_res);

我还在您的查询中为所有适用的列名添加了反引号:

SELECT `hash`, `active` FROM members WHERE `hash`='".$verification."' AND `active`='0'

这是您的代码示例:

//include("php/functions.php");

$DB_NAME = 'DATABASE_NAME';
$DB_HOST = 'DATABASE_HOST';
$DB_USER = 'DATABASE_USER';
$DB_PASS = 'DATABASE_PASSWORD';

$con = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

// Added a connection error check before continuing
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if(isset($_GET['verification']) && !empty($_GET['verification'])){  
    $hash = $con->mysqli_real_escape_string($con, $_GET['verification']);

    // Use back ticks on query column names, 
    // use single quotes for comparative operations
    $search_sql = "SELECT `hash`, `active` FROM members WHERE `hash` = '".$verification."' AND `active` = '0'";

    $search_res = $con->mysqli_query($con, $search_sql);
    $match = $search_res->mysqli_num_rows($search_res);
}
于 2013-03-22T12:57:45.337 回答
0
<?php 
    include("functions.php"); //includes databse connection
    $search_sql = "SELECT hash, active FROM members WHERE hash='".$verification."' AND active='0'";   
    $search_res = mysqli_query($con, $search_sql);
    $match      = mysqli_num_rows($search_res);
?>

include("functions");还是include("functions.php");???你忘了.php

于 2013-03-22T12:14:55.350 回答
0

您忘记添加文件扩展名。

<?php include("functions.php"); ?>

还放`

左右hash作为其保留字 by MySQL

于 2013-03-22T12:15:46.953 回答
0

<?php include('functions.php'); ?>

另外,请确保您的结束?>标签位于正确的位置。

于 2013-03-22T12:13:12.177 回答
0

试着让它包含(“functions.php”)..我认为那是你的问题

于 2013-03-22T12:14:05.997 回答
0

请尝试以下代码:

<?php 
    include("functions.php"); //includes databse connection php file
    $search_sql = "SELECT hash, active FROM members WHERE hash='".$verification."' AND active='0'";   
    $search_res = mysqli_query($con, $search_sql);
    $match      = mysqli_num_rows($search_res);
?>

include 应该有一个 .php 文件作为参数。请检查一下。

于 2013-03-22T12:19:08.790 回答