If mysql row post exist

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Qaid
Forum Commoner
Posts: 33
Joined: Wed Feb 15, 2006 12:04 pm

If mysql row post exist

Post 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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
Qaid
Forum Commoner
Posts: 33
Joined: Wed Feb 15, 2006 12:04 pm

Post 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...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I think you need to post your code.
(#10850)
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post 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.
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

if this might help you

Post 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]
Qaid
Forum Commoner
Posts: 33
Joined: Wed Feb 15, 2006 12:04 pm

Post 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... :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_num_rows() will not return 'yes' ever.
Qaid
Forum Commoner
Posts: 33
Joined: Wed Feb 15, 2006 12:04 pm

Post by Qaid »

Do you have a solution to the problem..?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

dibyendrah posted the rough concept.
Qaid
Forum Commoner
Posts: 33
Joined: Wed Feb 15, 2006 12:04 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's the question?
Qaid
Forum Commoner
Posts: 33
Joined: Wed Feb 15, 2006 12:04 pm

Post 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);
      }
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

they are roughly similar, although yours has logic flaws, while dibyendrah's has possible SQL syntax errors.
reynoso
Forum Newbie
Posts: 2
Joined: Fri Mar 31, 2006 2:23 pm

mysql_affected_rows();

Post 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]
Post Reply