Page 1 of 1

mysql trouble

Posted: Fri Apr 09, 2010 8:49 am
by darin
Excuse me for the silly (I guess) topic, but I'm stuck. I'm trying to pull some data from a table with the following code and it keeps telling me "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in W:\www\elis\includes\userrights.php on line 8". PLEASE help!

Code: Select all

$fieldid = array(1,2,3,4,7);
$userid = 1;
foreach($fieldid as $id){
  $query  ="SELECT right ";
  $query .="FROM userrights ";
  $query .="WHERE (userid = '{$userid}') AND (fieldid = '{$id}')";
  $queryresult = mysql_query($query, $connection);
  $j = mysql_fetch_array($queryresult);
  $right[$id] = $j['right'];
}
My table userrights looks like this:

userrightid userid fieldid right
1 1 1 1
2 1 2 1
3 1 3 1
4 1 4 1
5 1 5 1
6 1 6 1
7 1 7 1


Once again I apologise for being such a newbie...
Darin

Re: mysql trouble

Posted: Fri Apr 09, 2010 10:30 am
by pickle
Welcome to the boards, but...
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:1. Select the correct board for your query. Take some time to read the guidelines in the sticky topic.
Thread moved.

Re: mysql trouble

Posted: Fri Apr 09, 2010 10:42 am
by AbraCadaver
mysql_connect() mysql_select_db() ???

Re: mysql trouble

Posted: Fri Apr 09, 2010 10:54 am
by darin
Sorry for the wrong place pickle :oops:

AbraCadaver thisi is not it. Please find bellow the whole code, the trouble is that the first select works, but not the second :(

Code: Select all

<?php
  function readrights($userid, $screenid){
    global $connection;
    $query1  ="SELECT fieldid ";
    $query1 .="FROM fields ";
    $query1 .="WHERE screenid = '{$screenid}'";
    $quryresult1 = mysql_query($query1, $connection);
    $i = 0;
    while($field = mysql_fetch_array($quryresult1)){
      $fieldid[$i] = $field['fieldid'];
      $i = $i+1;
    }
    foreach($fieldid as $id){
      $query  ="SELECT right ";
      $query .="FROM userrights ";
      $query .="WHERE (userid = '{$userid}') AND (fieldid = '{$id}')";
      $queryresult = mysql_query($query, $connection);
      $j = mysql_fetch_array($queryresult);
      $right[$id] = $j['right'];
    }
    return $right;
  }
?>

Re: mysql trouble

Posted: Fri Apr 09, 2010 11:10 am
by AbraCadaver

Code: Select all

$queryresult = mysql_query($query, $connection) or die(mysql_error());

Re: mysql trouble

Posted: Fri Apr 09, 2010 11:21 am
by darin
AbraCadaver wrote:

Code: Select all

$queryresult = mysql_query($query, $connection) or die(mysql_error());
Thanks for the support, here is what i get:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM userrights WHERE (userid = '1') AND (fieldid = '1')' at line 1

The names of the DB fields and table are ten times checked :( ....

Re: mysql trouble

Posted: Fri Apr 09, 2010 11:26 am
by AbraCadaver
I should have looked more closely: http://dev.mysql.com/doc/refman/5.1/en/ ... words.html

Code: Select all

$query  = "SELECT `right` ";

Re: mysql trouble

Posted: Fri Apr 09, 2010 11:30 am
by darin
Yes! That did solve my problem, thank you verry much, but how in the world the first select works :?: ?

Kind regards :) ,
Darin

oops you gave a link, i'll read it....