1

I have been trying to make working php code which will return "1".

As well the database information is all removed, but is accurate since I get no "3" error.

Example being: www.mysite.com/login.php/?test=itemindatabase

and if "itemindatabase" is found in the database, then it will return "1"

A few notes:

login is the name of the table.

test is the name of the only column (other than id)

<?php 
 $host = "localhost";  
 $username = "";  
 $password = "";  
 $database = "";  

      $connect = new PDO("mysql:host=$host; dbname=$database", $username, $password);  
      $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
           if(empty($_POST["test"]))  
           {  
                die("3");  
           }  
           else  
           {  
                $query = "SELECT * FROM login WHERE test = :test";  
                $statement = $connect->prepare($query);  
                $statement->execute(  
                     array(  
                          'test'     =>     $_POST["test"]  
                     )  
                );  
                $count = $statement->rowCount();  
                if($count > 0)  
                {  
                     die("1"); //success!
                }  
                else  
                {  
                     die("0"); //fail not found
                }  
        }

?>

I have only been getting "0" every try which means failure.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Johan Doe
  • 43
  • 3
  • 10

1 Answers1

2

Things:-

1.Your all db credentials are empty? if you removed them because of security issue then Ok. Otherwise please provide valid and correct values there.Otherwise connection itself will not create.

2.$_POST["test"] need to be $_GET["test"]

3.array('test'=>$_POST["test"]) need to be array(':test'=>$_POST["test"])

Check this code and try:-

<?php

 //comment these two lines when code started working fine
 error_reporting(E_ALL);
 ini_set('display_errors',1);

 $host = "localhost";  
 $username = "";  //check and provide valid value
 $password = "";  //check and provide valid value
 $database = "";  //this can't be empty in any case

    try {
        $connect = new PDO("mysql:host=$host;dbname=$database", $username, $password);  
        $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  

           if(!empty($_GET['test'])){
                $statement = $connect->prepare("SELECT * FROM login WHERE test = :test");  
                $statement->execute(array(':test'=>$_GET["test"]));  
                $count = $statement->rowCount();  
                if($count > 0)  
                {  
                     echo "record found successfully";
                }  
                else  
                {  
                     echo "no record found";
                }  

           }else{
             echo "Please provide input value";
           }
    }
    catch(PDOException $e){
        echo "Error: " . $e->getMessage();
    }
    $conn = null;
?>

Note:- How to fetch records

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • I modified the original description. All the database information is in the actual script but I removed due to privacy. And thank you for your answer. But I am still getting a return of "0" – Johan Doe Jun 06 '17 at 04:28
  • Yes I have. thanks for the answer as I am sure it has helped me to further fixing this issue. – Johan Doe Jun 06 '17 at 04:30
  • Worked like a charm! Thank you so much! – Johan Doe Jun 06 '17 at 04:43
  • I have a quick question. How would I make it print the correct input back using this code? ie: a row in the database was "found" and it will print found on the page – Johan Doe Jun 06 '17 at 04:47
  • @JohanDoe Instead of asking everything try to learn from php manual or w3school. Check this manual for help:- http://php.net/manual/en/pdostatement.fetch.php – Alive to die - Anant Jun 06 '17 at 04:53