Hatim
Hatim

Reputation: 1522

How to Check if value exists in a MySQL database

Suppose I have this table:

id | name | city
------------------
1  | n1   | c1
2  | n2   | c2
3  | n3   | c3
4  | n4   | c4

I want to check if the value c7 exists under the variable city or not.

If it does, I will do something.
If it doesn't, I will do something else.

Upvotes: 41

Views: 282937

Answers (10)

Reinder Wit
Reinder Wit

Reputation: 6615

using modern MySQLi:

$mysqli = new mysqli(SERVER, DBUSER, DBPASS, DATABASE);

$city = 'c7';
$result = $mysqli->execute_query("SELECT id FROM mytable WHERE city = ? LIMIT 1", [$city]);
if($result->num_rows == 1) {
    // found
}

using legacy mysqli

$mysqli = new mysqli(SERVER, DBUSER, DBPASS, DATABASE);

$city = 'c7';
$stmt = $mysqli->prepare("SELECT id FROM mytable WHERE city = ? LIMIT 1");
$stmt->bind_param("s", $city);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows == 1) {
    // found
}

using PDO:

$pdo = new PDO($dsn, $user, $pass, $options);

$city = 'c7';
$stmt = $pdo->prepare("SELECT id FROM mytable WHERE city = ? LIMIT 1");
$stmt->execute([$city]);
if($stmt->rowCount() == 1) {
    // found
}

Upvotes: 75

Adan Vivero
Adan Vivero

Reputation: 422

Here's is what worked with me if you're using PHP Version 8

$result = mysqli_query(database name, "SELECT id FROM mytable WHERE city = 'c7'");      
  
  if(mysqli_num_rows($result) == 0) {
       // Do Something 
  }
  else {
      // Do something else 
  }

The difference with my answer and Reindeer Wit is the 'i' in mysqli_query and mysqli_num_rows()

Also including the name of the database name inside of mysqli_query() that you are using.

Upvotes: -1

Gaurav Kumar
Gaurav Kumar

Reputation: 1

$result = mysqli_query($conn, "SELECT * FROM WHERE = ''"; $found = mysqli_num_rows($result) > 0 ? 'Yes' : 'no' ; echo $found;

Upvotes: -1

Mahesh Chikkanna
Mahesh Chikkanna

Reputation: 21

use

select count(city) from mytable where city = 'c7'

This will send only a single value from the query. If it is 0 it is not present else it is present. Since you will not be use the other column values.

Upvotes: 2

CrazyMind90
CrazyMind90

Reputation: 169

This works for me :


$db = mysqli_connect('localhost', 'UserName', 'Password', 'DB_Name') or die('Not Connected');
mysqli_set_charset($db, 'utf8');

$sql = mysqli_query($db,"SELECT * FROM `mytable` WHERE city='c7'");
$sql = mysqli_fetch_assoc($sql);
$Checker = $sql['city'];

if  ($Checker != null) {
    
    echo 'Already exists';
    
} else {

    echo 'Not found';
}

Upvotes: 2

Hassam Satti
Hassam Satti

Reputation: 1

For Matching the ID:

Select * from table_name where 1=1

For Matching the Pattern:

Select * from table_name column_name Like '%string%'

Upvotes: -1

Hari
Hari

Reputation: 23

Assuming the connection is established and is available in global scope;

//Check if a value exists in a table
function record_exists ($table, $column, $value) {
    global $connection;
    $query = "SELECT * FROM {$table} WHERE {$column} = {$value}";
    $result = mysql_query ( $query, $connection );
    if ( mysql_num_rows ( $result ) ) {
        return TRUE;
    } else {
        return FALSE;
    }
}

Usage: Assuming that the value to be checked is stored in the variable $username;

if (record_exists ( 'employee', 'username', $username )){
    echo "Username is not available. Try something else.";
} else {
    echo "Username is available";
}

Upvotes: 0

mushkincode
mushkincode

Reputation: 67

I tried to d this for a while and $sqlcommand = 'SELECT * FROM database WHERE search="'.$searchString.'";';
$sth = $db->prepare($sqlcommand); $sth->execute(); $record = $sth->fetch(); if ($sth->fetchColumn() > 0){}
just works if there are TWO identical entries, but, if you replace if ($sth->fetchColumn() > 0){} with if ($result){} it works with only one matching record, hope this helps.

Upvotes: 0

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

SELECT
    IF city='C7'
    THEN city
    ELSE 'somethingelse'
    END as `city`
FROM `table` WHERE `city` = 'c7'

Upvotes: 0

Blaster
Blaster

Reputation: 9080

For Exact Match

"SELECT * FROM yourTable WHERE city = 'c7'"

For Pattern / Wildcard Search

"SELECT * FROM yourTable WHERE city LIKE '%c7%'"

Of course you can change '%c7%' to '%c7' or 'c7%' depending on how you want to search it. For exact match, use first query example.

PHP

$result = mysql_query("SELECT * FROM yourTable WHERE city = 'c7'");
$matchFound = mysql_num_rows($result) > 0 ? 'yes' : 'no';
echo $matchFound;

You can also use if condition there.

Upvotes: 18

Related Questions