chrmlr Posted November 23, 2007 Share Posted November 23, 2007 I'm working on my first php project, so I apologize if the questions seem a little simple . 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; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/ Share on other sites More sharing options...
helraizer Posted November 23, 2007 Share Posted November 23, 2007 I'm working on my first php project, so I apologize if the questions seem a little simple . 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; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397607 Share on other sites More sharing options...
Wes1890 Posted November 23, 2007 Share Posted November 23, 2007 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; } Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397608 Share on other sites More sharing options...
revraz Posted November 23, 2007 Share Posted November 23, 2007 You forgot a closing quote here function rank() { $query="select * from ladder; <--------- Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397610 Share on other sites More sharing options...
wsantos Posted November 23, 2007 Share Posted November 23, 2007 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++) Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397612 Share on other sites More sharing options...
revraz Posted November 23, 2007 Share Posted November 23, 2007 I think you mean for(int $i=0; and not for($int i=0; Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397614 Share on other sites More sharing options...
wsantos Posted November 23, 2007 Share Posted November 23, 2007 LOL...didn't read the whole line Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397616 Share on other sites More sharing options...
chrmlr Posted November 23, 2007 Author Share Posted November 23, 2007 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; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397618 Share on other sites More sharing options...
helraizer Posted November 23, 2007 Share Posted November 23, 2007 What errors does it produce? Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397620 Share on other sites More sharing options...
revraz Posted November 23, 2007 Share Posted November 23, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397623 Share on other sites More sharing options...
wsantos Posted November 23, 2007 Share Posted November 23, 2007 Let's take this a step at a time... Do this and post the result here... function read( $table, $field, $userid ) { $sql="SELECT $field FROM $table WHERE userid='$userid'"; echo $sql; } Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397625 Share on other sites More sharing options...
chrmlr Posted November 23, 2007 Author Share Posted November 23, 2007 Let's take this a step at a time... Do this and post the result here... function read( $table, $field, $userid ) { $sql="SELECT $field FROM $table WHERE userid='$userid'"; echo $sql; } Seems to work properly SELECT username FROM user WHERE userid='1' Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397631 Share on other sites More sharing options...
chrmlr Posted November 23, 2007 Author Share Posted November 23, 2007 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']; } Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397640 Share on other sites More sharing options...
wsantos Posted November 23, 2007 Share Posted November 23, 2007 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); Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397652 Share on other sites More sharing options...
chrmlr Posted November 23, 2007 Author Share Posted November 23, 2007 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"]; } Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397675 Share on other sites More sharing options...
chrmlr Posted November 23, 2007 Author Share Posted November 23, 2007 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); } Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397804 Share on other sites More sharing options...
helraizer Posted November 24, 2007 Share Posted November 24, 2007 <?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']; Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397835 Share on other sites More sharing options...
chrmlr Posted November 24, 2007 Author Share Posted November 24, 2007 Thanks for the reply! 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']; Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-397839 Share on other sites More sharing options...
wsantos Posted November 24, 2007 Share Posted November 24, 2007 while ($row=mysql_fetch_array($result,MYSQL_ASSOC)) { $array[]=$row; } Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-398129 Share on other sites More sharing options...
chrmlr Posted November 24, 2007 Author Share Posted November 24, 2007 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; } Quote Link to comment https://forums.phpfreaks.com/topic/78581-solved-php-code-help/#findComment-398236 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.