www.webdeveloper.com
Results 1 to 8 of 8

Thread: Is this syntax correct?

  1. #1
    Join Date
    Mar 2010
    Posts
    701

    Is this syntax correct?

    PHP Code:
    $num 1;
    while(
    mysql_query("SELECT post_title FROM wp_posts WHERE post_title='$title'")){
    preg_replace('/\d$/','',$title,1);
    $title $title $num;
    $num $num 1;

    I always get
    Code:
    Fatal error: Maximum execution time of 30 seconds exceeded in...
    Last edited by narutodude000; 07-12-2010 at 10:45 AM.

  2. #2
    Join Date
    Mar 2010
    Posts
    672
    Well, technically the syntax is correct, but the use of the functions are not. You're basing your while loop on a query... But unless i am misunderstanding your code, you should be basing your while loop on the query results. Assuming the query is running correctly, it will always return true, and so you end up with an infinite loop, hence why the max execution time is being reached.

    Edit: after looking at the query more, i am not sure what your code is trying to achieve. If your query is pulling a post_title from the post_name (which should be unique in wordpress), your query should only ever be returning one row, so it wouldn't make any sense to base a while loop off of it.
    Last edited by Jarrod1937; 07-12-2010 at 10:32 AM.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    See the manpage for mysql_fetch_assoc() for an example of how/when to use the while loop for a DB query.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Mar 2010
    Posts
    701
    How's this?
    PHP Code:
    $num 1;
    while(
    1){
    $result mysql_query("SELECT post_name FROM wp_posts WHERE post_name='$slug'");
    if(
    $row mysql_fetch_assoc($result)){
    preg_replace('/\d$/','',$slug,1);
    $slug $slug $num;
    $num $num 1;
    }
    else
    {
    break;
    }

    Last edited by narutodude000; 07-12-2010 at 03:34 PM.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    If I correctly interpreted what you want to do, I think this would be easier an a bit more efficient:
    PHP Code:
    $num 1;
    $result mysql_query("SELECT COUNT(*) FROM wp_posts WHERE post_name='$slug'");
    if((
    $rows mysql_result($result0)) > 0) {
        
    $slug .= (string) ($rows 1); // (string) probably isn't really needed

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Mar 2010
    Posts
    701
    I need a while loop because I'm trying to create unique slugs (URL friendly string). If the slug already exists, then an incrementing number can be added to the end of the slug to make it unique.

    PHP Code:
    $num 2;
    $result mysql_query("SELECT post_name FROM wp_posts WHERE post_name='$slug'");
    while(
    $row mysql_fetch_assoc($result)){
    preg_replace('/\d$/','',$slug,1);
    $slug $slug $num;
    $num $num 1;


  7. #7
    Join Date
    Jan 2009
    Posts
    3,346
    If that is the case perhaps you mean to use
    Code:
    LIKE '$slug%'
    so you pull all existing slugs with the same name and a possibly appended number?

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    My previous response was using the SQL COUNT() function to get the count of matches, instead of having to loop through all the results in the PHP code in order to do the same thing: count the matches.

    In actuality, though, I suspect I would concatenate the DB row's ID (assuming it's an auto-increment integer) rather than go through these contortions, which might have unforeseen side effects if a record gets deleted. In addition, that would remove the query and related processing, adding some efficiency to the process.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles