-3

I have this problem with short php code I am using in to get suggestions for JQuery autocomplete. I have tried many things but I am unable to find solution:

//connect to your database
<?php require('Connections/con_1.php');?>
<?php

//retrieve the search term that autocomplete sends
$term = trim(strip_tags($_REQUEST['term']));

//save search queries to csv file
$file = fopen("searchqueries.csv","a");
fputcsv($file,explode(',',$term));
fclose($file);

//query the database for entries containing the term
$qstring ="USE database_2";"SELECT DISTINCT Pracodawca as value FROM Pensje WHERE Pracodawca LIKE '%".$term."%'";
$result = mysql_query($qstring) or die($result.mysql_error());

//loop through the retrieved values        
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
    {

    //build an array
    $row['value']=htmlspecialchars(stripslashes($row['value']));
    $row_set[] = $row['value'];
    $row['value']=
    $row_set[] = array('value' => ($row['value']));
    }

//format the array into json data
echo json_encode($row_set, JSON_UNESCAPED_UNICODE);
?>

I get error "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in" and the result is NULL.

What I am doing wrong?

Piotrek
  • 23
  • 1
  • 7
  • If you're writing new code, **_please_ don't use the `mysql_*` functions**. They are old and broken, were deprecated in PHP 5.5 (which is so old it no longer even receives security updates), and completely removed in PHP 7. Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577 for details. – Chris Mar 19 '17 at 12:26
  • First of all, use `mysqli_*` ... second... you can't send 2 queries at once like this.... you need to seperate them... 3rd... well fix the first ones and maybe someone will help ya. – Flash Thunder Mar 19 '17 at 12:28

1 Answers1

0

Please don't use the mysql_* functions.

Change the following line:

$qstring ="USE database_2";"SELECT DISTINCT Pracodawca as value FROM Pensje WHERE Pracodawca LIKE '%".$term."%'";

to

$qstring = "SELECT DISTINCT Pracodawca as value FROM Pensje WHERE Pracodawca LIKE '%".$term."%'";

As there is no need to pass USE dbname when you are querying through php as database is already selected at the time of connecting with database.

and try again.

Mayank Pandeyz
  • 24,624
  • 3
  • 35
  • 55