Minor MySQL Issue

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

User avatar
Devnull
Forum Commoner
Posts: 52
Joined: Fri Oct 22, 2004 2:19 pm

Minor MySQL Issue

Post by Devnull »

I've got a function with the following code:

Code: Select all

function login($user, $pass)
{
   // connect
   $conn = db_connect();
   if (!$conn)
      return false;
   
   // search for the user, ** note the 'select user_id' **
   $result = mysql_query("select id from user
                           where user = '$user'
                           and pass = MD5('$pass')");
  
   // found a user?
   if (!result)
   {
      return false;
   }
   if (mysql_num_rows($result)>0)
   {
      // store the user id
      $_SESSION['user_id'] = mysql_result($conn, 0, 'user_id');
      return true;
   }
   else
   {
      return false;
   }
}
I'm getting the following error on Line 45, and I don't know what is the problem:
Warning: mysql_result(): supplied resource is not a valid MySQL result resource in /home/virtual/site117/fst/var/www/html/portfolio/telereport/includes/global.php on line 45
This is line 45:

Code: Select all

$_SESSION['user_id'] = mysql_result($conn, 0, 'user_id');
Last edited by Devnull on Mon Aug 08, 2005 7:28 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

SELECT id

or

SELECT user_id

Your code uses user_id, but your query is id
User avatar
Devnull
Forum Commoner
Posts: 52
Joined: Fri Oct 22, 2004 2:19 pm

Post by Devnull »

I replaced line 45 with the following:

Code: Select all

$_SESSION['id'] = mysql_result($conn, 0, 'id');
I still seem to be getting the same error:
Warning: mysql_result(): supplied resource is not a valid MySQL result resource in /home/virtual/site117/fst/var/www/html/portfolio/telereport/includes/global.php on line 45
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you likely have an error in your query or something.. check what mysql_error() says.
User avatar
Devnull
Forum Commoner
Posts: 52
Joined: Fri Oct 22, 2004 2:19 pm

Post by Devnull »

I already tried that before, I don't get an error message, all I get is warning message I quoted above.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hmm... $conn doesn't have much to do with mysql_result(). You want $result. :?
User avatar
Devnull
Forum Commoner
Posts: 52
Joined: Fri Oct 22, 2004 2:19 pm

Post by Devnull »

Thanks, that sorted it out, but now I'm having another problem :(, I got the following function:

Code: Select all

function get_area() {
   $conn = db_connect();
   if (!$conn)
      return false;
    
    mysql_query("SELECT * FROM `user` WHERE `id` = " . (int)$_SESSION['id'] . " AND `area` = '$area' LIMIT 25");

// added a $ before result here
   if (!$result)
      return false;
   if (mysql_num_rows($result)>0)
      return true;
   else
      return false;

// here's a line to extract the row from the db
$array = mysql_fetch_assoc($result);

// store the area in $area
$area = $array['area'];
}
When I try the following code it doesn't output anything.

Code: Select all

<?php echo "$area"; ?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're not storing $result from the query involved..
User avatar
Devnull
Forum Commoner
Posts: 52
Joined: Fri Oct 22, 2004 2:19 pm

Post by Devnull »

I replaced that line to the following but the output is still blank.

Code: Select all

$array = mysql_fetch_assoc($area);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

no, no, no... from mysql_query() ;)
User avatar
Devnull
Forum Commoner
Posts: 52
Joined: Fri Oct 22, 2004 2:19 pm

Post by Devnull »

feyd wrote:no, no, no... from mysql_query() ;)
I'm confused now, which function are you on about? :oops:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

mysql_query("SELECT * FROM `user` WHERE `id` = " . (int)$_SESSION['id'] . " AND `area` = '$area' LIMIT 25");
to

Code: Select all

$result = mysql_query("SELECT * FROM `user` WHERE `id` = " . (int)$_SESSION['id'] . " AND `area` = '$area' LIMIT 25");
User avatar
Devnull
Forum Commoner
Posts: 52
Joined: Fri Oct 22, 2004 2:19 pm

Post by Devnull »

There is still no output...

/me gets an AK47 and shoots himself :evil:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

var_export($array)

see if anything comes out..

you may need to add "or die(mysql_error())" onto the mysql_query() call..
User avatar
Devnull
Forum Commoner
Posts: 52
Joined: Fri Oct 22, 2004 2:19 pm

Post by Devnull »

I added var_export($array); to my code and it outputted 'NULL'.
Post Reply