Page 1 of 2

Minor MySQL Issue

Posted: Mon Aug 08, 2005 6:57 am
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');

Posted: Mon Aug 08, 2005 7:00 am
by feyd
SELECT id

or

SELECT user_id

Your code uses user_id, but your query is id

Posted: Mon Aug 08, 2005 7:07 am
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

Posted: Mon Aug 08, 2005 7:08 am
by feyd
you likely have an error in your query or something.. check what mysql_error() says.

Posted: Mon Aug 08, 2005 7:14 am
by Devnull
I already tried that before, I don't get an error message, all I get is warning message I quoted above.

Posted: Mon Aug 08, 2005 7:17 am
by feyd
hmm... $conn doesn't have much to do with mysql_result(). You want $result. :?

Posted: Mon Aug 08, 2005 7:22 am
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"; ?>

Posted: Mon Aug 08, 2005 7:36 am
by feyd
you're not storing $result from the query involved..

Posted: Mon Aug 08, 2005 7:56 am
by Devnull
I replaced that line to the following but the output is still blank.

Code: Select all

$array = mysql_fetch_assoc($area);

Posted: Mon Aug 08, 2005 7:59 am
by feyd
no, no, no... from mysql_query() ;)

Posted: Mon Aug 08, 2005 8:02 am
by Devnull
feyd wrote:no, no, no... from mysql_query() ;)
I'm confused now, which function are you on about? :oops:

Posted: Mon Aug 08, 2005 8:05 am
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");

Posted: Mon Aug 08, 2005 8:09 am
by Devnull
There is still no output...

/me gets an AK47 and shoots himself :evil:

Posted: Mon Aug 08, 2005 8:11 am
by feyd
var_export($array)

see if anything comes out..

you may need to add "or die(mysql_error())" onto the mysql_query() call..

Posted: Mon Aug 08, 2005 8:22 am
by Devnull
I added var_export($array); to my code and it outputted 'NULL'.