+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: PHP/MySQL Query Help

  1. #1
    froger is offline x10Hosting Member froger is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    49

    PHP/MySQL Query Help

    Hello, I am currently facing a problem with a query of mine. You see, it wastes to much resources. I have an idea of how to fix it, but I just don't know how to implement it.

    I have a table named Test with 4 columns. X, Y, Z, textimage. The table has a whole bunch of rows with x, y, z co ordinates with a text image. Is there another alternative than to have to query the database so many time to just get the table rows text image and to be scalable with x, y, z?

    PHP Code:
    $sql mysql_fetch_array(mysql_query("SELECT * FROM Test WHERE X='1' AND Y='1' AND Z='1'"));

    for (
    $y=$sql['Y'];$y<=65;$y++)
      {
      for (
    $x=$sql['X'];$x<=100;$x++)
        {
        
    $query mysql_fetch_array(mysql_query("SELECT * FROM Test WHERE X='{$x}' AND Y='{$y}' AND Z='{$sql['Z']}'"));

        echo 
    $query['textimage'];
        }
      echo 
    '<br/>';
      } 


    *EDIT* I have gotten up to this point,

    PHP Code:
    $query mysql_query("SELECT * FROM Test WHERE (X BETWEEN 1 AND 100) AND (Y BETWEEN 1 AND 65) AND Z='1' ORDER BY Y, X");

    $y=1;

    while (
    $row mysql_fetch_array($query))
      {
      if (
    $row['Y'] == $y)
        {
        echo 
    $row['Image'];
        } else {
        echo 
    '<br/>';
        
    $y++;
        }
      } 
    What I need is 2 variables that will auto increment as the query runs.

    PHP Code:
    $query mysql_query("SELECT * FROM Test WHERE (X BETWEEN 1 AND 100) AND (Y BETWEEN 1 AND 65) AND Z='1' ORDER BY Y, X");

    $y=1;

    //auto increment variables
    $yi 0;
    $xi 0;

    while (
    $row mysql_fetch_array($query))
      {
      if (
    $row['Y'] == $y)
        {
        echo 
    $row['Image'];
        
    $xi++;
        } else {
        echo 
    '<br/>';
        
    $y++;
        
    $yi++;
        }
      } 
    But I just can't seem to get it right. For some reason, the above statement doesn't work as intended for me. Is there an easier way to do this?
    Last edited by froger; 08-15-2010 at 10:32 AM.

  2. #2
    dlukin is offline x10 Lieutenant dlukin is on a distinguished road
    Join Date
    Oct 2009
    Posts
    427

    Re: PHP/MySQL Query Help

    What are you trying to do?

    From what I can see, you are just getting all textimages for z=1 and 1<= x < = 100 and
    1 <= y <= 65, right?

  3. #3
    froger is offline x10Hosting Member froger is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    49

    Re: PHP/MySQL Query Help

    What I am trying to do is display all the text images in their coordinated spot. You see X goes all the way up to 100, and Y goes all the way up to 65.

    What that statement does is querys the database to get the textimage for each coordinate. So if X goes up to 100 and Y goes up to 65 then than is 100x65 queries to the database. You see how this can become pretty memory intensive? I would like to just query once to the database and use some type of method to display the data in a orderly fashion.
    Last edited by froger; 08-14-2010 at 09:41 PM.

  4. #4
    dlukin is offline x10 Lieutenant dlukin is on a distinguished road
    Join Date
    Oct 2009
    Posts
    427

    Re: PHP/MySQL Query Help

    $sql_query = "SELECT textimage FROM Test
    WHERE Z=1 AND X>=1 AND X<=100 AND Y>=1 AND Y<=65
    SORT BY Y ASC, X ASC"

    I think should work.

  5. #5
    froger is offline x10Hosting Member froger is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    49

    Re: PHP/MySQL Query Help

    Quote Originally Posted by dlukin View Post
    $sql_query = "SELECT textimage FROM Test
    WHERE Z=1 AND X>=1 AND X<=100 AND Y>=1 AND Y<=65
    SORT BY Y ASC, X ASC"

    I think should work.
    I was using something similar to this...

    PHP Code:
    $sql mysql_query("SELECT * FROM Test WHERE (X BETWEEN 1 AND 100) AND (Y BETWEEN 1 AND 65) AND Z='1' ORDER BY X");

    but for some reason when echoing the datait will order x something like this...

    111
    10
    11
    100
    11
    2
    11
    3
    1,1

    I don
    't know why it doesn'do it in orderMabe its because the column is set to varchar

    *EDIT* Yea, I set my columns to INT and everything started coming aligned. Thanks.
    Last edited by froger; 08-14-2010 at 10:25 PM.

  6. #6
    froger is offline x10Hosting Member froger is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    49

    Re: PHP/MySQL Query Help

    updated my original post.

  7. #7
    dlukin is offline x10 Lieutenant dlukin is on a distinguished road
    Join Date
    Oct 2009
    Posts
    427

    Re: PHP/MySQL Query Help

    What do you want to do?

    Explain it in words.

  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP/MySQL Query Help

    Quote Originally Posted by froger View Post
    updated my original post.
    Which makes it confusing, since you're making a new request by changing the original. On a site like SO, updating the original post is the correct action, but this is a forum; it's fine (rather, preferred) to write updates in new posts.

    Quote Originally Posted by froger View Post
    For some reason, the above statement doesn't work as intended for me.
    What happens instead, and how is it different than what you want? When describing a problem, state the behavior you expect/desire and what you get.

    To break up the result set into lines, you can check for when the y-value changes.
    PHP Code:
    <div>
      <div class="line">
        <?php
        $prevY 
    1;
        
    // $db is a PDO instance
        // DB access should be performed in a separate layer
        
    foreach ($db->query('SELECT x, y, z, textimage FROM test WHERE x BETWEEN 1 AND 100 AND y BETWEEN 1 AND 65 ORDER BY y, x') as $piece) {
          if (
    $prevY != $piece['y']) {
            
    ?>
            </div>
            <div class="line">
            <?php
          
    }
          echo 
    $piece['textimage'];
          
    $prevY $piece['y'];
        }
        
    ?>
      </div>
    </div>
    As noted in the comment, DB access should be isolated in a data access layer. The query would then become a prepared statement so that the ranges of x and y values can be altered.
    PHP Code:
    $imgQuery $db->prepare('SELECT x, y, z, textimage 
        FROM test 
        WHERE x BETWEEN :xmin AND :xmax 
          AND y BETWEEN :ymin AND :ymax 
        ORDER BY y, x'
    );
    $imgQuery->execute(array(':xmin' => 1':xmax' => 100':ymin' => 1':ymax' => 65 )); 
    What do the assembled textimage pieces make? A map? ASCII art?
    Last edited by misson; 08-15-2010 at 04:49 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  9. #9
    froger is offline x10Hosting Member froger is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    49

    Re: PHP/MySQL Query Help

    Quote Originally Posted by misson View Post
    1. What do the assembled textimage pieces make? A map? ASCII art?
    2. What happens instead, and how is it different than what you want?
    To answer question 1, yes the text images make up a ASCII art map.

    To answer question 2, you see, I can change the text into images with smaller images. When I use the imagecopymerge function the map seems seriously altered.

    *EDIT* I found the solution to my problem. Thanks.
    Last edited by froger; 08-15-2010 at 06:36 PM.

  10. #10
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP/MySQL Query Help

    Quote Originally Posted by froger View Post
    To answer question 1, yes the text images make up a ASCII art map.
    In that case, amend my earlier example to structure the data as pre-formatted content:
    PHP Code:
    <pre><?php
    $prevY 
    1;
    foreach (
    $db->query('SELECT x, y, z, textimage FROM test WHERE x BETWEEN 1 AND 100 AND y BETWEEN 1 AND 65 ORDER BY y, x') as $piece) {
        if (
    $prevY != $piece['y']) {
            echo 
    "\n";
        }
        echo 
    $piece['textimage'];
        
    $prevY $piece['y'];
    }
    ?></pre>
    All in all, it illustrates that storing two-dimensional data in a relational DB is, at best, problematic.

    Quote Originally Posted by froger View Post
    *EDIT* I found the solution to my problem. Thanks.
    What was the solution?
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Dealing with NULL in MySQL Query
    By lostcommander in forum Programming Help
    Replies: 2
    Last Post: 12-10-2009, 06:51 PM
  2. MySQL query sometimes worsks
    By wjh2303 in forum Programming Help
    Replies: 3
    Last Post: 04-06-2009, 11:32 AM
  3. MySQL Databases/SQL query How to?
    By Anfrany1 in forum Free Hosting
    Replies: 3
    Last Post: 11-04-2008, 10:28 PM
  4. mysql query question
    By garrettroyce in forum Programming Help
    Replies: 8
    Last Post: 05-13-2008, 12:29 AM
  5. MySQL query
    By BigEmmaFanz in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 05-23-2005, 11:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers