geotri314 Posted September 9, 2010 Share Posted September 9, 2010 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 More sharing options...
rwwd Posted September 9, 2010 Share Posted September 9, 2010 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 https://forums.phpfreaks.com/topic/212971-function-wont-echo-anything/#findComment-1109227 Share on other sites More sharing options...
geotri314 Posted September 9, 2010 Author Share Posted September 9, 2010 hmmmm half way there... i dont believe i forgot a semicolon!!! :@ but it's not over. the outcome now is 1 whereas the real number should be 33 Link to comment https://forums.phpfreaks.com/topic/212971-function-wont-echo-anything/#findComment-1109229 Share on other sites More sharing options...
rwwd Posted September 9, 2010 Share Posted September 9, 2010 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 https://forums.phpfreaks.com/topic/212971-function-wont-echo-anything/#findComment-1109239 Share on other sites More sharing options...
geotri314 Posted September 9, 2010 Author Share Posted September 9, 2010 i highlt doubt this. for my sake. otherwise all i ve read from the book i learn php are to doubted... Link to comment https://forums.phpfreaks.com/topic/212971-function-wont-echo-anything/#findComment-1109245 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.