Jump to content

Little help with a length count & insert?


bnovak

Recommended Posts

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
Share on other sites

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
Share on other sites

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?  :shrug:

 

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.