4 Replies - 562 Views - Last Post: 29 November 2012 - 12:52 PM Rate Topic: -----

#1 SeanKeenan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 74
  • Joined: 23-March 09

Can't pull from the database 100%

Posted 28 November 2012 - 02:21 AM

Hi guys, basically I have a script that takes two users, you vote between them and the'winner', 'loser' and 'voter' is stored in to the database 'votes'.

My problem is this:

    $user1 = $_GET['opp1'];
    $user1 = strip_tags($user1);
    $user2 = $_GET['opp2'];
    $user2 = strip_tags($user2);
    $voter = $user3['login'];
    $o = '0';
    $check = "SELECT * from votes WHERE winner = '$user1' AND loser = '$user2' AND voter = '$user3[login]' OR winner = '$user2' AND loser = '$user1' AND voter = '$user3[login]'";
    $check1 = mysql_query($check) or die ("Couldn't run it" .mysql_error());
    while ($check2 = mysql_fetch_array($check1)) { $o++; }



The reason the top code has a while statement is I was able to block some information off if the count from the database was over 0 so obviously it's recognising that this user has voted on these two users before but I can't seem to get it to show who won said vote.

I don't want the voter(user) to be able to vote on a matchup they have already voted on. I'm trying to search the database for results where the voter has voted user 1 has won and 2 lost or 2 won and 1 lost - If you understand.

I tried just using
$user1 = $_GET['opp1'];
    $user1 = strip_tags($user1);
    $user2 = $_GET['opp2'];
    $user2 = strip_tags($user2);
    $voter = $user3['login'];
    $check = "SELECT * from votes WHERE winner = '$user1' AND loser = '$user2' AND voter = '$user3[login]' OR winner = '$user2' AND loser = '$user1' AND voter = '$user3[login]'";
    $check1 = mysql_query($check) or die ("Couldn't run it" .mysql_error());


Then tried using the below
data stored in $user1 and $user 2 is exactly the same as what's stored in either winner or loser.
Tried all 3, to no avail.
if ($user1 == $check['winner'])
 { do_stuff(); }

if ($user1 == $check1['winner'])
 { do_stuff(); }

if ($user1 == $check2['winner'])
 { do_stuff(); }



Anyone got any ideas why I can't return the result? It literally returns nothing but doesn't prompt an error.

I just want to be able to print out who won and who lost.

This post has been edited by SeanKeenan: 28 November 2012 - 02:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Can't pull from the database 100%

#2 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Can't pull from the database 100%

Posted 28 November 2012 - 02:27 AM

Hello,

In your above code $check is just a string, so your DB result will not be there. The value $check1 should be a MySQL resource therefore $check2 in your first snippet will contain the result, but you don't appear to use it correctly.

Take a look at the example snippet below, hopefully that gives you an idea!
$sql = 'SELECT someStuff FROM someTable WHERE someThing = someValue';
$query = mysql_query($sql) or die(mysql_error());
while ($result = mysql_fetch_array($query))
{
	// Here, $result will be a different row
	// of your query each time it loops.
	echo $result['someStuff'];
}

Was This Post Helpful? 0
  • +
  • -

#3 SeanKeenan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 74
  • Joined: 23-March 09

Re: Can't pull from the database 100%

Posted 28 November 2012 - 03:57 AM

I've pretty much done the exact same thing except I've used

$check = "SELECT * from votes WHERE winner = '$user1' AND loser = '$user2' AND voter = '$user3[login]' OR winner ='$user2' AND loser = '$user1' AND voter = '$user3[login]'";
$check1 = mysql_query($check) or die ("Couldn't run it" .mysql_error());
while ($check2 = mysql_fetch_array($check1)) 
{ 
   $o++; 
}



So should be able now to print

print $check2['winner'];


Or would I have to add it to a variable within the while statement like

{
   $o++;
   $winner = $check2['winner'];
}

print $winner;



Thanks for your reply

EDIT: What I suggested doesn't work.

Think I've solved it. Had it all inside another if statement which I think was failing.

This post has been edited by SeanKeenan: 28 November 2012 - 04:07 AM

Was This Post Helpful? 0
  • +
  • -

#4 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Can't pull from the database 100%

Posted 29 November 2012 - 02:35 AM

Hello again,

The second snippet you suggested there (third snippet on the post) should work, the $check2 variable is only available within that statement scope. Assigning it to another value ($winner) seems a little wasteful (as I cannot see the rest of your code), I would perform what you need to within the scope rather than doing that.
Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Can't pull from the database 100%

Posted 29 November 2012 - 12:52 PM

Just to point this out... You should try to use descriptive, easily understandable variable names. If you use $check for the SQL string, $check1 for the SQL result and $check2 for the individual rows retrieved from the SQL result, you are only making it harder to read. People will have to start retracing their steps just to figure out what each variable is supposed to be. - Instead, use names that actually make sense, like $sql for the SQL command, $result for the result set and $row for the rows.

This may seem trivial in such a small snippet, but believe me, it makes a LOT of difference when the code starts to grow, especially when more than one coder is involved. You will no doubt spend more time reading code than writing it, so why not making that easier on everybody involved?

Also, while I'm already nitpicking. Avoid using SELECT * FROM ... in your code. It's exceedingly rare that you will actually require ALL the fields from a table. Fetching unnecessary data is just wasteful. Instead do stuff like: SELECT winner, loser, voter FROM .... (Assuming your code will need to use those three values.)
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1