Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    75
    Thanks
    9
    Thanked 2 Times in 2 Posts

    Help with grabing data

    hi I'm trying to grab data from the mysql using this code

    Code:
    <?php
      $con = mysql_connect("localhost","duser","pass");
      if (!$con)
        {
        die('could not connect: ' . mysql_error());
      }
      
      if($User){
     	$thisUser = $User->nickname();
     } else {
     	$thisUser = "Guest";
     }
     
     mysql_select_db("dname", $con);
     $result = mysql_query("SELECT * FROM users WHERE gender = '$_GET[gender]'");
     $gender = mysql_result($result, 0);
     
     
     if($gender == '1' ) {
             $ProMode = PROFILMODE11;
     } elseif ($gender == '2') {
             $ProMode = PROFILMODE5;
     } else {
             $ProMode = PROFILMODE0;
     }
     ?>
    and then use on another file this code to echo the data

    Code:
     <PARAM name="UserRole" value="<?php echo $ProMode; ?>">
    but for some reason I'm not picking up the data

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 594 Times in 593 Posts
    I take it that
    Code:
    WHERE gender = '$_GET[gender]'
    is referring to male/female. So using $gender in the next line is confusing because you should already know the gender.

    But your problem is $gender in
    Code:
    $gender = mysql_result($result, 0);
    is not a string, it's an array so
    Code:
    if($gender == '1' )
    is meaningless.

    Normally written:
    Code:
    $query = "SELECT * FROM users  WHERE gender = 'something';
    $result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
    echo $row[0];
    echo $row[1];
    echo $row[2];
    echo $row[3]; // to how many fields you have in the table
    }
    This will show you what your getting from the query.

  • #3
    New Coder
    Join Date
    Feb 2011
    Posts
    75
    Thanks
    9
    Thanked 2 Times in 2 Posts
    well I'm trying to get data then pass it on from one file to another

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by sunfighter View Post
    But your problem is $gender in
    Code:
    $gender = mysql_result($result, 0);
    is not a string, it's an array so
    Code:
    if($gender == '1' )
    is meaningless.
    This isn't correct. mysql_result returns a string of the scoped column and row, never an array.

    The problem is its highly unlikely you are pulling the correct data. Using a * pulls all records of a table user, which likely contains more than one field and highly improbable that the first one is the gender (although no rule exists specifying that an primary key is the first field, its typically the first field for simplicity). Either specify the correct field in the mysql_result, or modify the query to SELECT gender FROM ....

    The question of why are you querying for a known where value is valid though. There is no reason to query for a known value.
    If you want to retrieve all records within a row, use mysql_fetch_assoc or mysql_fetch_row. If there are multiple iterations use a while loop. Never use mysql_result for multiple fields or rows as its very slow.

    So the question really is what are you looking for? The query tells me you want to find any user that is male or female as specified in $_GET[gender] (which should be using '" . $_GET['gender'] . "' btw), in which case you are intending to pull multiple records from a resultset. For this you must loop.

  • #5
    New Coder
    Join Date
    Feb 2011
    Posts
    75
    Thanks
    9
    Thanked 2 Times in 2 Posts
    ok here what I'm trying to do
    get the gender from database 0 is no gender 1 is male 2 is female then using the param to echo it

    The param needs to echo back to the irc server (which it doing but always PROFILMODE0)
    PROFILMODE0 for no gender
    PROFILMODE11 male
    and PROFILEMODE5 female

  • #6
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 594 Times in 593 Posts
    Fou-Lu Your probably right. I don't know enough to say one way or the other. What I do know is if you do this
    Code:
    $result = mysql_query($query) or die(mysql_error());
    and try this
    Code:
    print_r($result);
    or this
    Code:
    echo $result;
    You get
    Code:
    Resource id #4
    for an answer.That's all I need to know. In my world $result is unusable and we need to further condition it with mysql_fetch_array or one other command from that group.

    But the bigger question is for Whatnot.
    Doesn't $_GET[gender] already give you the gender?

  • #7
    New Coder
    Join Date
    Feb 2011
    Posts
    75
    Thanks
    9
    Thanked 2 Times in 2 Posts
    no just Resource id #2

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    The resource number is irrelevant as it is a number tracked internally by PHP and doesn't know how to describe it. Therefore you cannot print an external resource, you must pass it to a function that knows how to deal with it (such as mysql_fetch_row, fread, whatever the resource is).
    Unless you mean you are only receiving a resource id, in which case you need to post your current code.

  • #9
    New Coder
    Join Date
    Feb 2011
    Posts
    75
    Thanks
    9
    Thanked 2 Times in 2 Posts
    so it should be like this?

    Code:
    <?php
    
     if(mysql_num_rows($result15) > 0) {
    	$gender = mysql_result($result15,0,"gender");
    	
     if($gender == '1' ) {
             $ProMode = PROFILMODE3;
     } elseif ($gender == '2') {
             $ProMode = PROFILMODE4;
     } else {
             $ProMode = PROFILMODE0;
     }
     ?>

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by Whatnot View Post
    so it should be like this?

    Code:
    <?php
    
     if(mysql_num_rows($result15) > 0) {
    	$gender = mysql_result($result15,0,"gender");
    	
     if($gender == '1' ) {
             $ProMode = PROFILMODE3;
     } elseif ($gender == '2') {
             $ProMode = PROFILMODE4;
     } else {
             $ProMode = PROFILMODE0;
     }
     ?>
    So olong as $result15 is that of a mysql_query, then yes if the field name is 'gender' that will work to pull the gender from the first record in the resultset.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •