如何将mysql放在php函数中? [英] How to put mysql inside a php function?
本文介绍了如何将mysql放在php函数中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对将mysql放入函数showMsg()有问题.如果mysql没有被showMsg()函数包装,则mysql工作正常,但是当我用showMsg()函数包装时,它会给出错误消息警告:mysql_query():提供的参数无效".如何将mysql放在php函数中?以下是我的代码:
I have problem about putting mysql into a function showMsg(). The mysql is working fine if it is not wrapped by function showMsg(), but when I wrap it with function showMsg(), it gives error message "Warning: mysql_query(): supplied argument is not a valid". How to put mysql inside a php function? Below is my codes :
<?php
function showMsg(){
$query2 = "SELECT id, message, username, datetime FROM messageslive ORDER BY id DESC LIMIT 20";
$result2 = mysql_query($query2,$connection) or die (mysql_error());
confirm_query($result2);
$num = mysql_num_rows($result2);
while($msginfo = mysql_fetch_array($result2)){
echo $msginfo['message'];
echo $msginfo['username'];
}
}
<div>
<?php showMsg(); ?>
</div>
?>
推荐答案
您可能需要:
global $connection;
(即在函数内部)
请参见 Variable Scope
这篇关于如何将mysql放在php函数中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文