• 0

[PHP] Breaks site


Question

When I include the following bit of code on a page my site seems to break. It doesnt get past this part of the code and also I cannot load any other part of the site... This is until I close the browser and open it again, however the same problem occurs when I run this script. Any ideas why this is so? This piece of code was working fine though like once or twice without the problems mentioned.

<?php
$ip=$_SERVER['REMOTE_ADDR'];
$query=mysql_query("SELECT * FROM votes WHERE voter='$ip'") or die(mysql_error());
$query_total=mysql_query("SELECT * FROM polls") or die(mysql_error());
$num_of_polls=mysql_num_rows($query_total);

	$selected_poll=mt_rand(1,$num_of_polls);
	$x=1;
		while($row=mysql_fetch_array($query))
	{
		$voted_poll[$x]=$row['poll_id'];
		$x+=1;
	}
	$loop="1";
		while(isset($loop))
	{	
			if(in_array($selected_poll,$voted_poll))
		{
			$selected_poll=mt_rand(1,$num_of_polls);
		}
			else
		{
			unset($loop);
		}
	}
	$query=mysql_query("SELECT * FROM polls WHERE id='$selected_poll'")or die(mysql_error());
	$row=mysql_fetch_array($query);
	echo $row['question'];

?>

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

if i'm reading your code correctly then you could do this all in one sql query.

SELECT P.question FROM polls P INNER JOIN votes V ON P.id = V.poll_id WHERE V.voter = '$ip' ORDER BY RAND() LIMIT 1

Link to comment
Share on other sites

  • 0

Do you have code above this that actually connects to a MySQL database?

function mysql_connect_tp()
{
	mysql_connect('localhost', 'username', 'pw') or die(mysql_error());
	mysql_select_db('tehplane_census') or die(mysql_error());
}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.