通过变量传递参数

时间:2017-05-13 11:02:14

标签: php mysqli

这是我的代码...... docrow('& ddname')没有传递。如果我使用docrow(' Jhon')那么我的代码就可以了。在返回时给出3行数。但是& #39; docrow错误('& ddname')?它给了我0行的回报。虽然有3行!

<?php 
$ddname="Jhon"; 
include 'config.php';
//my database connection
function docrow($name){
    global $conn;
    $sqlquery=mysqli_query($conn,"SELECT id,name,docname,discount,docget from income where docname='$name'");
    $countrow=mysqli_num_rows($sqlquery);
    return $countrow;
}
echo docrow('$ddname');
?>

2 个答案:

答案 0 :(得分:5)

将连接作为参数传递给函数,并且在将参数传递给函数时不要在参数周围使用引号。

<?php
$ddname = "Jhon";
include 'config.php';
//my database connection
function docrow($name, $conn)
{
    $sqlquery = mysqli_query($conn, "SELECT id,name,docname,discount,docget from income where docname='$name'");
    return mysqli_num_rows($sqlquery);
}

echo docrow($ddname, $conn);
?>

Please read more about the difference between single and double quotesglobal variables

答案 1 :(得分:2)

试一试:

<?php
$ddname="Jhon"; 
include 'config.php';
//my database connection
function docrow($name){
    global $conn;
    $sqlquery=mysqli_query($conn,"SELECT id,name,docname,discount,docget from income where docname='$name'");
    $countrow=mysqli_num_rows($sqlquery);
    return $countrow;
}
echo docrow($ddname);
?>