A
A
Alexander Sharomet2014-09-08 09:28:26
PHP
Alexander Sharomet, 2014-09-08 09:28:26

How to check if a mysqli entry exists?

Hello. I can't figure out how to check if a record exists in a table.

$mysqli=includeDB(); //подключение к базе
$ip=getIp(); // получаю ip
$res=$mysqli->query("SELECT * FROM users WHERE ip='.$ip.' AND id_rating='.$id.'") or die (mysql_error());
if($res->num_rows)return true; //true если запись имеется
return false; //если её нет
$mysqli->close();

constantly shows false, even though there is a record or not(
Thank you!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
andreyqin, 2014-09-08
@sharomet

You have an error in your request, try this:

$res = $mysqli->query('SELECT * FROM users WHERE ip = "' . $ip . '" AND id_rating = ' . $id) or die (mysql_error());

F
FanatPHP, 2014-09-08
@FanatPHP

Some questions (instead of answers) just want to take, and gouge.
To answer such - only to spoil the gene pool of mankind.
So that an illiterate shkolota does not accuse of unfoundedness, let's briefly go through the rake laid out in this typical code, alas, for a mess:
1. We connect to the database for each request, and immediately close the connection.
2. getIp() - inside there is a classic shit code for getting an IP address from HTTP headers, which shkolota from generation to generation passes on to each other as secret knowledge.
3. and immediately substitute the received address into the request, receiving an injection.
4. Learning syntax using copy-paste is a terrible thing. Well, how can you not get confused in these incomprehensible dots and quotation marks? The inability to distinguish SQL code from PHP code has been passed down from generation to generation.
5. Also mindless copy-paste leads to constructions like $res = $ mysqli ->query('...') or die ( mysql _error());
6. The entire construction tells us that the mysql ext ban hysteria was absolutely pointless. The devastation is not in the extensions, but in the heads. Who with his head on his shoulders - he himself would begin to use something new, without prohibitions. And who, in the old fashioned way, without connecting the head, will not be helped by any prohibitions on writing the same vulnerable shit code.

S
Sali_cat, 2014-09-08
@Sali_cat

go to base)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question