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
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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.