-1

I'm not understand why the query is not working, if i try using localhost is work fine but using "mysql" and i try to use PDO in my website and not give a result. and i try to look the error_log i a get error like this

PHP Fatal error:  Call to a member function fetch() on a non-object in fungsi/f_record.php on line 9

This my source f_record.php

<?php
include "../config/c_config.php";
$db = dbConn::getConnection();

$uq = strtolower($_GET["term"]);
$return = array();
$sqlac= "SELECT * FROM str_user WHERE name LIKE '$uq'";
$resac = $db->query($sqlac);
while($resac = $sqlac->fetch(PDO::FETCH_ASSOC)){
    array_push($return,array('label'=>$resac['name'],'value'=>$resac['name']));
    }
    echo(json_encode($return));
?>

and index.php

    <script>
    $(function() {
        $( "#txtname" ).autocomplete({
            source: "f_record.php",
            minLength: 1
        });
    });
    </script>

</head>
<body>


<div>
    <label for="Name">Name: </label>
    <input id="txtname" class="txtname"/>
</div>

how to fix this..

Riski Febriansyah
  • 335
  • 1
  • 8
  • 21

2 Answers2

0

Read the error message carefully.

Call to a member function fetch() on a non-object

There is one call to fetch() in your code. It is $sqlac->fetch(PDO::FETCH_ASSOC).

$sqlac is a string. You can't call fetch on a string because it isn't an object.

This is covered in the PHP reference.

Community
  • 1
  • 1
Paul
  • 6,572
  • 2
  • 39
  • 51
0

First create the db handle properly, I am not sure "$db" how it gets created in your code sine you have not mentioned code for dbconn::getconnection(). I have replaced it with $dbh.

    <?php
    include "../config/c_config.php";
    $db = dbConn::getConnection();
    *$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);*

    $uq = strtolower($_GET["term"]);
    $return = array();
    $sqlac = $dbh->prepare("SELECT * FROM str_user WHERE name LIKE '$uq'");
    $sqlac->execute();

    while($resac = $sqlac->fetch(PDO::FETCH_ASSOC)){
        array_push($return,array('label'=>$resac['name'],'value'=>$resac['name']));
        }
        echo(json_encode($return));
    ?>
Rahul
  • 2,189
  • 7
  • 40
  • 60