JosephD
JosephD

Reputation: 321

MySQL Query is slowing down page load

I run this query

        $result1 = mysql_query("SELECT * FROM 'departments'");
        while($row1 = mysql_fetch_assoc($result1))
        {
            $depts = array("Name" => $row1['Name'], "Value" => $row1['Value'], "ID" => $row['CollegeID']);
        }

with three other simple queries, one loads data into profile text boxes, and another loads a drop down. Is something wrong with my Query here or my PHP code that could be causing this. It only returns about 51 records. Do you think it could be the server the MySQL server is running on a 2008 Windows Server.

Upvotes: 0

Views: 551

Answers (3)

Christo
Christo

Reputation: 41

You could also try using the OOP mysqli functions rather than the old mysql versions.

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$mysqli->query("SELECT Name, Value, CollegeID  FROM departments");

Upvotes: 1

genesis
genesis

Reputation: 50966

Never use *

$result1 = mysql_query("SELECT Name, Value, CollegeID  FROM 'departments'");
while($row1 = mysql_fetch_assoc($result1))
{
    $depts = array("Name" => $row1['Name'], "Value" => $row1['Value'], "ID" => $row['CollegeID']);
}

and use LIMIT

$result1 = mysql_query("SELECT Name, Value, CollegeID  FROM 'departments' LIMIT 1");
while($row1 = mysql_fetch_assoc($result1))
{
    $depts = array("Name" => $row1['Name'], "Value" => $row1['Value'], "ID" => $row['CollegeID']);
}

it doesn't matter in your case because you're always overwriting your actual $depts variable with new one row

Upvotes: 0

Naor
Naor

Reputation: 24053

Maybe you have many columns in departments table. Try this:

    $result1 = mysql_query("SELECT Name, Value, CollegeID  FROM 'departments'");
    while($row1 = mysql_fetch_assoc($result1))
    {
        $depts = array("Name" => $row1['Name'], "Value" => $row1['Value'], "ID" => $row['CollegeID']);
    }

Upvotes: 2

Related Questions