NEREDEN seçerek sorun?

3 Cevap php

Bu kodu vardır, ama Fld2 de '555 gibi bir değer 'olduğunu ya da hiç FLD2 orada bile bile garip, her zaman, "evet!" Dışarı echos.

$testresult = mysqli_query($database, "SELECT * FROM imei359327039828680 WHERE FLD2 = '555'");

if ( $testresult ) { echo('yes!'); } else { echo('no!'); }

Herhangi bir fikir, teşekkürler!

3 Cevap

ctshryock tarafından belirtildiği gibi, iyi-oluşturulmuş SQL sorgusu döndürülen değeri bir değişken seti olarak her zaman görülecektir true mantıksal bir nesne olarak gözden zaman.

Herhangi bir veri döndürdü olup olmadığını test etmek için, mysql_num_rows() PHP Documentation hangi satır numarası döndürülür dönecektir kullanabilirsiniz. Sorgu herhangi bir satır maç olmaz, o zaman bu fonksiyon if() durumuna göre false olarak görüldü olacak 0 dönmelidir.

$testresult = mysql_query( "SELECT * FROM imei359327039828680 WHERE FLD2 = '555'" );

if( !$testresult )
  die( 'SQL Error' ); # The SQL Query Failed for some reason

if( mysql_num_rows( $testresult ) )
  die( 'SQL Returned No Result' ); # The SQL Query returned nothing

while( $r = mysql_fetch_assoc( $testresult ) ) {
  # Process your returned rows here
}

Sorgu bir şekilde başarısız olmadıkça mysqli_query Her zaman bir sonuç nesne döndürür. Sorgu sadece herhangi bir satır dönmezse, yine bir sonuç nesne olsun; sadece o herhangi bir satır olmaz.

$testresult, doğruysa test ediyoruz değil sorgu beklenen sonuçları onların isen. Sorgu geçerli ise, yani iyi oluşmuş, geçerli bir nesne döndürür. Eğer bir şey ya da bulursa görmek için $testresult dönüş değeri içine bakmak istiyorum.