关于return的返回的问题

peixin229 2009-08-31 06:14:05
function db_connect() {
$result = new mysqli('localhost', 'bm_user', 'password', 'bookmarks');
if (!$result) {
throw new Exception('Could not connect to database server');
} else {
return $result;
}
}


function register($username, $email, $password) {
// register new person with db
// return true or error message

// connect to db
$conn = db_connect();

// check if username is unique
$result = $conn->query("select * from user where username='".$username."'");
if (!$result) {
throw new Exception('Could not execute query');
}

if ($result->num_rows>0) {
throw new Exception('That username is taken - go back and choose another one.');
}

// if ok, put in db
$result = $conn->query("insert into user values
('".$username."', sha1('".$password."'), '".$email."')");
if (!$result) {
throw new Exception('Could not register you in database - please try again later.');
}

return true;
}

请问各位高手,第二个函数register($username, $email, $password)中的变量$result和第一个函数中return的$result是不是同一个。如果不是,那第一个函数return是不是仅仅返回$result的值,而不是变量return?
...全文
80 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ms_X0828 2009-09-01
  • 打赏
  • 举报
回复
第一个返回的是数据库操作
第二个返回的为Boolean值
tony-杨 2009-08-31
  • 打赏
  • 举报
回复
第一个返回的是资源句柄
第二个返回的是Boolean值True
lypfriends 2009-08-31
  • 打赏
  • 举报
回复
ding
fenyao 2009-08-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tfxg 的回复:]
db_connect() 這個函數返回的是一個類似的#12這樣的值
register 這個返回是true
[/Quote]
peixin229 2009-08-31
  • 打赏
  • 举报
回复
哦,不好意思,其实这个问题应该是变量作用域的问题,我自己刚刚写了代码测试。第二个函数里面的$result跟第一个函数里面的$result不是同一个,虽然以上两位没有回到我想要的,但是还是感谢!
总哈哈 2009-08-31
  • 打赏
  • 举报
回复
第一个仅仅返回 new mysqli('localhost', 'bm_user', 'password', 'bookmarks'); 这个东西
tfxg 2009-08-31
  • 打赏
  • 举报
回复
db_connect() 這個函數返回的是一個類似的#12這樣的值
register 這個返回是true

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧