0

i can't seem to get the code working to determine how to check if a record exists in Mysql table. Im not very good at this but i'm learning. i would like to check if a certain barcode exists in my table called users.

i get the following error when i execute the code below.

Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\watermeters\see.php:14 Stack trace: #0 {main} thrown in C:\xampp\htdocs\watermeters\see.php on line 14

See the code below. Can someone help me with this code?


<?php


$link = mysqli_connect("localhost", "root", "", "watermeter");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}



$query = "SELECT barcode from users where barcode='5645454'";
$result = mysql_query($link, $query);

if(mysql_num_rows($result) > 0)
{
    // row exists. do whatever you would like to do.
} else {

    // row does not exists. do whatever you would like to do.
}




// Close connection
mysqli_close($link);

?>

Sjaakarie
  • 19
  • 6
  • `mysql_query` is a typo, `mysqli_query ` – user3783243 Mar 23 '19 at 17:36
  • you want `mysqli_query` not `mysql_query` – akaBase Mar 23 '19 at 17:36
  • `mysql_num_rows` also needs to chabge – user3783243 Mar 23 '19 at 17:37
  • PHP has three different MySQL interfaces: mysql, mysqli, and PDO. The mysql interface uses function names that start with `mysql_`. These have been deprecated and are not available in new PHP versions -- don't use these. The mysqli interface uses function names that start with `mysqli_`. The PDO interface is object oriented only. See [**this page**](https://phptherightway.com/#databases) for more details and some good examples. – Alex Howansky Mar 23 '19 at 17:38

1 Answers1

0

You are mixing mysql and mysqli

Use mysqli_query

Andrei Lupuleasa
  • 2,677
  • 3
  • 14
  • 32