Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

If a code is in db I want it to generate a random #

Heres what I got:

Code
<?php
    require("inc/connection.php");
    if ($link->connect_error)
        die('Connect Error: '.$link->connect_errno.': '.$link->connect_error);
    $insertedcode = $_POST['code'];
    //$results = $link->query("SELECT code FROM code WHERE code = :code");
    $query_string = "SELECT code FROM code WHERE code = :code";
    $query_params = array(
        ':code' => $_POST['code']
    );

    $stmt = $link->prepare($query_string);
    //$stmt->bind_param('s', $query_params[':code']);
    $result = $stmt->execute($query_params);
    $row = $stmt->fetch();
    if($row) {
        $number = mt_rand(0,3999);
        echo $number;
    }
?>

But it doesnt work i get this error please help:

Fatal error: Call to a member function execute() on a non-object
Since $link is supposed to return an object on prepare I guess it returns a bad result, checking out your query
Code
SELECT code FROM code WHERE code = :code
You should specify the column as code.code
As per the documentation, PDO will return false or throw an error on failure.

Obviously it is failing. Set the PDO error mode to PDO::ERRMODE_EXCEPTION then catch the exception and examine it's message to find out why your query preparation is failing.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by MemomaOct 5, 2018 12:32 AM 3 171 Oct 5, 2018 12:32 AM
Last Post: Strayaa
[PHP] by SkidyDec 11, 2017 11:02 AM 6 234 Dec 11, 2017 11:02 AM
Last Post: Charger
[PHP] by Ashish BishtOct 23, 2016 11:20 AM 8 351 Oct 23, 2016 11:20 AM
Last Post: F3nrir