A
A
Andrew2015-05-08 12:10:57
PHP
Andrew, 2015-05-08 12:10:57

Incomprehensible work of the script. Why does mysqli_query() return an empty object?

Good afternoon.
I need your advice on how the script works.

$city =  iconv('windows-1251', 'UTF-8', $_GET['city']);
    $departament =  iconv('windows-1251', 'UTF-8', $_GET['departament']);
    $host = *************;
    $user = *************;
    $pass = *************;
    $data_base = **********;
    $connect = mysqli_connect($host,$user,$pass,$data_base) or die('Could not connect: ' . mysqli_error($connect));
    $sql="SELECT  * FROM 'имя таблицы' WHERE city = '".$city."'OR departament = '".$departament."'";
    $result = mysqli_query($connect,$sql) or die("ERROR: ".mysqli_error($connect));
    while($row = mysqli_fetch_array($result)){
        echo<<<"OUTPUT_DATA"
            <a href= $row[href] class="sj-j" data-r="34" data-c="9 ">
                <em><b>$row[vacancy]</b></em>
                <div></div>
                <i>$row[city]</i>
            </a>
OUTPUT_DATA;
    }
    mysqli_close($connect);

Task : there is a database with a table of vacancies (vacancy, requirements, city .. etc.). You need to make a form on the site, something similar to www.work.ua/jobs/by-company/297373/ (under the company description, "open vacancies").
How I did it: Through ajax - the request I get the necessary parameter (city or section) and then I process it all in the php script (the code is given above). When developing and debugging on OpenServer, everything worked. e. I choose the required city or section and, in fact, everything .. shows nothing.
I checked the login, password, host and database name 100 times, everything was entered correctly. There are no spaces after echo<<<"OUTPUT_DATA" either.
It is not clear why var_dump($result); returns object(mysqli_result)#2 (0) { } ?
Could it be related to the php version? (on site 5.2.) on openserver (5.3).
Tell me, please, what is my mistake?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
Fedor Kirichenko, 2015-05-08
@fxpelive

WHERE city = '".$city."'OR
Where is the space before OR ?

M
myfirepukan, 2015-05-08
@myfirepukan

one.
2.

while($row = mysqli_fetch_array($result)){
$did[] = $row;
}
var_dump($did);

And see what var_dump says

F
fedot1325, 2015-05-08
@fedot1325

$mysqli = new mysqli($host, $user, $pass, $data_base);

$vacancies = $mysqli->prepare("SELECT * FROM 'имя таблицы' WHERE city = ? OR departament = ?");
        if ($vacancies) {
            $vacancies->bind_param('ss', $city, $departament); 
            $vacancies->execute();
            $vacancies->store_result();
            $countRows = $vacancies->num_rows;
            if ($countRows != 0){
            //на каждый столбец нужно забиндить по переменной, либо указывать в SELECT конкретные и биндить конкретные.
              $vacancies->bind_result($href, $vacancy, $city_result, $departament_result); 
                while ($vacancies->fetch()) {
                      echo '<a href="' . $href . '" class="sj-j" data-r="34" data-c="9 ">
                                <em><b>' . $vacancy . '</b></em>
                                <div></div>
                                <i>' . $city_result . '</i>
                            </a>';
                }
            }
            else{
                echo 'По вашему запросу ничего не найдено';
            }
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question