1. <legend id='UGQZd'><style id='UGQZd'><dir id='UGQZd'><q id='UGQZd'></q></dir></style></legend>

  2. <i id='UGQZd'><tr id='UGQZd'><dt id='UGQZd'><q id='UGQZd'><span id='UGQZd'><b id='UGQZd'><form id='UGQZd'><ins id='UGQZd'></ins><ul id='UGQZd'></ul><sub id='UGQZd'></sub></form><legend id='UGQZd'></legend><bdo id='UGQZd'><pre id='UGQZd'><center id='UGQZd'></center></pre></bdo></b><th id='UGQZd'></th></span></q></dt></tr></i><div id='UGQZd'><tfoot id='UGQZd'></tfoot><dl id='UGQZd'><fieldset id='UGQZd'></fieldset></dl></div>

      <small id='UGQZd'></small><noframes id='UGQZd'>

        <bdo id='UGQZd'></bdo><ul id='UGQZd'></ul>
      <tfoot id='UGQZd'></tfoot>

      函数内部的 mysqli/mysql 查询不起作用

      时间:2023-07-29
      <tfoot id='A2x0K'></tfoot>
    1. <i id='A2x0K'><tr id='A2x0K'><dt id='A2x0K'><q id='A2x0K'><span id='A2x0K'><b id='A2x0K'><form id='A2x0K'><ins id='A2x0K'></ins><ul id='A2x0K'></ul><sub id='A2x0K'></sub></form><legend id='A2x0K'></legend><bdo id='A2x0K'><pre id='A2x0K'><center id='A2x0K'></center></pre></bdo></b><th id='A2x0K'></th></span></q></dt></tr></i><div id='A2x0K'><tfoot id='A2x0K'></tfoot><dl id='A2x0K'><fieldset id='A2x0K'></fieldset></dl></div>

      • <bdo id='A2x0K'></bdo><ul id='A2x0K'></ul>

        <small id='A2x0K'></small><noframes id='A2x0K'>

          <tbody id='A2x0K'></tbody>

            <legend id='A2x0K'><style id='A2x0K'><dir id='A2x0K'><q id='A2x0K'></q></dir></style></legend>

              1. 本文介绍了函数内部的 mysqli/mysql 查询不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试为我的网站构建一些功能,其中一些包括从mysql数据库中获取数据.当我在函数之外测试代码时它似乎工作正常.所以在这里,第一页:

                I'm tryin to build some functions for a website of mine and some of them consist in fetching data from the mysql database. When I test the code outside of the function it seems to work properly. So here it is, The first page:

                require('db.php');
                require('functions.php');
                
                $email = 'sample@gmail.com';
                
                if (user_exists($email) == true){
                 echo "Good news, this exists";
                }
                

                现在 db.php :

                Now db.php :

                $db = new MySQLi("localhost","test","test","test");
                if ($db->connect_errno){
                    echo "$db->connect_errno";
                }
                

                还有functions.php文件:

                And the functions.php file:

                function sanitize ($data){
                    $db->mysqli_real_escape_string($data);
                }
                function user_exists($usermail){
                    $usermail = sanitize($usermail);
                    $query = $db->query("SELECT COUNT(userId) FROM users WHERE userEmail= '$usermail' ");
                    $check = $query->num_rows;
                    return ($check == 1) ? true : false;
                }
                

                访问第一个文件时遇到的错误是:

                And the error I'm getting when accessing the first file is:

                Notice: Undefined variable: db in C:xampphtdocsauctiorincfunctions.php on line 6
                
                Fatal error: Call to a member function query() on a non-object in C:xampphtdocsauctiorincfunctions.php on line 6
                

                所以我需要/包含了 db.php,其中 $db 是 mysqli 连接.在同一个文件(第一个文件)中,我调用位于 functions.php 的函数

                SO I've required/included the db.php where $db is the mysqli connect. And within the same file(first file) I call the functions located at functions.php

                先谢谢你,我很感激你的帮助,因为这让我很生气......

                Thank you in advance, I'd appreciate your help as this is pissing me off......

                推荐答案

                你可能需要使用 global 关键字,否则 $db 被认为是局部范围内的 var.

                You probably need to use the global keyword, otherwise $db is considered a var in local scope.

                function sanitize ($data){
                    global $db;
                    $db->mysqli_real_escape_string($data);
                }
                
                function user_exists($usermail){
                    global $db;
                    $usermail = sanitize($usermail);
                    $query = $db->query("SELECT COUNT(userId) FROM users WHERE userEmail= '$usermail' ");
                    $check = $query->num_rows;
                    return ($check == 1) ? true : false;
                }
                

                这篇关于函数内部的 mysqli/mysql 查询不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在命名空间中使用 MySQLi 下一篇:mysqli::multi_query 是否比多个单个查询更有效?

                相关文章

                最新文章

                1. <tfoot id='7gPTD'></tfoot>

                    <legend id='7gPTD'><style id='7gPTD'><dir id='7gPTD'><q id='7gPTD'></q></dir></style></legend>

                  1. <i id='7gPTD'><tr id='7gPTD'><dt id='7gPTD'><q id='7gPTD'><span id='7gPTD'><b id='7gPTD'><form id='7gPTD'><ins id='7gPTD'></ins><ul id='7gPTD'></ul><sub id='7gPTD'></sub></form><legend id='7gPTD'></legend><bdo id='7gPTD'><pre id='7gPTD'><center id='7gPTD'></center></pre></bdo></b><th id='7gPTD'></th></span></q></dt></tr></i><div id='7gPTD'><tfoot id='7gPTD'></tfoot><dl id='7gPTD'><fieldset id='7gPTD'></fieldset></dl></div>

                    <small id='7gPTD'></small><noframes id='7gPTD'>

                    • <bdo id='7gPTD'></bdo><ul id='7gPTD'></ul>