Jump to content

Recommended Posts

I'm working on my first php project, so I apologize if the questions seem a little simple :D. Thanks for your help it is greatly appreciated ;).

 

Here is my classes.php file. I want the write function to take the table the field I'm looking for and the userid, but it is not working properly.

 

I get this error when I try to run the function.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #3' at line 1

 

The rank function is supposed to load up all the userids in the database into an array and use usort to rank them according their win/lose ratio. I was a little confused on how to use the usort function. Any help here would be greatly appreciated.

 

I'm still working on the write function, but I assume it will be pretty similar to the read function.

 

<?php
class ladderfunctions
{
	function connect
	{
		[b]HIDDEN[/b]
	}

	function selectdb( $db )
	{
		mysql_select_db("$db")or die("cannot select DB");
	}

	function read( $table, $field, $userid )
	{
		$sql="SELECT $field FROM $table WHERE userid='$userid'";
		$result=mysql_query($sql) or die(mysql_error());
		if(mysql_num_rows($result)) {
		// userid exists
		$sql="SELECT $field FROM $table WHERE userid='$userid'";
		$result=mysql_query($result) or die(mysql_error());
		$row=mysql_fetch_assoc($result);
		return $row['$field']; }
	}

	function write( $table, $field, $userid ) 
	{

	}

	function rank()
	{
		$query="select * from ladder;
		$result=mysql_query($query);
		$row=mysql_fetch_row($result);
		$array[mysql_num_rows($result)];
		//Store userid into array
		for( int i=0; i<mysql_num_rows($result); i++)
		{
			$array[i]=$row[i];
		}
		//Sort userid in array by win/lose ratio
		function cmp($a, $b)
		{
			if( ((read(ladder, win, $a) / (read(ladder, lose, $a)) > read(ladder, win, $b) / read(ladder, lose, $b))) )
			{
				return $a;
			}
			else
				return $b;
		}
		usort($array, "cmp");
		return $array;
	}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/78581-solved-php-code-help/
Share on other sites

I'm working on my first php project, so I apologize if the questions seem a little simple :D. Thanks for your help it is greatly appreciated ;).

 

Here is my classes.php file. I want the write function to take the table the field I'm looking for and the userid, but it is not working properly.

 

I get this error when I try to run the function.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #3' at line 1

 

The rank function is supposed to load up all the userids in the database into an array and use usort to rank them according their win/lose ratio. I was a little confused on how to use the usort function. Any help here would be greatly appreciated.

 

I'm still working on the write function, but I assume it will be pretty similar to the read function.

 

<?php
class ladderfunctions
{
	function connect
	{
		[b]HIDDEN[/b]
	}

	function selectdb( $db )
	{
		mysql_select_db("$db")or die("cannot select DB");
	}

	function read( $table, $field, $userid )
	{
		$sql="SELECT $field FROM $table WHERE userid='$userid'";
		$result=mysql_query($sql) or die(mysql_error());
		if(mysql_num_rows($result)) {
		// userid exists
		$sql="SELECT $field FROM $table WHERE userid='$userid'";
		$result=mysql_query($result) or die(mysql_error());
		$row=mysql_fetch_assoc($result);
		return $row['$field']; }
	}

	function write( $table, $field, $userid ) 
	{

	}

	function rank()
	{
		$query="select * from ladder;
		$result=mysql_query($query);
		$row=mysql_fetch_row($result);
		$array[mysql_num_rows($result)];
		//Store userid into array
		for( int i=0; i<mysql_num_rows($result); i++)
		{
			$array[i]=$row[i];
		}
		//Sort userid in array by win/lose ratio
		function cmp($a, $b)
		{
			if( ((read(ladder, win, $a) / (read(ladder, lose, $a)) > read(ladder, win, $b) / read(ladder, lose, $b))) )
			{
				return $a;
			}
			else
				return $b;
		}
		usort($array, "cmp");
		return $array;
	}
}
?>

 

As you can tell from the color of the PHP code there you have missed out a close quote mark " so it's trying to include everything in $query.

 

So it'd be

 

<?php
class ladderfunctions
{
	function connect
	{
		[b]HIDDEN[/b]
	}

	function selectdb( $db )
	{
		mysql_select_db("$db")or die("cannot select DB");
	}

	function read( $table, $field, $userid )
	{
		$sql="SELECT $field FROM $table WHERE userid='$userid'";
		$result=mysql_query($sql) or die(mysql_error());
		if(mysql_num_rows($result)) {
		// userid exists
		$sql="SELECT $field FROM $table WHERE userid='$userid'";
		$result=mysql_query($result) or die(mysql_error());
		$row=mysql_fetch_assoc($result);
		return $row['$field']; }
	}

	function write( $table, $field, $userid ) 
	{

	}

	function rank()
	{
		$query="select * from ladder"; // added close quotes here, and tada the colour has changed. 
		$result=mysql_query($query);
		$row=mysql_fetch_row($result);
		$array[mysql_num_rows($result)];
		//Store userid into array
		for( int i=0; i<mysql_num_rows($result); i++)
		{
			$array[i]=$row[i];
		}
		//Sort userid in array by win/lose ratio
		function cmp($a, $b)
		{
			if( ((read(ladder, win, $a) / (read(ladder, lose, $a)) > read(ladder, win, $b) / read(ladder, lose, $b))) )
			{
				return $a;
			}
			else
				return $b;
		}
		usort($array, "cmp");
		return $array;
	}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397607
Share on other sites

error: change to this

 

		function rank()
	{
		$query="select * from ladder";
		$result=mysql_query($query);
		$row=mysql_fetch_row($result);
		$array[mysql_num_rows($result)];
		//Store userid into array
		for( int i=0; i<mysql_num_rows($result); i++)
		{
			$array[i]=$row[i];
		}
		//Sort userid in array by win/lose ratio
		function cmp($a, $b)
		{
			if( ((read(ladder, win, $a) / (read(ladder, lose, $a)) > read(ladder, win, $b) / read(ladder, lose, $b))) )
			{
				return $a;
			}
			else
				return $b;
		}
		usort($array, "cmp");
		return $array;
	}

Link to comment
https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397608
Share on other sites

You may want to check on your parsing...like

 

$query="select * from ladder;

 

versus

 

$query="select * from ladder";

 

and use $ to signify variables like here

 

for( int i=0; i<mysql_num_rows($result); i++)

 

versus

 

for($int i=0; $i<mysql_num_rows($result); $i++)

Link to comment
https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397612
Share on other sites

Thank you for all replys. I have made the changes you suggested. I haven't been able to test the rank function as it calls the read function, and the read function still displays an error.

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #3' at line 1

 

<?php
class ladderfunctions
{
	function connect
	{
		HIDDEN
	}

	function selectdb( $db )
	{
		mysql_select_db("$db")or die("cannot select DB");
	}

	function read( $table, $field, $userid )
	{
		$sql="SELECT $field FROM $table WHERE userid='$userid'";
		$result=mysql_query($sql) or die(mysql_error());
		if(mysql_num_rows($result)) {
		// userid exists
		$sql="SELECT $field FROM $table WHERE userid='$userid'";
		$result=mysql_query($result) or die(mysql_error());
		$row=mysql_fetch_assoc($result);
		return $row['$field']; }
	}

	function write( $table, $field, $userid ) 
	{

	}

	function rank()
	{
		$query="select * from ladder";
		$result=mysql_query($query);
		$row=mysql_fetch_row($result);
		$array[mysql_num_rows($result)];
		//Store userid into array
		for( int $i=0; $i<mysql_num_rows($result); $i++)
		{
			$array[$i]=$row[$i];
		}
		//Sort userid in array by win/lose ratio
		function cmp($a, $b)
		{
			if( ((read(ladder, win, $a) / (read(ladder, lose, $a)) > read(ladder, win, $b) / read(ladder, lose, $b))) )
			{
				return $a;
			}
			else
				return $b;
		}
		usort($array, "cmp");
		return $array;
	}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397618
Share on other sites

You still have a lot more issues in you code:

 

Example, why do you repeat the exact same query here?

 

$sql="SELECT $field FROM $table WHERE userid='$userid'";

$result=mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($result)) {

// userid exists

$sql="SELECT $field FROM $table WHERE userid='$userid'";

$result=mysql_query($result) or die(mysql_error());

 

You already did a Select once with the same statement, and you are using $result= on the last line with a argument of $result instead of $sql

Link to comment
https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397623
Share on other sites

You still have a lot more issues in you code:

 

Example, why do you repeat the exact same query here?

 

$sql="SELECT $field FROM $table WHERE userid='$userid'";

$result=mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($result)) {

// userid exists

$sql="SELECT $field FROM $table WHERE userid='$userid'";

$result=mysql_query($result) or die(mysql_error());

 

You already did a Select once with the same statement, and you are using $result= on the last line with a argument of $result instead of $sql

 

If I change the code to the following, it returns nothing.

 

 function read( $table, $field, $userid )
{
$sql="SELECT $field FROM $table WHERE userid='$userid'";
$result=mysql_query($sql) or die(mysql_error());
$row=mysql_fetch_assoc($result);
return $row['$field'];
}

Link to comment
https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397640
Share on other sites

Again paste the results of the following

 

mysql>select * where userid = '1';

 

$sql="SELECT $field FROM $table WHERE userid='$userid'";
$result=mysql_query($sql) or die(mysql_error());
$row=mysql_fetch_assoc($result);
print_r($row);

Link to comment
https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397652
Share on other sites

Thanks, I have solved my problem.

 

The read function should have looked like this:

 

		function read( $table, $field, $userid )
	{
		$sql="SELECT $field FROM $table WHERE userid='$userid'";
		$result=mysql_query($sql) or die(mysql_error());
		$row=mysql_fetch_assoc($result);
		return $row["$field"];
	}

Link to comment
https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397675
Share on other sites

I'm still having trouble on the rank function. Everything should work except I'm only storing one row in $row and I want to store all of the rows.

 

I think I need a while loop around $row=mysql_fetch_row($result); but I'm not exactly sure how to do it so it saves all the rows after each other.

 

		function rank()
	{
		$sql="select * from ladder";
		$result=mysql_query($sql);
		$row=mysql_fetch_row($result);
		for($i=0; $i<(mysql_num_rows($result)*6); $i+=6)
		{
			$ascarray[(read(ladder, win, $row[$i]) / read(ladder, lose, $row[$i]))] = $row[$i];
		}
		return krsort($ascarray);
	}

Link to comment
https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397804
Share on other sites

<?php
function rank()
	{
		$sql="select * from ladder";
		$result=mysql_query($sql) or die(mysql_error());
//This is what the while loop would look like

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

		//your code for the loop here

					}
	}

?>

 

Say table ladder had fields called 'username' 'pass' 'id'. If you want to show those variable's results in the loop. You have to use $row['username'] or $row['pass'], $row['id'] etc.. unless you otherwise define them.

Say $user = $row['username'];

Link to comment
https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397835
Share on other sites

Thanks for the reply!  :D

 

Is there a way to load all the rows one after each other into the array.

ie if i have two rows and 2 fields userid and username

then $array[0]=userid $array[1]=username $array[2]=userid $array[3]=username

 

<?php
function rank()
	{
		$sql="select * from ladder";
		$result=mysql_query($sql) or die(mysql_error());
//This is what the while loop would look like

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

		//your code for the loop here

					}
	}

?>

 

Say table ladder had fields called 'username' 'pass' 'id'. If you want to show those variable's results in the loop. You have to use $row['username'] or $row['pass'], $row['id'] etc.. unless you otherwise define them.

Say $user = $row['username'];

Link to comment
https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397839
Share on other sites

Thanks for the reply. That almost does it except for it saves it as an associative array and I just was a normal array. If I take out the mysql_assoc part I get this output when I print_r after your code. The array doesn't quite look right, but it loaded both rows.

 

Array ( [0] => Array ( [0] => 1 [userid] => 1 [1] => 2314-4953-3952 [friendcode] => 2314-4953-3952 [2] => 5 [win] => 5 [3] => 3 [lose] => 3 [4] => 0 [disconnect] => 0 [5] => 0 [newbr] => 0 ) [1] => Array ( [0] => 3 [userid] => 3 [1] => 2314-4953-3952 [friendcode] => 2314-4953-3952 [2] => 23 [win] => 23 [3] => 7 [lose] => 7 [4] => 2 [disconnect] => 2 [5] => 0 [newbr] => 0 ) ) 

 

while ($row=mysql_fetch_array($result,MYSQL_ASSOC)) 
{
    $array[]=$row;
}

Link to comment
https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-398236
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.