在Num_Rows中没有选择数据库 [英] No Database Selected in Num_Rows

查看:135
本文介绍了在Num_Rows中没有选择数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到一个奇怪的错误,说我在运行mysqli_num_rows查询时未连接到数据库。下面是代码:

Got a strange error saying that I'm not connected to a database while running a mysqli_num_rows query. Here is the code:

    <?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);

任何想法为什么这不工作?

Any ideas why this isn't working?

推荐答案

我已经改变了你的代码中的几个东西,请检查。

I have changed several things in your code, please review.

如果您使用 mysqli 类,那么在您的类实例化之后,任何内容应该类似:

If you are using the mysqli class then anything after your class instatiation should look something like:

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

> - > 。

using the object operator ->.

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

To get the num_rows your object operator would be after the query variable, like so:

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

我还添加了对查询中所有适用列名称的反引号:

I also added the backticks to all applicable column names in your query:

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);
}

这篇关于在Num_Rows中没有选择数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆