[thelist] PHP/MySQL odd problem

Burhan Khalid thelist at meidomus.com
Sat Jul 10 10:12:52 CDT 2004


Mr. Tenuta wrote:
>>If you already have the query result in a variable, why do you need to
> 
> execute the >query again a second time on the same page? Can't you just
> reuse the variable you >already have?
> 
> Yes I could re-use the same query twice, but there are differences between
> the two in the WHERE clause. One i mention freeroll_main and the other
> freeroll_list. Whether those categories exist or not there shouldn't be an
> error. Let me give you an example of what happening here with the full loop.
> 
> // QUERY AND OUTPUT1
> // Get query
> $res = mysql_query("SELECT * FROM freeroll_menu WHERE category =
> 'freeroll_main'", $link) or die("Select Failed: " . mysql_error());
> 
> // Iteration loop, for each row in rowset
> while($row = mysql_fetch_assoc($res))
> {
>  // Assigning variables from cell values to later output
>  $category = $row["category"];
>  $link = $row["link"];
>  $link_name = $row["link_name"];
>  $order_num = $row["order_num"];
> 
>  // Output the info
>  echo "<br>Category: $category<br>Link Name: $link_name<br>Link URL:
> $link<br>Order: $order_num<br><br>";
> }// end while
> 
> ... here's the second
> 
> // QUERY AND OUTPUT2
> // Get query
> $res = mysql_query("SELECT * FROM freeroll_menu WHERE category =
> 'freeroll_list'", $link) or die("Select Failed: " . mysql_error());
> 
> // Iteration loop, for each row in rowset
> while($row = mysql_fetch_assoc($res))
> {
>  // Assigning variables from cell values to later output
>  $category = $row["category"];
>  $link = $row["link"];

Here you are reassigning $link, which makes it invalid for your second 
query. That's why only one works.


More information about the thelist mailing list