Jump to content

labiere

New Members
  • Posts

    6
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

labiere's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Well I finally found the source of the problem.. It had to do with the order of operators in my WHILE condition: while ($row = mysql_fetch_array($result, MYSQL_NUM) && $p<3) { was bracketed and changed to this: while (($row = mysql_fetch_array($result, MYSQL_NUM)) && ($p<3)) {
  2. The number of rows has worked fine so far. In my actual code I have it peppered with echo statements including a count of num_rows. I have checked the number of rows of $result in the line just before entering the final WHILE loop, and am seeing the correct numbers there.
  3. I've been making some simple queries to a mysql table from within a PHP script. Everything works as expected the first time I loop and use the mysql_fetch_array function. Once I break out of the loop and start a new loop, the function no longer appears to be iterating from where it left off.. or at all.. It's not like I just end up getting the wrong row back from the loop, Suddenly my variable $row seems to have nothing assigned to it at all. <?php // Connecting, selecting database $link = mysql_connect('mysql.domain.com', 'user', 'password') or die('Could not connect: ' . mysql_error()); mysql_select_db('db_trial') or die('Could not select database'); // Grab a subset of rows that match an expression. This works. $query = "SELECT * FROM products WHERE item REGEXP '^($collection)[0-9]'"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); // Iterate through the subset of rows until some condition is met... This works. $i=0; do { $row = mysql_fetch_array($result, MYSQL_NUM); $i++; } while ($i != 4 * ($page - 1) + 1); //Now that we have the correct row, use its values to create some variables.. This works $p = 0; $product_thumb[$p] = './images/'.$row[0].'.jpg'; // echo output is "./images/xxy32.jpg" $product_name[$p] = $row[1]; // echo output is "WEED WHACKER" //Now I want to continue iterating through the subset and create some more variables. This does not work!! while ($row = mysql_fetch_array($result, MYSQL_NUM) && $p<3) { $p++; $product_thumb[$p] = './images/'.$row[0].'.jpg'; $product_name[$p] = $row[1]; echo $product_thumb[$p]; // output is "./images/.jpg" echo $product_name[$p]; //nothing is output } // Free resultset mysql_free_result($result); // Closing connection mysql_close($link); ?> Can anyone explain what is going on here?
  4. I am trying to select a subset of rows from a table based on a text pattern. My table column, labeled "id", has entries like this: xx2 xx38 xxy2 (These items are always made up of 1 or more letters followed by one or more numbers) I have defined a PHP variable $prefix="xx" I want to perform a query that will select the two "xx" rows, but not the "xxy" row. When I try the query like this, I get all three rows back, as I would expect: // Performing SQL query $query2 = "SELECT * FROM table WHERE id REGEXP '^$prefix'"; I thought this change would give me rows that start with "xx" immediately followed by a digit: // Performing SQL query $query2 = "SELECT * FROM table WHERE id REGEXP '^$prefix[0-9]'"; .. What I get with that change is an error message: Parse error: syntax error, unexpected '-', expecting ']' Been banging my head over it for a while now.. Any ideas?
  5. Can you point me to a topic thread or URL on this subject? Fortunately there is nothing sensitive in my DB on this site, but I would like to learn the best methods from the start.
  6. Noob here. I haven't been able to find an example of this syntax, although it seems like it ought to be a common situation. I am passing a parameter to my page in the URL via a query string http://www.labiere.com?drink=beer I parse the parameter using $_GET: $drink = $_GET['drink']; Now I want to make a query to my SQL table where my condition is to grab the row that has $drink as the index. // Incorrect syntax $query = "SELECT * FROM table WHERE beverage=$drink"; Is this a syntax error, or do I instead have to select each row and check its value against $drink in a looped if statement?
×
×
  • 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.