Check if value exist in db - new php version

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());



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

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…

$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";
Sponsor our Newsletter | Privacy Policy | Terms of Service