2015-06-14 51 views
0

我設法做我想做的事,但如果用戶不匹配第一個數字,那麼它表示他們沒有匹配任何時候他們可以匹配5等等......這就像是彩票:)PHP&Mysql,檢查表中的列是否與另一個表中的另一列相匹配(複雜)

<? 
include "includes/db_connect.php"; 

$email=$_SESSION["real_name"]; 
$sql="SELECT * from users WHERE email='$email'"; 
$result=mysql_query($sql); 

while($rows=mysql_fetch_array($result)){ // Start looping table row 

$one2=$rows['1']; 
$two2=$rows['2']; 
$three2=$rows['3']; 
$four2=$rows['4']; 
$five2=$rows['5']; 
$six2=$rows['6']; 
{ 
     $q = mysql_query("SELECT * FROM `numbers` WHERE `1` = '$one2' OR `2` = '$one2' OR `3` = '$one2' OR `4` = '$one2' OR `5` = '$one2' OR `6` = '$one2' LIMIT 1"); 
     $get = mysql_num_rows($q); 
if($get == 0) { 
    echo 'Sorry you didnt match anything'; 
} else { 
    $q1 = mysql_query("SELECT * FROM `numbers` WHERE `1` = '$two2' OR `2` = '$two2' OR `3` = '$two2' OR `4` = '$two2' OR `5` = '$two2' OR `6` = '$two2' LIMIT 1"); 
     $get1 = mysql_num_rows($q1); 
if($get1 == 0) { 
    echo 'You matched one number'; 
} else { 
    $q2 = mysql_query("SELECT * FROM `numbers` WHERE `1` = '$three2' OR `2` = '$three2' OR `3` = '$three2' OR `4` = '$three2' OR `5` = '$three2' OR `6` = '$three2' LIMIT 1"); 
     $get2 = mysql_num_rows($q2); 
if($get2 == 0) { 
    echo 'You matched two numbers'; 
} else { 
    $q3 = mysql_query("SELECT * FROM `numbers` WHERE `1` = '$four2' OR `2` = '$four2' OR `3` = '$four2' OR `4` = '$four2' OR `5` = '$four2' OR `6` = '$four2' LIMIT 1"); 
     $get3 = mysql_num_rows($q3); 
if($get3 == 0) { 
    echo 'You matched three numbers'; 
} else { 
    $q4 = mysql_query("SELECT * FROM `numbers` WHERE `1` = '$five2' OR `2` = '$five2' OR `3` = '$five2' OR `4` = '$five2' OR `5` = '$five2' OR `6` = '$five2' LIMIT 1"); 
     $get4 = mysql_num_rows($q4); 
if($get4 == 0) { 
    echo 'You matched four numbers'; 
} else { 
    $q5 = mysql_query("SELECT * FROM `numbers` WHERE `1` = '$six2' OR `2` = '$six2' OR `3` = '$six2' OR `4` = '$six2' OR `5` = '$six2' OR `6` = '$six2' LIMIT 1"); 
     $get5 = mysql_num_rows($q5); 
if($get5 == 0) { 
    echo 'You matched five numbers'; 
} else { 
    echo 'You matched all the numbers'; 
} 
?> 


<? }}}}}}} ?> 

現在你所看到的代碼,我想我需要更多的升技解釋。 one2-six2是用戶選擇的6個數字。 他們需要與表中的數字匹配「數字」 它可以工作,但如果用戶不匹配他們的第一個數字(one2)但匹配(two2)例如,那麼它表示他們沒有匹配任何。

有什麼建議嗎?

+0

1.停止使用MySQL_ *其破舊和使用mysqli_ *然後對於每一個抱歉替換抱歉,你沒有得到你的第一個/第二個/ etc的號碼,那麼它會移動到下一個說你有號碼/二/三/等如果得到一個正確的號碼 –

+0

1.停止使用MySQL_ *它的破舊和使用mysqli_ * 2.結束之後,每一個在另一個之前。然後對於每一個抱歉替換抱歉,你沒有得到你的第一個/第二個/等號碼,然後它會移動到下一個說如果得到了一個正確的號碼,你有號碼/二/三/等。應該像if($ one2 === $ one2_query){$ correct = $ number + 1; } else {$ wrong = $ number_wrong +1;} //狀態結束Echo'you got'。$ number。' Numbers Right and'。$ number_wrong。'數字錯了。'; –

+0

你可以發佈一個例子: $ q = mysql_query(「SELECT * FROM'numbers' WHERE'1' ='$ one2'或'2' ='$ one2'或'3' ='$ one2'或者' 4'='$ one2'或'5'='$ one2'或'6'='$ one2'LIMIT 1「); $ get = mysql_num_rows($ q); if($ get == 0){ echo'對不起,你沒有匹配任何東西'; } else { ///首字母匹配,檢查第二等.. 我的代碼的工作原理是如何我希望它的工作,但如果他們的第一個數字不匹配,但第二個它說沒有作品,所以它說的第一個沒有匹配,它需要檢查他們的第二個等... 我是相當新的PHP因此,我很困惑 – StuckonPHP

回答

0
<? 
include "includes/db_connect.php"; 

$email=$_SESSION["real_name"]; 
$sql="SELECT * from users WHERE email='$email'"; 
$result=mysql_query($sql); 

$number = 0; 
$number_wrong = 0; 

while($rows=mysql_fetch_array($result)){ // Start looping table row 

$one2=$rows['1']; 
$two2=$rows['2']; 
$three2=$rows['3']; 
$four2=$rows['4']; 
$five2=$rows['5']; 
$six2=$rows['6']; 
{ 
     $q = mysql_query("SELECT * FROM `numbers` WHERE `1` = '$one2' OR `2` = '$one2' OR `3` = '$one2' OR `4` = '$one2' OR `5` = '$one2' OR `6` = '$one2' LIMIT 1"); 
     $get = mysql_num_rows($q); 
if($get == 0) { 
    $number++; 
} else { 

$number_wrong++; 
} 
    $q1 = mysql_query("SELECT * FROM `numbers` WHERE `1` = '$two2' OR `2` = '$two2' OR `3` = '$two2' OR `4` = '$two2' OR `5` = '$two2' OR `6` = '$two2' LIMIT 1"); 
     $get1 = mysql_num_rows($q1); 
if($get1 == 0) { 
    $number++; 
} else { 

$number_wrong++; 
} 

    $q2 = mysql_query("SELECT * FROM `numbers` WHERE `1` = '$three2' OR `2` = '$three2' OR `3` = '$three2' OR `4` = '$three2' OR `5` = '$three2' OR `6` = '$three2' LIMIT 1"); 
     $get2 = mysql_num_rows($q2); 
if($get2 == 0) { 
    $number++; 
} else { 

$number_wrong++; 
} 
    $q3 = mysql_query("SELECT * FROM `numbers` WHERE `1` = '$four2' OR `2` = '$four2' OR `3` = '$four2' OR `4` = '$four2' OR `5` = '$four2' OR `6` = '$four2' LIMIT 1"); 
     $get3 = mysql_num_rows($q3); 
if($get3 == 0) { 
    $number++; 
} else { 

$number_wrong++; 
} 
    $q4 = mysql_query("SELECT * FROM `numbers` WHERE `1` = '$five2' OR `2` = '$five2' OR `3` = '$five2' OR `4` = '$five2' OR `5` = '$five2' OR `6` = '$five2' LIMIT 1"); 
     $get4 = mysql_num_rows($q4); 
if($get4 == 0) { 
$number++; 
} else { 

$number_wrong++; 
} 
    $q5 = mysql_query("SELECT * FROM `numbers` WHERE `1` = '$six2' OR `2` = '$six2' OR `3` = '$six2' OR `4` = '$six2' OR `5` = '$six2' OR `6` = '$six2' LIMIT 1"); 
     $get5 = mysql_num_rows($q5); 
if($get5 == 0) { 
    $number++; 
} else { 

$number_wrong++; 
}} 

Echo 'you got '.$number.' Numbers right and '.$number_wrong.' Numbers wrong.'; 
?> 

對不起,在我的手機上這樣做。不是最簡單的。請檢查語法。

+0

看起來不錯, 它說:「你有數字正確和數字錯誤。」 不保存數量的數量 任何想法 – StuckonPHP

+0

經過一些調整, 它的100%工作,我想如何。 – StuckonPHP

+0

謝謝,它的完美 – StuckonPHP

0

問題已回答! 不知道這是否是正確的方式對不起

+0

如果Jamie回答了問題,請檢查答案旁邊的綠色複選標記。 – dmikester1

相關問題