Hi,

I am hoping someone can explain to me a better way of coding than I currently do right now. It concerns accessing the results of a SQL query stored in an array.

OK so right now I do somthing similar to this:

$rows = @mysql_query("select count(*) from debate_queue") or die(mysql_error() );
$queue = mysql_fetch_array($rows);
$total = @mysql_query("SELECT count(*) FROM debate") or die(mysql_error() ); 
$realtotal = mysql_fetch_array($total);

<h3>To date there are <?php echo '<span class="highlight"> ' . $realtotal[0] . '</span>';  ?>  points in the debate and there are <?php echo '<span class="highlight"> ' . $queue[0] . '</span>'; ?> people in the que waiting to post a point.</h3>

The only thing is I can't help but think there is a better more efficient way to do things. It annoys me I have to create two variables for every bit of info I need from a SQL query. Plus it is going to get even more annoying as I have to now output various details of the user.

I was thinking about using something like this to try and code it better but it doesn't work:

$details = array();
$details['all'] = @mysql_query("SELECT * FROM debate");
$details['id'] = mysql_fetch_array($details['all']['0']);
				
echo "<h2>$details[id]</h2>";

Anyone out there help a guy write better code?


Thanks for looking.

you could write a function:

function q($conn,$query) {
$r = @mysql_query(mysql_real_escape_string($query,$conn));
if(!$r) {
throw new Exception(mysql_error($conn));
}
return $r;
}

and catch the exception in the caller

Hi,
Can u try this,

$details = array();
$details['deb_queue_cnt'] = @mysql_result(mysql_query("select count(*) from debate_queue")) or die(mysql_error() );

$details['deb_cnt'] = @mysql_result(@mysql_query("SELECT count(*) FROM debate")) or die(mysql_error() );

Please revert back to me ur opinion on my comments

Hi,

I am hoping someone can explain to me a better way of coding than I currently do right now. It concerns accessing the results of a SQL query stored in an array.

OK so right now I do somthing similar to this:

$rows = @mysql_query("select count(*) from debate_queue") or die(mysql_error() );
$queue = mysql_fetch_array($rows);
$total = @mysql_query("SELECT count(*) FROM debate") or die(mysql_error() ); 
$realtotal = mysql_fetch_array($total);

<h3>To date there are <?php echo '<span class="highlight"> ' . $realtotal[0] . '</span>';  ?>  points in the debate and there are <?php echo '<span class="highlight"> ' . $queue[0] . '</span>'; ?> people in the que waiting to post a point.</h3>

The only thing is I can't help but think there is a better more efficient way to do things. It annoys me I have to create two variables for every bit of info I need from a SQL query. Plus it is going to get even more annoying as I have to now output various details of the user.

I was thinking about using something like this to try and code it better but it doesn't work:

$details = array();
$details['all'] = @mysql_query("SELECT * FROM debate");
$details['id'] = mysql_fetch_array($details['all']['0']);
				
echo "<h2>$details[id]</h2>";

Anyone out there help a guy write better code?


Thanks for looking.

Hi VinothKumar C,

Thanks for the suggestion. Sorry I haven't got back sooner, I was busy at Uni.

OK so I tried the code you suggested but am getting the following error;

Warning: Wrong parameter count for mysql_result() in /home/richardj/public_html/thinksane/debate.php on line 25

The only thing I have changed was to remove the @ symbol so that the error messages show up.

The code I have now is the following;

$details = array();
      		$details['deb_queue_cnt'] = mysql_result(mysql_query("SELECT count(*) FROM debate_queue")) or die(mysql_error() );
      		$details['deb_cnt'] = mysql_result(mysql_query("SELECT count(*) FROM debate")) or die(mysql_error() ); 
			
			echo $details['deb_queue_cnt'];
			echo $details['deb_cnt'];
			
			echo "<h2>debate queue count = " . $details['deb_queue_cnt'] . " <br />
						debate count = " . $details['deb_cnt'] . "</h2>";

Any further help you can give me on this would be great. Also I'm not sure why using the PHP functions you have in the above code would be different. Could you explain a bit to let me know why you have chose them. It's just I like to know what code is for.

Thanks very much.

O and just in case it helps you to see the message youself the page can be found at;

http://thinksane.net/debate.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.