MYSQL result returns incorrectly

So, I have a piece of code that checks to see if you are following the user or not. And basically you can follow them if you haven't. So here

if($_SESSION['loggedIn'] == true){
    $result = $con->prepare("SELECT * FROM followers WHERE follow_from = :username AND follow_to = :post_id");
    $result->bindParam(':username', $username);
    $result->bindParam(':post_id', $follower);
    $result->execute();
    $reprint = $result->fetchAll(PDO::FETCH_ASSOC);
}
print_r($reprint);
if($reprint < 1){
    $stmt = $con->prepare("INSERT INTO followers (follow_from, follow_to) VALUES (:ff, :ft)");
    $stmt->bindValue(':ff', $follower, PDO::PARAM_STR);
    $stmt->bindValue(':ft', $username, PDO::PARAM_STR);
    $stmt->execute();
}
else{
    echo 'Error';
    exit();
}
//Display follower
$stmt1 = $con->prepare("SELECT COUNT(*) AS count FROM followers WHERE follow_to = :username");
$stmt1->bindValue(':username', $username, PDO::PARAM_STR);
$stmt1->execute();
$likes = $stmt1->fetchAll(PDO::FETCH_ASSOC);
print_r($likes);

So, when I run it once. I repeat the else statement. My question is, why is this happening? I have no entry in the database, so I would expect it to enter once. I do not get any errors. loggedIntruly. And the variables are passed successfully. Any ideas?

+4
source share
3 answers

, fetchAll(). , . , , , , .

, , . , followers? ( , , , ).

, , INSERT. followers (follow_from,follow_to), , , 'Duplicate entry' INSERT. . 'Duplicate entry', , , , .

, :

$stmt = $con->prepare("INSERT 
                         INTO followers (follow_from, follow_to)
                       VALUES (:ff, :ft)");
$stmt->bindValue(':ff', $follower, PDO::PARAM_STR);
$stmt->bindValue(':ft', $username, PDO::PARAM_STR);
$result = $stmt->execute();
if ($result) {
     /* this follow pair was successfully added */
} else {
     /* MySQL may return the error 'Duplicate entry' */
     if (false == stripos($stmt->errorCode,'Duplicate')){
        echo 'Something failed in the insert: ' . '$stmt->errorCode';
     }
     else {
         /* this follow pair was already in your table */
     }
}

Pro tip: SELECT * ; ; , , , , .

Pro tip: , WHERE, COUNT() , . , ?

+4

count ($ reprint) , . $reprint - ,

if(count($reprint) < 1)
{
    $stmt = $con->prepare("INSERT INTO followers (follow_from, follow_to) VALUES (:ff, :ft)");
    $stmt->bindValue(':ff', $follower, PDO::PARAM_STR);
    $stmt->bindValue(':ft', $username, PDO::PARAM_STR);
    $stmt->execute();
}
else
{
    echo 'Error';
    exit();
}
+1

PDOStatement:: fetchAll

PDOStatement:: fetchAll - ,

, , -.

, - :

try
{
    ...
}
catch (PDOException $e)
{
    echo $e->getMessage();
}

PDO:

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

, , .

, ,

$reprint = $result->fetchAll(PDO::FETCH_ASSOC);

( , ),

$reprint = $result->fetchAll(PDO::FETCH_ASSOC);

, , else.

Edit

, ", , else. [...] ", " , doesn" ".

Can you talk more clearly about the current, current, problem?

+1
source

Source: https://habr.com/ru/post/1546554/


All Articles