A
A
Alexander Grigoriev2020-01-13 00:07:39
Python
Alexander Grigoriev, 2020-01-13 00:07:39

Why is mysqli_query returning NULL?

Why does it return null?

$q = mysqli_query($connect, "SELECT * FROM `tabel` WHERE `served_id` = '{$served}' AND `receiver` = '{$receiver}'";
$query = mysqli_fetch_assoc($q);

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Gornostaev, 2019-03-26
@AnnoyingWolf

right_answers = [-1, 1, 0]

print('Сколько будет 3+4-4*2, 9%4 и ~-1?')
while True:
    user_answers = (int(input('Введите {} число:'.format(i))) for i in ('первое', 'второе', 'третье'))
    equalities = [a == b for a, b in zip(right_answers, user_answers)]
    if all(equalities):
        print('Поздравляю, вы ответили верно!')
        break
    else:
        if sum(equalities):
            nums = ', '.join('{}-й'.format(n) for n, i in enumerate(equalities, start=1) if i)
            print('Вы ответили верно на {} вопрос'.format(nums))
        else:
            print('Вы ответили не верно')
    print('Попробуйте снова...')

print('Хорошая работа!!!')
input('Нажмите ENTER чтобы завершить')

A
AlmeryCloud, 2020-01-13
@AlmeryCloud

You have specified the $query variable in mysqli_fetch_assoc but it looks like you need the $q variable

S
Svyatoslav, 2020-01-13
@ShameQNS

mysqli_query cannot return null, only true, false and an object of type mysqli_result.
mysqli_fetch_assoc will return null if there are no records matching your query.
PS In mysqli_fetch_assoc, pass the $q variable, not $query.

A
AUser0, 2020-01-13
@AUser0

First, there is a bug in the code, there is no closing parenthesis at the end of mysqli_query().
And second, do this:

$q = mysqli_query($connect, "SELECT * FROM tabel WHERE served_id='".mysqli_real_escape_string($connect, $served)."' AND receiver='".mysqli_real_escape_string($connect, $receiver)."'") or die(mysqli_error($connect));
$query = mysqli_fetch_assoc($q) or die(mysqli_error($connect));

and check your browser for errors.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question