mysql - Querying a database within a PHP function -
this question has answer here:
i'm trying load data database using php, reason doesn't work when put inside function. if try code without function, works fine:
//$dbc connection $call1 = 0; $output = ''; $query = "select * artists order lname limit $call1, 15"; $result = mysqli_query($dbc, $query); while($row = mysqli_fetch_array($result)){ $output .= "<ul>"; $output .= "<li>" . $row['name'] . "</li>"; $output .= "</ul>"; }
however, when change code inside function, don't database (or @ least won't return anything):
//$dbc connection $call1 = 0; $output = ''; function loadartists($call){ $query = "select * artists order lname limit $call, 15"; $result = mysqli_query($dbc, $query); while($row = mysqli_fetch_array($result)){ $output .= "<ul>"; $output .= "<li>" . $row['name'] . "</li>"; $output .= "</ul>"; } } loadartists($call1);
what doing wrong here?
you cannot use $dbc
in function, because global
variable.
you can use either
function loadartists($call){ global $dbc; ... }
to make $dbc
known loadartists()
or pass second parameter
function loadartists($dbc, $call){ ... }
and call
loadartists($dbc, $call1);
Comments
Post a Comment