problem with mysql again ... ffs

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 960 Views - Last Post: 09 September 2008 - 10:25 AM Rate Topic: -----

#1 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

problem with mysql again ... ffs

Posted 07 September 2008 - 02:01 AM

$name keeps echoing resource id #4

					<?php
						$name = mysql_query("select username from members where id != '".$_SESSION['id']."' ") or die(mysql_error());
						$namearray = mysql_fetch_array($name);						 
						$yesno = rand(0, 100);
						$who_t = rand(0,100);
						$who = $namearray[$who_t];
						$amount = rand(0, 100);
						echo $namearray;
						echo $name;
							function extort(){
								global $yesno;
								global $who;
								global $amount;
									if(isset($_POST['extort'])){						
										if($yesno > 1){
												echo "you extorted ".$amount." from ".$who."";	
																	
							}
						}
						}
						echo extort();
					?>


This post has been edited by ghqwerty: 07 September 2008 - 02:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: problem with mysql again ... ffs

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: problem with mysql again ... ffs

Posted 07 September 2008 - 06:27 AM

Of course it does, it's the resultset of a MySQL query!

It appears you would really be well-served to read -- and actually understand -- some of the tutorials here on PHP-MySQL integration.
Was This Post Helpful? 0
  • +
  • -

#3 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: problem with mysql again ... ffs

Posted 07 September 2008 - 09:05 AM

you need to echo $namearray[column], not $name
Was This Post Helpful? 0
  • +
  • -

#4 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: problem with mysql again ... ffs

Posted 07 September 2008 - 12:25 PM

i know

<inline>
echo $namearray;
and
echo $name
</inline>

are there to check what is returned as i was trying to see myself what was wrong

i do have a decent understanding of mysql and i cant find any tut's for my problem

my problem is in my query

i have changed variable names to make sure there is no overwriting
Was This Post Helpful? 0
  • +
  • -

#5 Ridikule  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 68
  • Joined: 29-September 05

Re: problem with mysql again ... ffs

Posted 07 September 2008 - 01:30 PM

change these lines:

$name = mysql_query("select username from members where id != '".$_SESSION['id']."' ") or die(mysql_error());
$namearray = mysql_fetch_array($name);

to

$result = mysql_query("select username from members where id != '".$_SESSION['id']."' ") or die(mysql_error());
$name = mysql_result($result, 0);
Was This Post Helpful? 0
  • +
  • -

#6 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: problem with mysql again ... ffs

Posted 08 September 2008 - 08:57 AM

have done that however it isnt working .. as i said it is something to do with my query

its saying Resource id #4 when i echo it

last this happened it was because i had overwriten something however now even when i change variable names it still doesnt work
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: problem with mysql again ... ffs

Posted 08 September 2008 - 09:09 AM

Post your current code.
Was This Post Helpful? 0
  • +
  • -

#8 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: problem with mysql again ... ffs

Posted 08 September 2008 - 09:10 AM

Try this code and let me know what the output on screen is:

<?php
	$sql = "SELECT id, username FROM members where id <> ".(int)$_SESSION['id'];
	$name = mysql_query($sql) or die('Something went tits up - Have a look at the SQL being passed: '.$sql.' ...and this is the database error: '.mysql_error());
	$namearray = mysql_fetch_array($name);
	//JUST ADDED THE FOLLOWING LINES FOR TESTING
	while($row = mysql_fetch_array($name, MYSQL_ASSOC)){
		echo '<p>'.$row['username'].' - ID: '.$row['id'].'</p>';
	}
	
	$yesno = rand(0, 100);
	$who_t = rand(0,100);
	$who = $namearray[$who_t]; // are you randomly picking an indexed row from the SQL results??
	// if only 10 results are found, and the random number picked is, say 14, then it will fail as that index won't exist
	$amount = rand(0, 100);
	//echo $namearray;
	//echo $name;
	function extort(){
		global $yesno;
		global $who;
		global $amount;
		if(isset($_POST['extort'])){ // a variable called extort is being passed through ?
			if($yesno > 1){
				return "you extorted ".$amount." from ".$who."";	
			}else{
				return 'The variable yesno is not greater than one';
			}
		}else{
			return 'There is no variable extort in the post data';
		}
	}
	echo extort();
?>


This post has been edited by pemcconnell: 08 September 2008 - 09:15 AM

Was This Post Helpful? 0
  • +
  • -

#9 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: problem with mysql again ... ffs

Posted 08 September 2008 - 09:15 AM

<?php
session_start();
if ($_SESSION['loggedIn'] != "true") {
     header("Location: checklogin.php");
}
require("conn.php");  
?>
<html>
<head>
<title>_-_Bliss wars_-_</title>

</head>
<body bgcolor="#ffffcc">
    <table name="shell" height="99%" width="99%" border="1">
        <tr >
            <td colspan="3" height="15%">
                <center>
                     <h1>
                         _-_Bliss wars_-_
                     </h1>
                </center>
            </td>
        </tr>
        <tr>
            <td width="10%">
                <?php require("links.php");?>
            </td>
            <td width="80%">
                <center>
                    <?php
                        $playerid = $_SESSION['id'];
                        $selectnames = mysql_query("SELECT username FROM members WHERE id = '".$playerid."'") or die(mysql_error());
                        $name = mysql_fetch_array($selectnames);                          
                        $yesno = rand(0, 100);
                        $who_t = rand(0,100);
                        $who = $sname[$who_t];
                        $amount = rand(0, 100);
                        echo $name[$who_t];
                        echo $selectnames;
                            function extort(){
                                global $yesno;
                                global $who;
                                global $amount;
                                    if(isset($_POST['extort'])){                        
                                        if($yesno > 1){
                                                echo "you extorted ".$amount." from ".$who."";    
                                                                    
                            }
                        }
                        }
                        echo extort();
                    ?>
                    <form method="post" action="extortion.php">
                    <input type="submit" name="extort" value="extort">
                    </form>
                </center>
            </td>
            <td width="10%">
                <?php require("links.php");?>
            </td>
            </tr>
    </table>

</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#10 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: problem with mysql again ... ffs

Posted 08 September 2008 - 09:19 AM

did you try the code i submitted?
Was This Post Helpful? 0
  • +
  • -

#11 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: problem with mysql again ... ffs

Posted 08 September 2008 - 09:20 AM

ok pemmconnell

tried your code

i also think that maybe i dont have enough rows in my db

but
when i hadnt pressed the extort button it said no variable called extort

when i had it said
you extorted 14 from

-----------------------------------------------------------------------------

also tonight im going to be looking into and learning how to do algorithms so this code should be better pretty soon :)

thanks for the advice jbrace
Was This Post Helpful? 0
  • +
  • -

#12 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: problem with mysql again ... ffs

Posted 08 September 2008 - 09:26 AM

woohoo
have changed
"SELECT id, username FROM members where id <> ".(int)$_SESSION['id'];

to

"SELECT username FROM members where id <> ".(int)$_SESSION['id'];

questions
why have you done <> ".(int)$_SESSION['id'];
is that like mysql version of not equal to ????

to make sure you always get a player
couldnt i do a count rows to find out how many rows there are and then do
$who_t = rand(0,$count);
???

This post has been edited by ghqwerty: 08 September 2008 - 09:28 AM

Was This Post Helpful? 0
  • +
  • -

#13 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: problem with mysql again ... ffs

Posted 08 September 2008 - 09:31 AM

i think that was more luck - i.e. the $who number was less than to total number of rows returned.

I've changed yer code again:

<?php
	$sql = "SELECT id, username FROM members where id <> ".(int)$_SESSION['id'];
	$name = mysql_query($sql) or die('Something went tits up - Have a look at the SQL being passed: '.$sql.' ...and this is the database error: '.mysql_error());
	$namearray = mysql_fetch_array($name);
	$totalnumrows = mysql_num_rows($name); // LOOK AT YOUR $who = LINE
	//JUST ADDED THE FOLLOWING LINES FOR TESTING
	while($row = mysql_fetch_array($name, MYSQL_ASSOC)){
		echo '<p>'.$row['username'].' - ID: '.$row['id'].'</p>';
	}
	
	$yesno = rand(0, 100);
	$who_t = rand(0,$totalnumrows); // NOW THIS CAN'T BE HIGHER THAN THE ARRAY INDEX TOTAL
	$who = $namearray[$who_t]; // are you randomly picking an indexed row from the SQL results??
	// if only 10 results are found, and the random number picked is, say 14, then it will fail as that index won't exist
	$amount = rand(0, 100);
	//echo $namearray;
	//echo $name;
	function extort(){
		global $yesno;
		global $who;
		global $amount;
//!!!!! if(isset($_POST['extort'])){ MOVED THIS
			if($yesno > 1){
				return "you extorted ".$amount." from ".$who."";	
			}else{
				return 'The variable yesno is not greater than one';
			}
		}else{
			return 'There is no variable extort in the post data';
		}
	}
	
	//ADDED THE MOVED LINE TO HERE
	if(isset($_POST['extort'])){
		echo extort();
		echo 'And the namearray looks like this:<br/>&nbsp;<br/>';
		print_r($namearray);
	}
?>


This post has been edited by pemcconnell: 08 September 2008 - 09:31 AM

Was This Post Helpful? 0
  • +
  • -

#14 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: problem with mysql again ... ffs

Posted 08 September 2008 - 09:35 AM

thats what i had just changed it to

now you numpty cos youve moved the if(isset)
it shows this

Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\extortion.php on line 91


the first one you did with my modifications works fine

$sql = "SELECT username FROM members where id <> ".(int)$_SESSION['id'];
    $name = mysql_query($sql) or die('Something went tits up - Have a look at the SQL being passed: '.$sql.' ...and this is the database error: '.mysql_error());
    $namearray = mysql_fetch_array($name);
    //JUST ADDED THE FOLLOWING LINES FOR TESTING
    while($row = mysql_fetch_array($name, MYSQL_ASSOC)){
        echo '<p>'.$row['username'].' - ID: '.$row['id'].'</p>';
    }
    $count = mysql_num_rows($name)  ;
    $yesno = rand(0, 100);
    $who_t = rand(0, $count);
    $who = $namearray[$who_t]; // are you randomly picking an indexed row from the SQL results??
    // if only 10 results are found, and the random number picked is, say 14, then it will fail as that index won't exist
    $amount = rand(0, 100);
    //echo $namearray;
    //echo $name;
    function extort(){
        global $yesno;
        global $who;
        global $amount;
        if(isset($_POST['extort'])){ // a variable called extort is being passed through ?
            if($yesno > 1){
                return "you extorted ".$amount." from ".$who."";    
            }else{
                return 'The variable yesno is not greater than one';
            }
        }else{
            return 'There is no variable extort in the post data';
        }
    }
    echo extort();


This post has been edited by ghqwerty: 08 September 2008 - 09:37 AM

Was This Post Helpful? 0
  • +
  • -

#15 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: problem with mysql again ... ffs

Posted 08 September 2008 - 10:50 AM

errm ok i just added 2 more peopl to my database to see how it would work and firstly it now echo's

james - ID:

brandy - ID:

attempt to extort someone

and secondly it has gone back to saying

you extorted x from blank
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2