Mysql查询的Php自定义函数不起作用
我创建了两个函数,一个用于连接MySQL数据库,另一个用于运行特定查询。 我输入数据库名称作为第一个函数连接到数据库的参数,这很好,但我的问题是第二个函数。 第二个函数通过运行查询返回Mysql查询的Php自定义函数不起作用,php,function,Php,Function,我创建了两个函数,一个用于连接MySQL数据库,另一个用于运行特定查询。 我输入数据库名称作为第一个函数连接到数据库的参数,这很好,但我的问题是第二个函数。 第二个函数通过运行查询返回$result,但是当我使用mysql\u fetch\u array和$result时,即使它应该给出多个输出,它也会给出一个输出。 因为我不是php专家,所以我找不到解决方案。请帮帮我 代码如下: File Function.php <?php function myconnect($data) {
$result
,但是当我使用mysql\u fetch\u array
和$result
时,即使它应该给出多个输出,它也会给出一个输出。
因为我不是php专家,所以我找不到解决方案。请帮帮我
代码如下:
File Function.php
<?php
function myconnect($data)
{
$db_host='localhost';
$db_user='root';
$db_pwd='';
$data=$data;
$dbc = mysqli_connect($db_host, $db_user,$db_pwd,$data) or die (mysql_error());
return $dbc;
}
function runquery($db,$table,$tcol,$tid)//(databse,table,column_name,identifier)
{
$dbc=myconnect($db);
$query="SELECT *FROM ".$table." WHERE ".$tcol."=".$tid." ORDER BY first_name ASC";
$result = mysqli_query($dbc, $query);
return $result;
}
?>
<?php
require_once('testfunc.php');
$result= runquery('user','user_basic','user_type','1');
//runquery('database','table','col','id')/
while($row=mysqli_fetch_array($result))
{
echo '<strong>First Name:</strong>' . $row['first_name'] . '<br/>';
}
?>
文件test.php
<?php
function myconnect($data)
{
$db_host='localhost';
$db_user='root';
$db_pwd='';
$data=$data;
$dbc = mysqli_connect($db_host, $db_user,$db_pwd,$data) or die (mysql_error());
return $dbc;
}
function runquery($db,$table,$tcol,$tid)//(databse,table,column_name,identifier)
{
$dbc=myconnect($db);
$query="SELECT *FROM ".$table." WHERE ".$tcol."=".$tid." ORDER BY first_name ASC";
$result = mysqli_query($dbc, $query);
return $result;
}
?>
<?php
require_once('testfunc.php');
$result= runquery('user','user_basic','user_type','1');
//runquery('database','table','col','id')/
while($row=mysqli_fetch_array($result))
{
echo '<strong>First Name:</strong>' . $row['first_name'] . '<br/>';
}
?>
如果我做错了,那么建议我一个更好的方法:-)快速浏览一下函数runquery 应该是
SELECT * FROM
注意后面的空格*
编辑:
我还注意到您使用的是*mysqli\u fetch\u array*,这不是有效的mysqli方法。您在mysql上使用mysqli扩展是正确的,但是您应该进一步研究以解决这个问题。我提供的链接提供了一个程序性示例,应该可以满足您的需要
function myconnect($db)
{
/*Removed redundant - single use variables*/
/*DB name was passed to the client_flags parameter of mysql_connect instead of mysql_select_db*/
$dbc = mysql_connect("localhost", "root","") or die (mysql_error());
/*Inserted Line*/
mysql_select_db($data);
return $dbc;
}
当前,您没有选择与使用数据库db_名称等效的数据库
两个语法更改和函数定义
function runquery($db,$table,$tcol,$tid)//(databse,table,column_name,identifier)
{
$dbc=myconnect($db);
/*Query and link identifier were in the wrong order*/
return mysql_query("SELECT * FROM ".$table." WHERE ".$tcol."=".$tid." ORDER BY first_name ASC", $doc);
}
最后是一些语法更改,函数调用
require_once('testfunc.php');
$result= runquery('user','user_basic','user_type','1');
/*fetch associateive array of result during iteration*/
while($row=mysql_fetch_assoc($result))
{
echo '<strong>First Name:</strong>' . $row['first_name'] . '<br/>';
}
require_once('testfunc.php');
$result=runquery('user','user_basic','user_type','1');
/*在迭代期间获取结果的关联数组*/
while($row=mysql\u fetch\u assoc($result))
{
回显“名字:”。$row['First_Name']。
;
}
您的问题不够清楚。你能更详细地描述一下你想要实现的目标吗?*从应该是*从。。。如果$table没有名为first\u name的列,则会出现问题。。。希望使用mysqli而不是mysql;或者(甚至更好)这段代码可以使用。您不应该使用mysql扩展,而应该使用mysqli扩展,因为它是改进版。我还没有读过它。一旦我有机会看到问题的相关性,我将编辑我的答案。谢谢你的提醒。