0

this is my code... docrow('&ddname') is not passing.if I am using docrow('Jhon') then my code is all right.it give 3 number of row in returns.but what's wrong with docrow('&ddname')? it give me 0 row in returns.though there have 3 rows!

<?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');
?>
Cœur
  • 34,719
  • 24
  • 185
  • 251
Mustaque Ahmed
  • 125
  • 1
  • 3
  • 12

2 Answers2

5

Pass the connection as a parameter to your function, and don't use quotes around your arguments when you pass them to a function.

<?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 quotes and global variables

Community
  • 1
  • 1
Junius L.
  • 14,324
  • 4
  • 33
  • 63
2

Try it:

<?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);
?>
Subscriberius
  • 856
  • 6
  • 10