Jump to content

Very Simple for all but me!


unistake

Recommended Posts

Hi,

I have a very simple problem!

 

I have a mysql database and all i want to do is "SELECT user FROM table WHERE col1='$col2'"

and then echo the user names.

I, for some reason though am just having trouble getting the data!

 

Any help much appreciated!

Thanks

Link to comment
Share on other sites

OK! this is what I have so far!...

It is meant to be a sort of referral tracking program! The users have a unique 'code' .

All the people they have referred will have the same value as 'code' typed in a column called 'friend'

Therefore this program is meant to show a counter of the amount of people referred! - But it is not working!

Anyone got any ideas?! Thanks

 
<?php
  $user = $_COOKIE["user"];
  
include("db.php");

$user = $_COOKIE["user"];
  $cxn = mysqli_connect($host,$username,$password,$database)
         or die ("couldn't connect to server");
  $query = "SELECT code FROM Member WHERE user='$user'";
  $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
$nrows = mysqli_num_rows($result);

while($row = mysqli_fetch_assoc($result)) {
extract($row);
}

if ($code !=0) {

$query = "SELECT * FROM Member WHERE friend='$code'";
  $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
$nrows = mysqli_num_rows($result);
$referred = $nrows-1;
echo "$referred";
}
else { 
echo "0";
}
?>

Link to comment
Share on other sites

ok tried that but the echos are exactly like the line below

SELECT code FROM Member WHERE user='testing-user'

 

also the second does not show because of the IF statement. It just skips it on to the ELSE statement and echos "0".

 

There is definately a value in the right mysql fields.

Link to comment
Share on other sites

You have a problem with your loop, as your code now stands, the while loop will go through all the record and then do the "if". The "if" will only act on the last record retrieved, not all of them. Also, don't use extract, but use the array, $row, itself. Try this:

<?php
  $user = $_COOKIE["user"];
  
include("db.php");

$user = $_COOKIE["user"];
  $cxn = mysqli_connect($host,$username,$password,$database)
         or die ("couldn't connect to server");
  $query = "SELECT code FROM Member WHERE user='$user'";
  $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
$nrows = mysqli_num_rows($result);

while($row = mysqli_fetch_assoc($result)) {

       if ($row['code'] !=0) {

          $query = "SELECT * FROM Member WHERE friend='" . $row['code'] . "'";
          $result = mysqli_query($cxn,$query)
                  or die ("Couldn't execute query.");
          $nrows = mysqli_num_rows($result);
          $referred = $nrows-1;
          echo $referred . '<br>';
    } else  
         echo "0<br>";
}
?>

 

Ken

Link to comment
Share on other sites

You are using the same variable "$result" for the two different mysqli_query() calls, you need to use different variables:

<?php
  $user = $_COOKIE["user"];
  
include("db.php");

$user = $_COOKIE["user"];
  $cxn = mysqli_connect($host,$username,$password,$database)
         or die ("couldn't connect to server");
  $query = "SELECT code FROM Member WHERE user='$user'";
  $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
$nrows = mysqli_num_rows($result);

while($row = mysqli_fetch_assoc($result)) {

       if ($row['code'] !=0) {

          $query = "SELECT * FROM Member WHERE friend='" . $row['code'] . "'";
          $rs = mysqli_query($cxn,$query)
                  or die ("Couldn't execute query.");
          $nrs = mysqli_num_rows($rs);
          $referred = $nrs-1;
          echo $referred . '<br>';
    } else  
         echo "0<br>";
}
?>[/code[

Ken

Link to comment
Share on other sites

Still echoing "0"!

Is there any other way that I can do this sort of referral thing?

All I want to do is to allow my users to make a unique referral code. So that their friends can type it in when registering.

Then the user can track his/her friends.

 

Any suggestions would be appreciated.

Link to comment
Share on other sites

That is what i have been doing.... this is everything.

 

<?php
  
include("db.php");

$user = $_COOKIE["user"];
  $cxn = mysqli_connect($host,$username,$password,$database)
         or die ("couldn't connect to server");
  $query = "SELECT code FROM Member WHERE user='$user'";
  $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
$nrows = mysqli_num_rows($result);

while($row = mysqli_fetch_assoc($result)) {

       if ($row['code'] !=0) {

          $query = "SELECT * FROM Member WHERE friend='" . $row['code'] . "'";
          $rs = mysqli_query($cxn,$query)
                  or die ("Couldn't execute query.");
          $nrs = mysqli_num_rows($rs);
          $referred = $nrs-1;
          echo $referred;
    } else  
         echo "0";
}
?>

 

Database: Member

userID    user          friend            code

28        123name                      testcode

31        user123    testcode

 

Everything i have given you is "as is".

Link to comment
Share on other sites

          $nrs = mysqli_num_rows($rs);
          $referred = $nrs-1;

 

you set $nrs = mysqli_num_rows($rs); then set $referred = $nrs-1;

 

so if $nrs == 1, $referred == 0;

 

if $nrs == 1, you'll get 0 echoed either way, whether $row['code'] !=0 or not.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.