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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效