Home > Backend Development > PHP Tutorial > Function问题

Function问题

WBOY
Release: 2016-06-23 14:20:09
Original
940 people have browsed it

function PHP自定义函数 PHP

<?phpfunction test($sql){	require_once("../../config.php");	$query = sqlsrv_query($conn,$sql);	if( $query === false) {		die( print_r( sqlsrv_errors(), true) );	}	$row = sqlsrv_fetch_array($query);	echo $row[0];}test("SELECT COUNT(name) FROM tb1 GROUP BY name");test("SELECT COUNT(name) FROM tb2 GROUP BY name");?>
Copy after login

test("SELECT COUNT(name) FROM tb1 GROUP BY name");
test("SELECT COUNT(name) FROM tb2 GROUP BY name");
调用一个是没问题的,但两个以上就报错了,是为什么呢?


回复讨论(解决方案)

结束一次查询后,第二次要清理前面请求的结果

结束一次查询后,第二次要清理前面请求的结果 怎么清理前面请求的结果?

function test($sql){
require_once("../../config.php"); //已加载就不加载
$query = sqlsrv_query( $conn,$sql); //如果不加载(第二次执行时)$conn 不就不存在了吗?
....
}

老徐说的对,你的代码$conn要作为全局变量才行,不然二次调用$conn就不存在了
看看出错信息是否如此

另外,清理结果也是必需的,以前我就试过不清理导致两次结果交错混淆了
mysql是mysql_free_result(),你的代码看上去是sqlsever,不太清楚,估计是sqlsrv_free_stmt()
其他数据库一般都有类似函数

require_once("../../config.php"); 放在函数外面加载不行么

尽量不要在函数里引入文件,在调用函数的文件中引入文件

function test($sql){
require_once("../../config.php"); //已加载就不加载
$query = sqlsrv_query( $conn,$sql); //如果不加载(第二次执行时)$conn 不就不存在了吗?
....
}

老徐说的对,你的代码$conn要作为全局变量才行,不然二次调用$conn就不存在了
看看出错信息是否如此

另外,清理结果也是必需的,以前我就试过不清理导致两次结果交错混淆了
mysql是mysql_free_result(),你的代码看上去是sqlsever,不太清楚,估计是sqlsrv_free_stmt()
其他数据库一般都有类似函数 谢谢你们!!!在这里还想问个题外题,就是mssql_init这个函数,我把它换成 sqlsrv_init 就不行,是不是没有sqlsrv_init这个函数啊?那应该用哪个函数代替呢?

看手册,各家接口不同,不是说把函数名前缀换了就行的

看手册 http://php.chinaunix.net/manual/zh/ref.sqlsrv.php

<?phprequire_once("../../config.php");global $conn;$conn=$conn;function test($sql){    global $conn;    $query = sqlsrv_query($conn,$sql);    if( $query === false) {        die( print_r( sqlsrv_errors(), true) );    }    $row = sqlsrv_fetch_array($query);    echo $row[0];}test("SELECT COUNT(name) FROM tb1 GROUP BY name");test("SELECT COUNT(name) FROM tb2 GROUP BY name");?>
Copy after login
Copy after login
Copy after login

<?phprequire_once("../../config.php");global $conn;$conn=$conn;function test($sql){    global $conn;    $query = sqlsrv_query($conn,$sql);    if( $query === false) {        die( print_r( sqlsrv_errors(), true) );    }    $row = sqlsrv_fetch_array($query);    echo $row[0];}test("SELECT COUNT(name) FROM tb1 GROUP BY name");test("SELECT COUNT(name) FROM tb2 GROUP BY name");?>
Copy after login
Copy after login
Copy after login

你这个不行哦!!!

function test($sql){
require_once("../../config.php"); //已加载就不加载
$query = sqlsrv_query( $conn,$sql); //如果不加载(第二次执行时)$conn 不就不存在了吗?
....
}

<?php $a = test("SELECT COUNT(name) FROM tb1 GROUP BY name");$b = test("SELECT COUNT(name) FROM tb2 GROUP BY name"); //为什么我这些写它就直接输出了呢?我想把值存在变量里,再用 echo 来输出。?>
Copy after login

把 echo $row[0];
改为 return $row[0];


<?phprequire_once("../../config.php");global $conn;$conn=$conn;function test($sql){    global $conn;    $query = sqlsrv_query($conn,$sql);    if( $query === false) {        die( print_r( sqlsrv_errors(), true) );    }    $row = sqlsrv_fetch_array($query);    echo $row[0];}test("SELECT COUNT(name) FROM tb1 GROUP BY name");test("SELECT COUNT(name) FROM tb2 GROUP BY name");?>
Copy after login
Copy after login
Copy after login

你这个不行哦!!!

<?phpglobal $conn;require_once("../../config.php");function test($sql){    global $conn;    $query = sqlsrv_query($conn,$sql);    if( $query === false) {        die( print_r( sqlsrv_errors(), true) );    }    $row = sqlsrv_fetch_array($query);    echo $row[0];}test("SELECT COUNT(name) FROM tb1 GROUP BY name");test("SELECT COUNT(name) FROM tb2 GROUP BY name");?>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template