Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my PHP query function not returning a result set when querying in a loop?
    text
    copied!<p>I have a database class that creates a database instance upon page load which in itself creates a database connection as such:</p> <pre><code>class database{ var $connection; /* Class constructor */ function database(){ /* Make connection to database */ $this-&gt;connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error()); mysql_select_db(DB_NAME, $this-&gt;connection) or die(mysql_error()); </code></pre> <p>Within this class I have a function that queries the database using the above connection like this:</p> <pre><code>function query($query){ return mysql_query($query, $this-&gt;connection); } </code></pre> <p>This works properly if I use this query function once in my php, but the second I put this query function call into a loop all but the first instance of the query function call return a null result set. Here is an example of the loop code that is not working:</p> <pre><code>$chartSQL = "CALL RESEARCH.INTRA_DAY_PRICE('".$row['TICKER_I']."', 7)"; $result = $database-&gt;query($chartSQL); </code></pre> <p>Could this have something to do with the fact that I am querying a MySQL routine and not a simple select statement? Note that the routine "INTRA_DAY_PRICE" is a select statement using the supplied argument. Could it maybe be that I am using the same connection in a loop and the connection is simply in use during each consecutive call, thus the null result set?</p> <p>Edit: Here is the whole loop:</p> <pre><code>$search_term = sr_addslashes($_GET['search']); $sql = "SELECT DISTINCT t.TICKER_I, t.TICKER_C, t.TICKER_N, cpv.CURRENT_PRICE_A, cpv.MARKET_CAP, cpv.DA\ YS_CHANGE_A FROM ".DB_NAME.".TICKER_MyISAM t INNER JOIN ".DB_NAME.".CURRENT_PRICE_V cpv ON t.TICKER_I = cpv.TICKER\ _I WHERE t.INTEREST_F = 1 AND (MATCH(t.TICKER_C, t.TICKER_N) AGAINST('".$search_term."' IN BOOLEAN MODE) OR t.TICK\ ER_C = '".$search_term."' OR t.TICKER_N = '".$search_term."') LIMIT 20"; $results = $database-&gt;query($sql); while($row = mysql_fetch_array($results)){ //Chart &lt;script type="text/javascript" src="http://www.google.com/jsapi"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; google.load('visualization', '1', {packages: ['annotatedtimeline']}); function drawVisualization() { var data = new google.visualization.DataTable(); data.addColumn('date', 'Date'); data.addColumn('number', '&lt;?php echo $row['TICKER_C']; ?&gt;'); data.addRows([ &lt;?php $chartSQL = "CALL RESEARCH.INTRA_DAY_PRICE('".$row['TICKER_I']."', 7)"; $result = $database-&gt;query($chartSQL); while($chartRow = mysql_fetch_array($result)){ echo $chartRow['FORMATTED']; } echo "null]);"; ?&gt; var annotatedtimeline = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_d\ iv_&lt;?php echo $count; ?&gt;')); annotatedtimeline.draw(data, {'scaleType': 'maximized', 'displayRangeSelector': false, 'displayZoom\ Buttons': false}); } google.setOnLoadCallback(drawVisualization); &lt;/script&gt; &lt;div id="chart_div_&lt;?php echo $count; ?&gt;" class="chart_div"&gt;&lt;/div&gt; &lt;?php // End Chart } </code></pre>
 

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