PHP ve MySQL hitcounter sorun?

1 Cevap php

Tamam ben php ve ben bir web sayfası isabet sayacı oluşturmak için ama benim sayfa sayacı doğru güncelleme yok denedim mysql yeniyim ve aşağıda listelenen aşağıdaki uyarıları alır.

Warning: mysqli_query() expects at least 2 parameters, 1 given in
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in

Aşağıda php kodudur.

<?php

$page = $_SERVER['SCRIPT_FILENAME']; 

// Query member data from the database and ready it for display
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT id page FROM mysql_counter_logs WHERE page = '$page'");

if (mysqli_num_rows($dbc) == 0) {
	$mysqli = new mysqli("localhost", "root", "", "sitename");
	$dbc = mysqli_query($mysqli,"INSERT INTO mysql_counter_logs (page) VALUES ('$page')");
	mysqli_query($dbc);
} elseif (mysqli_num_rows($dbc) == 1) {
	$mysqli = new mysqli("localhost", "root", "", "sitename");
	$dbc = mysqli_query($mysqli,"UPDATE mysql_counter_logs SET hits = hits + 1 WHERE page = '$page'");
	mysqli_query($dbc);
}

//Retreives the current count
$count = mysqli_fetch_row(mysqli_query($mysqli,"SELECT hits FROM mysql_counter_logs"));

if (!$dbc) {
	// There was an error...do something about it here...
	print mysqli_error();
} 


//Displays the count on your site
print "$count[0]";

?>

1 Cevap

Kodunuzu bir özü:

$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"INSERT INTO mysql_counter_logs (page) VALUES ('$page')");
mysqli_query($dbc);

Her şeyden önce, kılavuzuna göre mysqli_query, bir fonksiyonu olarak kullanıldığında:

mixed mysqli_query  ( mysqli $link  , string $query  [, int $resultmode  ] )

Hangi ikinci bir parametre olarak ilk parametre olarak $mysqli değişkeni, ve sorgu geçmek zorunda kalıyoruz.

Eğer kod diğer bölgelerinde doğru kullandığınızı yaparken, ben kopyalayıp yapıştırılan üçüncü satırında mysqli_query bu yolu kullanarak neden Ne alamadım?

Eğer burada işlevini kullanmak demek emin misiniz?


Also, knowing on which lines you are getting those error messages might help ;-)
Check you don't do anything else "wrong" there -- like " *mysqli_query() expects at least 2 parameters, 1 given* "


Thinking about it : mysqli_query will return boolean false if there is an error.

Dolayısıyla ikinci hata mesajı - geçerli bir mysqli_result değil gibi bu durumda, sen, mysqli_query dönüş değeri mysqli_num_rows dememeliyiz.

Siz senaryonun sonunda $dbc için kontrol ediyoruz, ama bir biit er de onu kontrol etmek ilginç olabilir - mysql_query, derim her aramadan sonra, aslında .


Also, as a sidenote : you probably don't need to instance the mysqli class that many times : just instanciate it once, and then use the $mysqli variable for there rest of your script.

Sadece güvenlik için, ve herhangi bir sorun önlemek için, ayrıca sorguları içine enjekte önce mysqli_real_escape_string on the $page değişkeni, kullanmak isteyebilirsiniz.

Oh, and, btw : you might be interested by the INSERT ... ON DUPLICATE KEY UPDATE Syntax that MySQL allows to use ;-)
(Well, that is if page is the primary key of your table, at least...)