13 Replies - 2257 Views - Last Post: 26 January 2003 - 11:53 PM Rate Topic: -----

#1 Markp  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 18-January 02

mysql_query AAAAAAGGGGHHH!

Posted 26 January 2003 - 09:35 AM

I can't work this out... I've been through several examples and just can't seem to get it!

Anyway... what I am trying to do is create a highscore table based on my mysql table...

The mysql table is called tutorial_user_auth
The column I want to get all results from is called HighScore

I can connect to the database quite easily... its just the mysql_query that I can't do?!

Any help would be great! :)

Mark

Is This A Good Question/Topic? 0
  • +

Replies To: mysql_query AAAAAAGGGGHHH!

#2 The Neoracle  Icon User is offline

  • Check, check, 1, 2.
  • member icon

Reputation: 21
  • View blog
  • Posts: 4,097
  • Joined: 30-March 01

Re: mysql_query AAAAAAGGGGHHH!

Posted 26 January 2003 - 09:57 AM

Select HighScore FROM tutorial_user_auth



Is that all your trying to do? Or are you having problems with the PHP part?
Was This Post Helpful? 0
  • +
  • -

#3 Markp  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 18-January 02

Re: mysql_query AAAAAAGGGGHHH!

Posted 26 January 2003 - 10:07 AM

RobotDeathSquad, on Jan 26 2003, 10:57 AM, said:

Select HighScore FROM tutorial_user_auth



Is that all your trying to do? Or are you having problems with the PHP part?

Hehe nice avatar... I love that film :D

I'm having problems with this part:

$result3 = mysql_query("SELECT * FROM $table WHERE userName = '$username' AND HighScore = '$highscore'");

Was This Post Helpful? 0
  • +
  • -

#4 Markp  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 18-January 02

Re: mysql_query AAAAAAGGGGHHH!

Posted 26 January 2003 - 10:19 AM

$result3 = mysql_query("SELECT HighScore FROM $table");
print $result3;


This is my latest code... Mmm... it just returns Resource id #2.

How can I get it to return all the values in the database column highscore?

:)
Was This Post Helpful? 0
  • +
  • -

#5 Cookie Mobster  Icon User is offline

  • nooneenooneenooonee
  • member icon

Reputation: 7
  • View blog
  • Posts: 4,730
  • Joined: 12-October 01

Re: mysql_query AAAAAAGGGGHHH!

Posted 26 January 2003 - 10:44 AM

Well what you're doing is just printing the resource (which doesn't work). What you need to do is use a fetch of some sort to get the rows from the result set.

if you want to get every row from the result at once:
$result = mysql_query($sql);
foreach($row = mysql_fetch_assoc($result)){
    print $row;
}


or you can pick through the result for a specific row:
$result = mysql_query($sql);
mysql_field_seek($result(10));
$row[10] = mysql_fetch_row($result);

mysql_field_seek($result(21));
$row[21] = mysql_fetch_row($result);



One last note you can use row/assoc/and array independent of the method you use.

mysql_fetch_assoc() will return an associative array (ie. $row["sam"], $row["mike"]) with the field names as identifiers.

mysql_fetch_row() will return a numeric array (ie. $row[0], $row[3]).

mysql_fetch_array() will return both types in one array (ie. $row["sam"],$row[0]) note that this gives you two ways to access the same data but doubles the size of your array.

Cheers Sam
Was This Post Helpful? 0
  • +
  • -

#6 Markp  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 18-January 02

Re: mysql_query AAAAAAGGGGHHH!

Posted 26 January 2003 - 10:56 AM

Cookie Mobster, on Jan 26 2003, 11:44 AM, said:

Well what you're doing is just printing the resource (which doesn't work). What you need to do is use a fetch of some sort to get the rows from the result set.

if you want to get every row from the result at once:
$result = mysql_query($sql);
foreach($row = mysql_fetch_assoc($result)){
    print $row;
}


or you can pick through the result for a specific row:
$result = mysql_query($sql);
mysql_field_seek($result(10));
$row[10] = mysql_fetch_row($result);

mysql_field_seek($result(21));
$row[21] = mysql_fetch_row($result);



One last note you can use row/assoc/and array independent of the method you use.

mysql_fetch_assoc() will return an associative array (ie. $row["sam"], $row["mike"]) with the field names as identifiers.

mysql_fetch_row() will return a numeric array (ie. $row[0], $row[3]).

mysql_fetch_array() will return both types in one array (ie. $row["sam"],$row[0]) note that this gives you two ways to access the same data but doubles the size of your array.

Cheers Sam

Thanks for the help cookie mob... erm I'm still having trouble though...

$result3 = mysql_query("SELECT HighScore FROM $table");

$result3 = mysql_query($sql);
foreach($row = mysql_fetch_assoc($result)){
   print $row;
}


but it doesn't work?! I'm sure its just a syntax problem.. but I'm not sure which bits should be where?! :)
Was This Post Helpful? 0
  • +
  • -

#7 Cookie Mobster  Icon User is offline

  • nooneenooneenooonee
  • member icon

Reputation: 7
  • View blog
  • Posts: 4,730
  • Joined: 12-October 01

Re: mysql_query AAAAAAGGGGHHH!

Posted 26 January 2003 - 11:02 AM

Damn, I gave you incomplete code.

on the print $row line you have to specify the field identifier.

print $row["HighScore"];


Was This Post Helpful? 0
  • +
  • -

#8 Markp  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 18-January 02

Re: mysql_query AAAAAAGGGGHHH!

Posted 26 January 2003 - 11:02 AM

Ok I've now changed it to:
$result3 = mysql_query("SELECT HighScore FROM $table");

foreach($row = mysql_fetch_assoc($result3)){
   print $row;
}


But its still not working...

Could I sort the top 10 rows to give me a high score?

Something like

1. Name 1 Score 9999
2. Name 2 Score 9988
3. Name 3 Score 1200
4. Name 4 Score 500
5. Name 5 Score 30

etc...??
Was This Post Helpful? 0
  • +
  • -

#9 Markp  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 18-January 02

Re: mysql_query AAAAAAGGGGHHH!

Posted 26 January 2003 - 11:06 AM

Now I'm getting Parse errors!

$conn_id=mysql_connect ("localhost", "database", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("markp_idb"); 

print "<b>Connected!</b>";

$conn_id = mysql_query("SELECT HighScore FROM $table");

foreach($row = mysql_fetch_assoc($conn_id)){
   print $row[HighScore];
}

Was This Post Helpful? 0
  • +
  • -

#10 Cookie Mobster  Icon User is offline

  • nooneenooneenooonee
  • member icon

Reputation: 7
  • View blog
  • Posts: 4,730
  • Joined: 12-October 01

Re: mysql_query AAAAAAGGGGHHH!

Posted 26 January 2003 - 11:47 AM

d/oh i made another mistake, replace that foreach with a while and you'll be set to go. Sorry about that.
Was This Post Helpful? 0
  • +
  • -

#11 Markp  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 18-January 02

Re: mysql_query AAAAAAGGGGHHH!

Posted 26 January 2003 - 01:09 PM

Ok, I think I've got it...

print "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\"><b>Connected!</b><BR>";

$conn_id = mysql_query("SELECT HighScore , userName FROM $table");

  while($row = mysql_fetch_assoc($conn_id)){
  print ("<b>" . $row["userName"] . "</b> Score: " . $row["HighScore"] . "<BR>");
}


I added a little HTML to make it look nicer.

How can I organize it so only the top 10 scores are listed?
Was This Post Helpful? 0
  • +
  • -

#12 Cookie Mobster  Icon User is offline

  • nooneenooneenooonee
  • member icon

Reputation: 7
  • View blog
  • Posts: 4,730
  • Joined: 12-October 01

Re: mysql_query AAAAAAGGGGHHH!

Posted 26 January 2003 - 11:31 PM

Change your sql to:
SELECT HighScore , userName FROM $table LIMIT 10

(i've always wanted to use the sql bbcode.
Was This Post Helpful? 0
  • +
  • -

#13 Markp  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 18-January 02

Re: mysql_query AAAAAAGGGGHHH!

Posted 26 January 2003 - 11:51 PM

Nope :(

It still returns all the HighScore values as zero and is ordering them by the unique/key field userID.

WHY IS IT DOIIINNNGGG THISSSS!!! *must throw laptop out of the window!* :crazy: :blink: :dead: :sleepy: some music will calm me :music: :)
Was This Post Helpful? 0
  • +
  • -

#14 Markp  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 18-January 02

Re: mysql_query AAAAAAGGGGHHH!

Posted 26 January 2003 - 11:53 PM

Look here! :) Its insane!

Here's all the code if that helps :)

<?php 
 $server = "localhost"; 
 $username = "******"; 
 $database = "*****";
 $table = "******";
 $password = "********";
$hisc = "******";

print "High Score 3<BR>"; 
 mysql_connect($server,$username,$password); 
 @mysql_select_db($database) or die( "Unable to select database "); 
// $query="SELECT count(*) FROM $table ORDER BY $hisc DESC"; 
$query="SELECT HighScore , userName FROM $table LIMIT 10";
 //$result=mysql_query($query); 
$result=mysql_query($query) or die( "query failed");
  
 $i = 0; 
 while ($i < 10) 
 { 
 $highscore = mysql_result($result,$i,"HighScore");   
 $username = mysql_result($result,$i,"userName");   
 print ("<b>" . $highscore . "</b> - " . $username . "<BR>");  
 $i++; 
 }

?>

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1