6 Replies - 1526 Views - Last Post: 11 September 2008 - 10:30 AM Rate Topic: -----

#1 ghqwerty   User is offline

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

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

my sql only selecting one row from db

Posted 10 September 2008 - 02:04 PM

ok so i think my problem is that my sql query is only selecting 1 row of data from the databse

i cant see why

could someone please see if they can spot where my mistake is

i think it is something to do with this line

$name = mysql_query("SELECT username FROM members WHERE id != ".$_SESSION['id']."") or die('Something went tits up - Have a look at the SQL being passed: '.$sql.' ...and this is the database error: '.mysql_error()); 



however this is the full code for that part

                                <?php                 
                                    $name = mysql_query("SELECT username FROM members WHERE id != ".$_SESSION['id']."") 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);
                                    $count = mysql_num_rows($name)  ;
                                    $yesno = rand(0, 100);
                                    $who_t = rand(0, $count-1);
                                    $who = $namearray[$who_t] ;
                                    $amount = rand(0, 100);
                                        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."</td>";    
                                                    }else{
                                                        return 'sorry better luck next time</td>';
                                                    }
                                                }else{
                                                    return 'attempt to extort money from someone</td>';
                                                }
                                        }
                                    
                                    echo extort(); 
                                ?>



Is This A Good Question/Topic? 0
  • +

Replies To: my sql only selecting one row from db

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5552
  • View blog
  • Posts: 14,558
  • Joined: 18-April 07

Re: my sql only selecting one row from db

Posted 10 September 2008 - 02:44 PM

Well run the query inside of mysql php myadmin (if you have that on your host) or just run it through PHP and echo out your $count variable to see if it is 1. Also make sure that $_SESSION['id'] is the value you expect and then use those two pieces of data while you browse your database to see why you might be receiving 1 record.

I believe the answer to your problem is going to lie in your database and the data it has in it as well as the value you are getting from $_SESSION['id'].

Keep in mind that $_POST is also from a form that has its method property set to post, not data through the URL. Just wanted to clarify that because of your comment about extort's source. :)
Was This Post Helpful? 1
  • +
  • -

#3 Ridikule   User is offline

  • D.I.C Head

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

Re: my sql only selecting one row from db

Posted 10 September 2008 - 03:38 PM

You are running your query, but you are only retrieving one value from your result set.

Add something like...
$name = mysql_query("SELECT username FROM members WHERE id != ".$_SESSION['id']."");
while( $namearray = mysql_fetch_array($name) )
{
    // do something with $namearray.
}



What you have is just grabbing the first result and then the script ends, you have to have a loop to keep grabbing records from the result set until all the records have been returned (like above).
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5552
  • View blog
  • Posts: 14,558
  • Joined: 18-April 07

Re: my sql only selecting one row from db

Posted 10 September 2008 - 03:41 PM

well holy crap I totally missed that! Damn I must be more tired than I thought. Good call there Ridikule. :^:
Was This Post Helpful? 1
  • +
  • -

#5 ghqwerty   User is offline

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

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

Re: my sql only selecting one row from db

Posted 11 September 2008 - 09:16 AM

damnit yes thank i totally forgot about the loop :S stupid me

*goes and gets a coffee*
Was This Post Helpful? 0
  • +
  • -

#6 ghqwerty   User is offline

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

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

Re: my sql only selecting one row from db

Posted 11 September 2008 - 10:15 AM

ok so now im getting pissed off

ive got it down to this and it stil dont f*cking work

i know that it keeps overwriting the variable (anyway to change this i have tried array but it dont work)
or if i just leave the while loop blank like
                                    while($namearray = mysql_fetch_array($name)){

                                    }
                                 



it still doesnt work

this is my full code
ive been trying to do this simple thing for a couple of days now and im getting kinda annoyed so whoever gets it working for me can have 3 thanks :D
im going to post the whole page code as it might be something outside what im looking at although i dont think so
<?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%">
            <br /><br /><br /><br />
                <p align="center">
                    <table name="extort" width="30%" height="20%" border="1">
                        <tr>
                            <td>
                                <center>
                                    <strong>
                                        extortion
                                    </strong>
                                </center>
                            </td>
                        </tr>
                        <tr>
                            <td>               
                                <?php                 
                                    $name = mysql_query("SELECT username FROM members WHERE id != '".$_SESSION['id']."'") or die('Something went tits up - Have a look at the SQL being passed: '.$sql.' ...and this is the database error: '.mysql_error());                                  
                                    while($namearray = mysql_fetch_array($name)){
                                        $ex = $namearray[username]; 
                                    }
                                    $count = mysql_num_rows($name)  ;
                                    $yesno = rand(0, 100);
                                    $who_t = rand(0, $count-1);
                                    $who = $ex[$who_t] ;
                                    $amount = rand(0, 100);
                                        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."</td>";    
                                                    }else{
                                                        return 'sorry better luck next time</td>';
                                                    }
                                                }else{
                                                    return 'attempt to extort money from someone</td>';
                                                }
                                        }
                                    
                                    echo extort(); 
                                ?>
                        </tr>
                        <tr height="5%">
                            <td>
                                <p align="right">
                                    <form method="post" action="extortion.php">
                                    <input type="submit" name="extort" value="extort">
                                    </form>
                                </p>
                </p>
            </td>
        </tr>
</table>



            <td width="10%">
                <?php require("links.php");?>
            </td>
            </tr>
    </table>

</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#7 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5552
  • View blog
  • Posts: 14,558
  • Joined: 18-April 07

Re: my sql only selecting one row from db

Posted 11 September 2008 - 10:30 AM

Well you are going to need more than just assigning a variable in the loop. You have to put all lines that work on $ex in the loop to. That way you can pull out the name, do your calculations with it, call your function etc and everything on every loop iteration. Otherwise you are just going to be rewriting $ex over and over again and then come out of the loop at the end of it all.

<?php                 
$name = mysql_query("SELECT username FROM members WHERE id != '".$_SESSION['id']."'") or die('Something went tits up - Have a look at the SQL being passed: '.$sql.' ...and this is the database error: '.mysql_error());                                  

// Loop through the result set and put it into an array from which we can pick a name from.
while($namearray = mysql_fetch_array($name)){
	// Here we dump it into an array called $ex so we can work on it
	$ex[] = $namearray[username]; 
}

// Count the total entries in our array and store into $count
$count = count($ex)  ;
$yesno = rand(0, 100);

// Get a random value from 0 to the upper subscript of our array
$who_t = rand(0, $count-1);

// Extract the name of the random person
$who = $ex[$who_t] ;

// Random amount
$amount = rand(0, 100);

// Extort the punk!
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."</td>";    
			}else{
				return 'sorry better luck next time</td>';
			}
		}else{
			return 'attempt to extort money from someone</td>';
		}
}

echo extort(); 
?>



Here we pull out all the names from the result set, store it into your $ex array and then pick a random person from the array for you to extort. This should move you along a bit and get things working for you. Read the in-code comments to see what is going on here.

Enjoy!

"At DIC we be extorting code ninjas... but we only extort cookies out of people. Mmmmm" :snap:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1