Jump to content

function wont echo anything


geotri314

Recommended Posts

hi guys. i am trying this code:

 

<?php count('Comedy'); ?>

 

which is supposed to call this function:

 

function count($cat){
$dbc = mysqli_connect('localhost', '60517', '59725972', '60517') or die ('Could connect to the sql db');
if(isset($cat)) { $query = "SELECT * FROM `movies` WHERE genre='$cat'"; }
if(!isset($cat)) { $query = "SELECT * FROM movies"; }
$result = mysqli_query($dbc, $query) or die('something went wrong with the query');
$movies = mysqli_num_rows($result);
mysqli_close($dbc) ;
echo $movies

 

however nothing would be echoed. what am i doing wrong? i literally cant figure out whats going wrong in 6 lines of code. i am new to php so any help would be appreciated. sorry for bothering you.

Link to comment
https://forums.phpfreaks.com/topic/212971-function-wont-echo-anything/
Share on other sites

Hi there,

 

If you had error_reporting(E_ALL); was on it would have flagged up the error..

function count($cat){
$dbc = mysqli_connect('localhost', '60517', '59725972', '60517') or die ('Could connect to the sql db');
if(isset($cat)) { $query = "SELECT * FROM `movies` WHERE genre='$cat'"; }
if(!isset($cat)) { $query = "SELECT * FROM movies"; }
$result = mysqli_query($dbc, $query) or die('something went wrong with the query');
$movies = mysqli_num_rows($result);
mysqli_close($dbc) ;
return $movies;// I have altered this so that it returns from the function
}

 

then:-

 

<?php echo count('Comedy'); ?>//then echo it here, much better!

 

But the main reason was you were missing the semi colon from the 'echo $movies' part of the function.

 

Cheers,

Rw

Hi there,

 

I personally don't use mysqli, and I just looked for mysqli_query(), but I can't find a reference to it as a function, but to me it looks as if you have your data base connection handle & your sql query var the wrong way around, though I could well be wrong about that:-

$result = mysqli_query([b]$query, $dbc[/b]) or die('something went wrong with the query');

 

That's the only thing that looks a bit squiffy to me.

 

Cheers,

Rw

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.