dso89762 2016-01-20 16:56
浏览 42
已采纳

函数返回一个不起作用的对象

I have some code which works:

$user = 'xxx';
$pass = 'xxx';
$db='vive';
$host ='localhost';
$name = 'chris';

function test($user, $pass, $db, $host){
$mysqli = new mysqli($host, $user, $pass, $db);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
        . $mysqli->connect_error);
}
else{
    echo 'Checkpoint 1.0 <br>';
        return $mysqli;
}
}
$mysqli2 = test($user, $pass, $db, $host);
$sql = "SELECT * FROM `vive_user` WHERE `username` LIKE"."'$name'";
$result = $mysqli2->query($sql);
$num_results = $result->num_rows;
if ($result->num_rows>0)  ...

I am able to connect to the database and pull the information that I want out of the database everytime. I wanted to clean up my code a little (all the details are not shown), so i made a new function register() out of the last part of the code. In this new function, i want to call on function test() to return me a database connection object which I can then use to perform queries:

<?php

$user = 'root';
$pass = 'root';
$db='vive';
$host ='localhost';
$name = 'chris';

function test($user, $pass, $db, $host){
$mysqli = new mysqli($host, $user, $pass, $db);
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}
    else{
        echo 'Checkpoint 1.0 <br>';
            return $mysqli;
    }
}

function register($name){
    echo'test';

    global $user;
    global $pass;
    global $db;
    global $host;

    $mysqli2 = test($user, $pass, $db, $host);
    $sql = "SELECT * FROM `vive_user` WHERE `username` LIKE"."'$name'";
    $result = $mysqli2->query($sql);
    $num_results = $result->num_rows;
    if ($result->num_rows>0)...
}

For some reason the function register() will never give me any values from the database. I am unable to get anything for $result. Any help is appreciated, I have been dancing around the problem for a few days now. Note that in my actual code I have these two functions in different php files.

  • 写回答

2条回答 默认 最新

  • dongluan6784 2016-01-20 20:32
    关注

    The LIKE statement there in the query is missing %...% wrapper.

    change the register to :

    function register($name){
    
      global $user;
     global $pass;
     global $db;
     global $host;
    
    
      $name = "%".$name."%"; // see here..
        echo'test';
        $mysqli2 = test($user, $pass, $db, $host);
        $sql = "SELECT * FROM `vive_user` WHERE `username` LIKE"."'$name'";
        $result = $mysqli2->query($sql);
        $num_results = $result->num_rows;
        if ($result->num_rows>0)...
    }
    

    and make sure the method is called

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么