Jump to content

Checking MySQL


Timma

Recommended Posts

How would I write a function that returns true if it finds a matching entry in the table. For example:

In table tab, column col, there are multiple different entries but a matching entry may be about to be inserted, how can I check if it's already there?

 

I also need it to return true or false for the purpose of an if statement. If it is already in the database it should return true.

 

function check(){
} 

My code so far, yes pathetic.

Link to comment
https://forums.phpfreaks.com/topic/44310-checking-mysql/
Share on other sites

<?php
function check($field, $table, $string)
{
     $check = mysql_query("SELECT {$field} FROM {$table} WHERE {$field}='{$string}' LIMIT 1");
     $result = mysql_num_rows($check);

     return $result;
}

$field = "Field to check in";
$table = "Table to check in";
$string = "String to check for in db";

$check_row = check($field, $table, $string);

if ($check_row)
{
     // Insert new row
}
?>

Link to comment
https://forums.phpfreaks.com/topic/44310-checking-mysql/#findComment-215226
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.