2017-04-16 87 views
0

我想回到$contact_id,但我得到的是:为什么我的mysqli_fetch_assoc不工作?

success. 
the user id is 

例如,在我的insert.htm我输入$ CheckContact: enter image description here

应该返回:the user id is 11。这是我的user表:

enter image description here

如果我在浏览器地址栏再次点击checkcontact.php然后我得到:

the user id is 23 

它总是23,无论什么$CheckContact是。 你能告诉我什么是错的吗?这里是我的代码:

<?php 

require('dbConnect.php'); 

$CheckContact = $_POST['phonenumber']; 
$sql = "SELECT * FROM user WHERE username = '$CheckContact'"; 
$result = mysqli_query($con, $sql); 
$check = mysqli_fetch_array($result); 
//if $CheckContact is in the user table... 
if(isset($check)) { 

    echo 'success.' . "<br>"; 

    // get the associated rows of $CheckContact 
     $row = mysqli_fetch_assoc($result); 
    // get the associated user_id in that row 

     $contact_id = $row["user_id"]; 
     echo "the user id is ", $contact_id; 

} 
//if $CheckContact is NOT in the user table... 
else { 

    echo 'failure'; 
    } 

?> 
+1

你打开了PHP中的错误报告吗? –

+1

你可以从insert.htm分享你的表单吗? –

+0

在您的查询失败的情况下,可能会发生此问题。 '$ check'将为空,并且因为它现在被设置,所以它传递'isset'的检查。而是使用'!empty' –

回答

0
why dont you use this: 
<?php 
$sql="Select query"; 
       $result=mysqli_query($con,$sql); 
       while($rws=mysqli_fetch_assoc($result)){ 
       ?> 
to print all data 
<?php 
} 
?> 

尝试空的()为$检查这将指示变量是否包含空值或不

2

当我我的代码更改为:

<?php 

require('dbConnect.php'); 

$CheckContact = $_POST['phonenumber']; 
$sql = "SELECT * FROM user WHERE username = '$CheckContact'"; 
$result = mysqli_query($con, $sql); 
$num_rows = mysqli_num_rows($result); 

if($num_rows >= 1) { 

etc.... 

它工作正常。