Jump to content


Photo

sql query question


  • Please log in to reply
3 replies to this topic

#1 blacknight

blacknight
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • Locationsaint john

Posted 12 October 2006 - 10:24 PM

im tryingt to make an upgrade script for a database and need to see if spacific colums are present in the table i have tryed

$sql_str = "SHOW COLUMNS FROM table_name LIKE 'approved'";
	$this->setSqlDebug("checkcolumDb: $sql_str");
      $result = $wowdb->query($sql_str);
      if ($wowdb->num_rows($result) == 0){
      echo "no colum";
      $t++;
      
      }
but no column is allways echoed if it exists or not i need tit to not return if it does any one have any help?
if at first your fail google will give you a pass

#2 blacknight

blacknight
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • Locationsaint john

Posted 13 October 2006 - 02:16 PM

?
if at first your fail google will give you a pass

#3 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 13 October 2006 - 02:18 PM

$id = 3;
$select = "SELECT * FROM userinfo WHERE id = '$id';";
$query = mysql_query($select);
if ($row = mysql_fetch_array($query)) {
echo "I apologize but that record already exists";
}else {
// insert, or update here
}

Is this what you mean.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 13 October 2006 - 02:55 PM

Here, this should work...

<?php
$MyTableName = "username"; //Your table name goes here
$sql = "SHOW COLUMNS FROM table_name WHERE Field = '$MyTableName'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);

if ($count > 0){
   echo "The column name already exists";
}
else {
   echo "There is no column with that name";
}
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users