bnovak Posted May 18, 2010 Share Posted May 18, 2010 This should be really easy, but I've been struggling with these 5 lines of code all day. Can someone else give it a glance over? Little overhead info - pretty simple database, just need to count the number of " | " occurences in a particular column CELL (not the entire column, just each cell per user ID) and add 1 to that number, then insert it into an empty table column. precursors - in phpmyadmin I can get the LENGHT(stateLicenses)-(LENGTH(replace(stateLicenses, '|',""); to return the correct value. $con = mysql_connect("localhost","XXXX","XXXX"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("XXXX", $con); $count = mysql_query("SELECT LENGTH((stateLicenses) - LENGTH(REPLACE(stateLicenses, '|', '')) FROM profile WHERE statecount="empty""); // only want to insert it into profile ID's that have "empty" (column initial value) $count++; echo $count; // just in for test to see if the above is working. mysql_query("UPDATE profile SET statecount='$count' WHERE statecount=''empty"); I'm totally new to this stuff so if it's a glaring mistake, let me down easy :'( When I ran just the mySQL query above in phpmyadmin it set all of my statecount's to "0" - when I run it in my web program it returns a syntax error. The cell information looks like "Alaska AK| Alabama AL|....Wyoming WY|" being generated from a checkbox on a form. Any ideas? Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/202212-little-help-with-a-length-count-insert/ Share on other sites More sharing options...
JAY6390 Posted May 18, 2010 Share Posted May 18, 2010 This will fix your parse errors $con = mysql_connect("localhost", "XXXX", "XXXX"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("XXXX", $con); $count = mysql_query("SELECT LENGTH((stateLicenses) - LENGTH(REPLACE(stateLicenses, '|', '')) FROM profile WHERE statecount='empty'"); // only want to insert it into profile ID's that have "empty" (column initial value) $count++; echo $count; // just in for test to see if the above is working. mysql_query("UPDATE profile SET statecount='$count' WHERE statecount='empty'"); Link to comment https://forums.phpfreaks.com/topic/202212-little-help-with-a-length-count-insert/#findComment-1060317 Share on other sites More sharing options...
bnovak Posted May 19, 2010 Author Share Posted May 19, 2010 Jay - thanks for the help. Yes, it did get rid of the syntax error I was getting, but... Any chance of getting a hint as to why I'm getting a column of blank values now in the statecount field? Does this need to be encapsulated into an array based on the id of the row that it's in? I thought that MySQL was smart enough to go row - by - row when making these type's of query's. Thanks again. BN Link to comment https://forums.phpfreaks.com/topic/202212-little-help-with-a-length-count-insert/#findComment-1060357 Share on other sites More sharing options...
bnovak Posted May 19, 2010 Author Share Posted May 19, 2010 BTT for the morning crowd. Link to comment https://forums.phpfreaks.com/topic/202212-little-help-with-a-length-count-insert/#findComment-1060610 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.