Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-15-2012, 01:18 PM   PM User | #1
Whatnot
New Coder

 
Join Date: Feb 2011
Posts: 71
Thanks: 8
Thanked 2 Times in 2 Posts
Whatnot is an unknown quantity at this point
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
Whatnot is offline   Reply With Quote
Old 04-15-2012, 05:08 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,491
Thanks: 18
Thanked 361 Times in 360 Posts
sunfighter is on a distinguished road
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.
sunfighter is offline   Reply With Quote
Old 04-15-2012, 06:01 PM   PM User | #3
Whatnot
New Coder

 
Join Date: Feb 2011
Posts: 71
Thanks: 8
Thanked 2 Times in 2 Posts
Whatnot is an unknown quantity at this point
well I'm trying to get data then pass it on from one file to another
Whatnot is offline   Reply With Quote
Old 04-15-2012, 07:19 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 04-15-2012, 08:17 PM   PM User | #5
Whatnot
New Coder

 
Join Date: Feb 2011
Posts: 71
Thanks: 8
Thanked 2 Times in 2 Posts
Whatnot is an unknown quantity at this point
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
Whatnot is offline   Reply With Quote
Old 04-16-2012, 04:17 AM   PM User | #6
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,491
Thanks: 18
Thanked 361 Times in 360 Posts
sunfighter is on a distinguished road
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?
sunfighter is offline   Reply With Quote
Old 04-16-2012, 02:47 PM   PM User | #7
Whatnot
New Coder

 
Join Date: Feb 2011
Posts: 71
Thanks: 8
Thanked 2 Times in 2 Posts
Whatnot is an unknown quantity at this point
no just Resource id #2
Whatnot is offline   Reply With Quote
Old 04-16-2012, 06:35 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 04-21-2012, 11:57 AM   PM User | #9
Whatnot
New Coder

 
Join Date: Feb 2011
Posts: 71
Thanks: 8
Thanked 2 Times in 2 Posts
Whatnot is an unknown quantity at this point
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;
 }
 ?>
Whatnot is offline   Reply With Quote
Old 04-21-2012, 05:30 PM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:53 AM.


Advertisement
Log in to turn off these ads.