Page 1 of 1

If mysql row post exist

Posted: Tue Mar 28, 2006 12:56 pm
by Qaid
Hi,

How can I make a php scrip that checks to see if there exist a post in a mysql row.

Fx:
If ROW contains data then echo "Row contains data"
Else if ROW not contains data then echo "Row don't contain any data"

Hope you understand

Best Regards,
Qaid

Posted: Tue Mar 28, 2006 2:11 pm
by shiznatix

Code: Select all

$sql = 'SELECT * FROM table_name WHERE thing = "'.$var.'"';
$query = mysql_query($sql) or die(mysql_error());

if (mysql_num_rows($query) != 0)
    echo 'supra sweet';//rows found
else
    echo 'not very supra sweet, lets add some tang!';//no rows found

Posted: Tue Mar 28, 2006 4:17 pm
by Qaid
This won't work... This will post "'supra sweet".

And if the row is empty it will post nothing,

Example:
Row called data store this inside "data"
Supra$row[data]hurra :: then I will get this output: Supradatahurra

Row called data stored nothing inside ""
Supra$row[data]hurra :: then I will get this output: Suprahurra

It won't post the else statement...

Posted: Tue Mar 28, 2006 4:28 pm
by Christopher
I think you need to post your code.

Posted: Tue Mar 28, 2006 4:32 pm
by Red Blaze
This is something similiar that I needed and it worked fine with me.

It did that because you have to edit it to fit your needs.

if this might help you

Posted: Wed Mar 29, 2006 4:04 am
by dibyendrah
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have made the simple function just to check the data if already exist on table or not.

Code: Select all

/*
bool function if_exist( string table, string field, string data)
$table as database table 
$field as table field to which we compare agains given data
$data as passed data to function to match with given field of table
*/
function if_exist($table, $field, $data){
      $query = "select * from ". $table. " where ". $field . " = " . $data ;
      if(mysql_num_rows(mysql_query($query))>0){
             retun(true);
      }else{
      	return(false);
      }
}

Dibyendra


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Mar 31, 2006 11:29 am
by Qaid
Is it possible to check if a given data exist exactly as in the code...

Like this...

Code: Select all

$query = "SELECT * FROM table WHERE data = 'yes'"; 
if(@mysql_num_rows(mysql_query($query)) != yes){ 
echo "yes";
}else{ 
echo "No";
}
This won't work... :(

Posted: Fri Mar 31, 2006 11:36 am
by feyd
mysql_num_rows() will not return 'yes' ever.

Posted: Fri Mar 31, 2006 11:41 am
by Qaid
Do you have a solution to the problem..?

Posted: Fri Mar 31, 2006 11:44 am
by feyd
dibyendrah posted the rough concept.

Posted: Fri Mar 31, 2006 11:53 am
by Qaid
feyd wrote:mysql_num_rows() will not return 'yes' ever.

Code: Select all

/*
bool function if_exist( string table, string field, string data)
$table as database table 
$field as table field to which we compare agains given data
$data as passed data to function to match with given field of table
*/
function if_exist($table, $field, $data){
      $query = "select * from ". $table. " where ". $field . " = " . $data ;
      if(mysql_num_rows(mysql_query($query))>0){
             retun(true);
      }else{
          return(false);
      }
}
I'm not a shark, but you said that num_rows never could post "yes"?

Best Regards.

Posted: Fri Mar 31, 2006 12:04 pm
by feyd
what's the question?

Posted: Fri Mar 31, 2006 12:24 pm
by Qaid
Is it possible to check if a given data exist exactly as in the code...

Like this...

Code: Select all

$query = "SELECT * FROM table WHERE data = 'yes'"; 
if(@mysql_num_rows(mysql_query($query)) != yes){ 
echo "yes";
}else{ 
echo "No";
}

But with this code instead?

Code: Select all

/*
bool function if_exist( string table, string field, string data)
$table as database table 
$field as table field to which we compare agains given data
$data as passed data to function to match with given field of table
*/
function if_exist($table, $field, $data){
      $query = "select * from ". $table. " where ". $field . " = " . $data ;
      if(mysql_num_rows(mysql_query($query))>0){
             retun(true);
      }else{
          return(false);
      }
}

Posted: Fri Mar 31, 2006 12:36 pm
by feyd
they are roughly similar, although yours has logic flaws, while dibyendrah's has possible SQL syntax errors.

mysql_affected_rows();

Posted: Fri Mar 31, 2006 2:58 pm
by reynoso
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


i think this will help

Code: Select all

$resultado=mysql_query("select * from table where value = '".$value."' ";
$n=mysql_affected_rows(); 
if ($n > 0)
{
      //HAS DATA
}
else{
     //DOES NOT HAVE DATA
}

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]