PHP and MySql hitcounter problem? -


OK I am new to php and mysql, I tried to make a web page hit counter but update my page counter Did not happen correctly and the following warnings are listed below.

  WARNING: mysqli_query () WARNING: At least 2 parameters given in mysqli_num_rows (), 1 is expected to be 1 mysqli_result, boolean  

Here is the php code.

  & lt ;? Php $ page = $ _SERVER ['SCRIPT_FILENAME']; // Query member data from database and ready to display $ mysqli = new mysqli ("localhost", "root", "", "sitename"); $ Dbc = mysqli_query ($ mysqli, "mysql_counter_logs SELECT id page where page = '$ page'"); If (mysqli_num_rows ($ dbc) == 0) {$ mysqli = new mysqli ("localhost", "root", "", "sitename"); Include $ Dbc = mysqli_query ($ mysqli, "mysql_counter_logs (page) values ​​('$ page')"); Mysqli_query ($ DBC); } Otherwise (mysqli_num_rows ($ dbc) == 1) {$ mysqli = new mysqli ("localhost", "root", "", "sitename"); $ Dbc = mysqli_query ($ mysqli, "UPDATE mysql_counter_logs SET hits = hits + 1 ou page = '$ page'"); Mysqli_query ($ DBC); } // current calculation $ count = mysqli_fetch_row (mysqli_query ($ mysqli, "select impressions from mysql_counter_logs")) retreives; If (! $ Dbc) {// there was an error ... do something about it ... print mysqli_error (); } // prints the print on your site "$ count [0]" prints; ? & Gt;  

An extract of your code:

  $ Mysqli = new mysqli ("localhost", "root", "", "sitename"); Include $ Dbc = mysqli_query ($ mysqli, "mysql_counter_logs (page) values ​​('$ page')"); Mysqli_query ($ DBC); According to the manual first, when used as a function:  
  mixed mysqli_query (mysqli $ link, string $ query [, int $ Resultmode])  

This means that you will have to give your $ mysqli variable as the first parameter, and your query should be given as the second parameter .

I do not find that you are using mysqli_query on a duplicate third line like this when using it correctly in other parts of your code?

Are you sure you want to use that function here?


Also, to know how you are getting those error messages ;-) Check that you do not mysqli_query () expect at least 2 parameters Think about it: bullion will return if there is an error.

In that situation, you can Do not call mysqli_num_rows on the return value of mysqli_query , as I am not a valid mysqli_result - hence the second error message.

You are checking $ dbc at the end of your script, but in fact, it is very interesting to do an investigation very quickly - in fact, every call < After code> mysql_query , I will say.


Also, as a sidenote: you probably do not need mysqli class for example that many times: just once Instciciate, and then use the $ mysqli variable for the rest of your code.

Just for security, and any trouble Areas To avoid this, you might also want to use the $ page variable before injecting it into question.

Oh, and, BTW: You may be interested to use MySQL ;-) (Well, that's if page is your The primary key of the table is, at least ...)


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -