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'];  
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.