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
  • 37,241
  • 25
  • 195
  • 267
Mustaque Ahmed
  • 125
  • 1
  • 3
  • 13

2 Answers2

4

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
  • 15,881
  • 6
  • 52
  • 96
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
  • 836
  • 6
  • 10