Re: Empty query result

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tuesday 27 July 2004 00:57, Philip Thompson wrote:

> Here's my function:
>
> function getMysqlSelectResultForUsername($identifier, $userName, $link)
> {
>      $queryResult = mysql_query("select $identifier from theTable where
> username = '$userName'", $link);
>
>      if (!($queryResult = mysql_result($queryResult, 0))) {
>          session_register('_selectIdentifierFromTheTable');
>          header("location:error.php");
>          exit;
>      }
>
>      return $queryResult;
> }
>
> Here's the code that calls it:
>
> $hall = getMysqlSelectResultForUsername('buildingID', $_userName,
> $link);
> $roomNum = getMysqlSelectResultForUsername('roomNum', $_userName,
> $link);
> $phone1 = getMysqlSelectResultForUsername('phone1', $_userName, $link);
> $phone2 = getMysqlSelectResultForUsername('phone2', $_userName, $link);
> $lastLogin = getMysqlSelectResultForUsername('lastLogin', $_userName,
> $link);

What you're doing is extremely inefficient. You could do in a single query 
what you're currently doing in 5 queries. Use a query like:

  SELECT col1, col2, col3, coln, FROM table WHERE colx = 'something'

Then use mysql_fetch_row() or siblings to get the column contents.

> If the mysql_result($queryResult, 0) returns zero/nothing, then it goes
> to the 'error.php' page. However, I just want it to move on. For
> example, if there is no 'phone2' given in the database, I want it to
> just assign $phone2 to zero/nothing, not go to the error page.

Well you wrote the code so that it will go to the error page.

-- 
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
------------------------------------------
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-db
------------------------------------------
/*
If little green men land in your back yard, hide any little green women
you've got in the house.
		-- Mike Harding, "The Armchair Anarchist's Almanac"
*/

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux