函数,SQL连接和全局变量 [英] Functions, SQL Connects and Global Variable
问题描述
连接和关闭数据库有什么问题吗?通过调用下面的函数使用两个
<$ c $之间的mysql_query和mysql_fetch_array命令c><?php
函数dbconnect()
{
$ sql =localhost;
$ username =------;
$ password =-----;
$ connection = mysql_connect($ sql,$ username,$ password)或
die(unwable to cct);
$ databse = mysql_select_db(-------,$ connection);
全球$连接;
}
函数close()
{
global $ connection;
mysql_close($ connection);
}
dbconnect();
$ query =某些SQL语句;
$ data = mysql_query($ query,$ connection); - L1
while(mysql_fetch_assoc($ data))
{
// echo something
}
close();
?>
目前,我收到一个错误消息,说L1的$ connection需要是一个资源,但是一个BOOL。如果我在那里给出一个死亡陈述,就会触发相同的陈述。我不知道什么是错的。请发现任何可能的错误。我必须从编码中休假,并在一段时间后回来。
谢谢,关于
您必须使用全局
关键字 / em>分配 $连接
变量。否则,你在函数内声明一个本地的 $ connection
,然后调用对不存在的全局 $ connection
。在其他函数中,使用了不存在的全局变量。
函数dbconnect()
{
// Global首先要确保后面的$连接是全局的
//而不是这个函数本地的新连接
global $ connection;
$ sql =localhost;
$ username =------;
$ password =-----;
//现在这会修改全局$连接
$ connection = mysql_connect($ sql,$ username,$ password)或死(unwable to cct);
$ databse = mysql_select_db(-------,$ connection);
}
更具可读性的是使用 $ GLOBALS
array:
函数dbconnect()
{
$ sql =localhost ;
$ username =------;
$ password =-----;
//使用$ GLOBALS超全局数组
$ GLOBALS ['connection'] = mysql_connect($ sql,$ username,$ password)或死(unwable to cct);
$ databse = mysql_select_db(-------,$ GLOBALS ['connection']);
}
最棒的是返回 $ connection
from dbconnect()
并在其他函数中使用该值:
函数dbconnect()
{
$ sql =localhost;
$ username =------;
$ password =-----;
$ connection = mysql_connect($ sql,$ username,$ password)或
die(unwable to cct);
$ databse = mysql_select_db(-------,$ connection);
//从函数
返回$ connection;
}
//调用为
$ connection = dbconnect();
//并定义您的其他函数以接受$ connection作为参数
Is there anything wrong with connecting and closing to a database by calling the function below with the mysql_query and mysql_fetch_array commands between the two
<?php
function dbconnect()
{
$sql = "localhost";
$username = "------";
$password = "-----";
$connection = mysql_connect($sql, $username, $password) or
die("unwable to cct");
$databse = mysql_select_db("-------", $connection);
global $connection;
}
function close()
{
global $connection;
mysql_close($connection);
}
dbconnect();
$query = "Some SQL Statement";
$data = mysql_query($query, $connection); - L1
while (mysql_fetch_assoc($data))
{
//echo something
}
close();
?>
At present, I am getting an error saying that $connection at L1 needs to be a resource but is a BOOL. If I give a die statement there, the same is triggered. I have no idea what is wrong. Please spot any errors you can. I have to take a sabbatical from coding and I am back after a while.
Thanks & regards
You must use the global
keyword before assigning the $connection
variable. Otherwise, you declare a local $connection
inside the function and then call a reference to the yet non-existent global $connection
. In the other functions, that non-existent global is used.
function dbconnect()
{
// Global first to be sure the subsequent $connection is the global
// rather than a new one local to this function
global $connection;
$sql = "localhost";
$username = "------";
$password = "-----";
// Now this modifies the global $connection
$connection = mysql_connect($sql, $username, $password) or die("unwable to cct");
$databse = mysql_select_db("-------", $connection);
}
More readable would be to use the $GLOBALS
array:
function dbconnect()
{
$sql = "localhost";
$username = "------";
$password = "-----";
// Using the $GLOBALS superglobal array
$GLOBALS['connection'] = mysql_connect($sql, $username, $password) or die("unwable to cct");
$databse = mysql_select_db("-------", $GLOBALS['connection']);
}
Best of all would be to return $connection
from dbconnect()
and use that value in other functions:
function dbconnect()
{
$sql = "localhost";
$username = "------";
$password = "-----";
$connection = mysql_connect($sql, $username, $password) or
die("unwable to cct");
$databse = mysql_select_db("-------", $connection);
// Return from the function
return $connection;
}
// call as
$connection = dbconnect();
// and define your other functions to accept $connection as a parameter
这篇关于函数,SQL连接和全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!