# Check if value exist in db - new php version

**URL:** <https://www.phphelp.com/t/check-if-value-exist-in-db-new-php-version/33954>\
**Category:** MySQL database\
**Created:** [September 16, 2021, 11:24am UTC](https://www.phphelp.com/t/check-if-value-exist-in-db-new-php-version/33954 "2021-09-16T11:24:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neo71](https://www.phphelp.com/letter_avatar_proxy/v4/letter/n/9fc29f/32.png) [@Neo71](https://www.phphelp.com/u/Neo71)\
**Post date:** [September 16, 2021, 11:24am UTC](https://www.phphelp.com/t/check-if-value-exist-in-db-new-php-version/33954/1 "2021-09-16T11:24:21Z")

</div>

in php 5 this code was correct

// ceck if CF alredy exist  
$ctrl\_cf = @mysql\_query($db,“SELECT id FROM avvisi WHERE cf=’$cfavviso’”) or die(mysql\_error());

if(@mysql\_num\_rows($ctrl\_cf)\>0)  
{  
?\>  
  
\<?php  
}else{

Now, with php 7 i must use mysqli, but if i use " or die(mysql’\_error());" ther was an error: mysqli\_error()  
expects parameter 1 to be mysqli, null given in

sorry for my english

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [September 16, 2021, 2:32pm UTC](https://www.phphelp.com/t/check-if-value-exist-in-db-new-php-version/33954/2 "2021-09-16T14:32:14Z")

</div>

Well, first, that old code was never correct. You do not use the @ at the beginning of mysql commands.  
That means it suppresses the error message. Therefore, the die may not give you the error.  
Also, nobody has used mysql in years. Mysqli replaced it as of PHP5.0. But, for better, much better security, you should learn PDO. It is what most professionals use today.

For mysqli, here is an example with the connection and a simple query with row count checking. Hope it helps…

```auto
<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";

$sql = "SELECT id, firstname, lastname FROM users";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
      echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
  echo "0 results";
}
?> 

```
