Jump to content

[SOLVED] is this an efficient sql selection?


scarhand

Recommended Posts

I have these selections in 1 file that the user is able to configure themselves via a form in their control panel....I've got it all working but I was wondering if this is a good way of doing it or not...

 

$adminsql = mysql_query("SELECT * FROM admin_user");
while($row = mysql_fetch_array($adminsql))
{
  $adminusername = $row['username']; 
  $adminpassword = $row['password']; 
}

$bandnamesql = mysql_query("SELECT * FROM config WHERE config_name = 'bandname'");
while($row = mysql_fetch_array($bandnamesql))
{
  $bandname = $row['config_value'];  
}

$copyrightsql = mysql_query("SELECT * FROM config WHERE config_name = 'copyright'");
while($row = mysql_fetch_array($copyrightsql))
{
  $copyright = $row['config_value'];  
}

well, it's ok but you may try to use

 

SELECT * FROM config WHERE config_name = 'bandname' OR config_name = 'copyright'

 

Instead of two queries which will increase your performance a little bit.

 

By the way you should had tried mysql section of this forum not php help section because this topic fits better there.

Things I did to make it more efficent:

- 1 less MySQL query

- No "SELECT *"s. Sepcified only the column names needed.

- mysql_fetch_assoc instead of mysql_fetch_array. You aren't using column numbers as keys.

 

<?php
$adminsql = mysql_query('SELECT username, password FROM admin_user');
while($row = mysql_fetch_assoc($adminsql))
{
$adminusername = $row['username']; 
$adminpassword = $row['password']; 
}


$bandnamesql = mysql_query("SELECT config_value FROM config WHERE config_name = 'bandname'" .
" OR config_name = 'copyright'");
while($row = mysql_fetch_assoc($bandnamesql))
{
switch($row['config_value']) {
	case "bandname":
		$bandname = $row['config_value'];  
		break;
	case "copyright":
		$copyright = $row['config_value'];  
		break;
}
}

 

You probably don't need that first while. But I wasn't sure if there were multiple results for that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.