Note that there are some explanatory texts on larger screens.

plurals
  1. POLocal Apache server crashes when using mysql_fetch_array() and foreach() to display results
    text
    copied!<p>So, basically what I'm trying to do is to display search hits on a page. I have set up a form which posts to the page below, this data is then searched for in my MySQL database and the results should be displayed in the following format:</p> <p><hr/> <br/> Name <br/> Profile Image Path <br/> Age <br/><br/> <hr/> <br/></p> <p>I searched on the internet extensively and found out that you have to use a while loop, as I was just using a foreach loop before.</p> <p>But my apache server crashes whenever I search for something. I think it could be because of the while loop as it didn't crash before but it would only display the <strong>same</strong> set of results (Name, Image and Age) a certain amount of times, depending on the amount of rows found in the database.</p> <p>Here's the search page code:</p> <pre><code>require_once "lib/mysql.php"; </code></pre> <p>if (isset($_POST[search]) &amp;&amp; !empty($_POST[search])){</p> <pre><code>$search = $_POST[search]; // The mysql class used for functions. $mysql = new mysqlHandle; // Opens access to the mysql server. $mysql-&gt;accessOpen('root', '', 'Portfolio', 'localhost'); // Sets $rows to number of entries (rows) with the 'Name' value of $search. $rows = $mysql-&gt;tableCheckNum('*', stats, Name, $search, Portfolio); echo "&lt;div id='content'&gt; &lt;div id='content_main'&gt; &lt;h2&gt;Search Results for '$search'&lt;/h2&gt; &lt;br/&gt;&lt;hr/&gt;&lt;br/&gt;"; // Makes sure that there's at least 1 search hit. if ($rows&gt;0){ // Sets the current hit number. (the first one is 1, the second is 2 et.c) $num = 1; $user = array(); // My mysql function for checking the order of entries, see the attached myqsl code. while ($result = $mysql-&gt;tableCheckOrder('*', stats, Name, $search, ID, DESC, Portfolio)){ // The $user array should hold the user's (searched for) details. $user[] = $result; foreach ($user as $result){ // Makes sure that we haven't exceeded the number of rows. if ($num&lt;=$rows){ // Adds one to the number of displayed rows, as we have dsplayed another. $num++; // Prints the user's (searched for) details. print_r ($user[Name]."&lt;br/&gt;"); print_r ($user[Image]."&lt;br/&gt;"); print_r ($user[Age]."&lt;br/&gt;"."&lt;br/&gt;"."&lt;hr/&gt;"."&lt;br/&gt;"); } } } }else{ echo "No users found"; } echo '&lt;/div&gt; &lt;/div&gt;'; </code></pre> <p>}</p> <p>Here's the applicable MySQL code:</p> <pre><code>// Gets order of entries (rows) The indentations under "function tableCheckOrder" isn't showing up btw. function tableCheckOrder($span, $table, $column, $value, $order, $mode, $base){ mysql_select_db($base); $this-&gt;query11="SELECT $span FROM $table WHERE $column = '$value' ORDER BY $order $mode"; $this-&gt;result11=mysql_query($this-&gt;query11) or die("&lt;br/&gt;"."Invalid $table CHECK ORDER query: " .mysql_error()); return mysql_fetch_array($this-&gt;result11, $result_type = MYSQL_BOTH); } // This function checks amount of rows in table. function tableCheckNum($span, $table, $column, $value, $base){ mysql_select_db($base); $this-&gt;query="SELECT $span FROM $table WHERE $column = '$value'"; $this-&gt;result=mysql_query($this-&gt;query) or die("Invalid $table CHECK NUM query: " .mysql_error()); return mysql_num_rows($this-&gt;result); } </code></pre> <p>Here's a pic of my 'stats' table structure:</p> <p><img src="https://i.stack.imgur.com/IKm3C.png" alt="My &#39;stats&#39; table structure"></p> <p>I really want to be able to display the search results without it crashing</p> <p>If I can give you any more information, please ask!</p> <p>Thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload