PHP,变量范围问题 [英] PHP, Variable Scope Question

查看:48
本文介绍了PHP,变量范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我在我的通用脚本代码和我的一个函数中使用了变量 $db.它的目的是成为用于 MySQL 连接的变量.我需要在函数内部将一些数据写入数据库.在我的脚本中,我不能假设现有的 db 连接将打开,因此我打开一个新连接并在函数退出之前关闭它.自从执行此操作后,我在脚本运行后收到错误消息,说 MySQL 引用错误/不存在.

My question is I am using the variable $db in my general script code and within one of my functions. It's purpose is to be the variable that is used for MySQL connections. I have a need inside a function to write some data to the database. In my script I cannot assume that an existing db connection will be open so I open a new one and close it before the function exits. Ever since doing this I am getting an error after the script runs saying the MySQL reference is bad / doesn't exist.

我唯一可以把它固定在我的核心代码中,我使用变量 $db 作为数据库连接的变量名.我也在函数中使用了相同的变量.我没想到这会是一个问题,因为我没有在函数中的 $db 前面使用 global .这应该意味着我在函数中引用的 $db 在函数私有范围内,但它似乎正在关闭公共 $db 的连接.

The only thing I can pin it to is in my core code I use the variable $db as the variable name for database connection. I also use the same variable in the function. I did not imagine this would be a problem because I do not use global in front of $db in the function. This should mean the $db I reference in my function is in the functions private scope but it seems to be closing the public $db's connection.

有什么想法吗?

我的代码片段是:

database.php

db_connect()
{
 // open mysql db connection and return it;
}

db_close( &$db )
{
 // close the passed by reference db connection
}

api.php

api_verify( $keyid, $userid, $key )
{
  // open a new db connection
  $db = db_connect();

  // check for errors. if any errors are found note them in the db

  // close the db
  db_close($db);
}

ma​​in.php

include api.php;
include database.php;

// open a connection to the db
$db = db_connect();

// pull a list of things to process from the db and move through them one at a time
  // call api_verify() on each key before working through it's data.

db_close($db)

推荐答案

我假设您在调用 db_connect 的每个地方都使用相同的用户名/密码打开到同一个数据库的连接.这样做时,除非您的 db_connect 明确指定您正在创建一个新链接,否则它将返回一个已经打开的链接.如果该链接随后使用 db_close() 关闭,它也会关闭另一个连接,因为该链接是相同的.如果您使用 mysql_connect 连接到数据库,则需要一个名为 new link

I assume you are opening a connection to the same database with the same username/password at each of the places you call db_connect. When doing so,unless your db_connect explicitly specifies, that you are creating a new link, it will return an already opened link.If that link is then closed using db_close(), it will also close the other connection, since the link is the same. If you are using mysql_connect to connect to the database, it takes an argument called new link

new_link如果使用相同的参数对 mysql_connect() 进行第二次调用,则不会建立新链接,而是返回已打开链接的链接标识符.new_link 参数修改此行为并使 mysql_connect() 始终打开新链接,即使之前使用相同参数调用 mysql_connect() 也是如此.在 SQL 安全模式下,该参数被忽略.

new_link If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.

参考http://php.net/manual/en/function.mysql-connect.php

我不确定这是否是您面临的问题.希望有帮助.

I'm not sure if this is the issue you are facing. Hope it helps.

这篇关于PHP,变量范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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