MySQL Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

MySQL Error

Post by bawla »

im trying to get some info from a database and i keep getting this error

Code: Select all

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/krunk/public_html/gtthing/gtthing3.php on line 25
Username: 

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/krunk/public_html/gtthing/gtthing3.php on line 29
Percentage:

this is the code that im using

Code: Select all

<?php



$db = mysql_connect("localhost", "database", "password");



mysql_select_db("database",$db);



$result = mysql_query("SELECT * FROM stats",$db);



printf("Username: %s<br>\n", mysql_result($result,"username"));



printf("Percentage: %s<br>\n", mysql_result($result,"percent"));



?>
line 25 is

Code: Select all

printf("Username: %s<br>\n", mysql_result($result,"username"));
and line 29 is

Code: Select all

printf("Percentage: %s<br>\n", mysql_result($result,"percent"));
?>
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

mysql_result()
Your using the function wrong. You need a row number, try
mysql_result($result,0,"username")
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

same error even with the row number
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

This may sound pretty basic but if you run the Select statement as a MySql query itself does it work?
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

wut do you mean?

like this?

Code: Select all

$result = mysql_query("SELECT * FROM stats");
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

Do you know how to run a MySQL query in MySQL itself, either with the MySql control center or at the shell level?

I'm thinking maybe you don't have a stats table or something to that matter.
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

I just ran your modified script from Illusionist's suggestion and it appears to work fine.

You have a problem with your MySQL database. Some things to look at are:are you giving the correct database name on the line

Code: Select all

mysql_select_db("database",$db);
and does the sites table exist?
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

im using this code to send info to the database

Code: Select all

<html>
<head>
</head>
<body>



<?php



if ($submit) &#123;

  // process form

  $db = mysql_connect("localhost", "krunk_gtstat", "password");

  mysql_select_db("krunk_gtstat",$db);

  $sql = "INSERT INTO stats (username,percent) VALUES ('$username','$percent')";

  $result = mysql_query($sql);


&#125; else&#123;



  // display form







&#125; // end if



?>


<form method="GET" action="gtthing.php">
<font size=1 face="verdana" color=#000000>
Username:<br>
<input type"text" name="username" size="20" style="font-family: Arial; font-size: 8pt; color: #000000"><br>
Games Won<br>
<input type"text" name="value1" size="20" style="font-family: Arial; font-size: 8pt; color: #000000"><br>
Games lost by someone reaching 100 mojo<br>
<input type"text" name="value2" size="20" style="font-family: Arial; font-size: 8pt; color: #000000"><br>
Games lost by zero<br>
<input type"text" name="value3" size="20" style="font-family: Arial; font-size: 8pt; color: #000000"><br>
Games played<br>
<input type"text" name="value4" size="20" style="font-family: Arial; font-size: 8pt; color: #000000"><br>
<input type="reset" value="Reset Form" name="reset" style="font-family: verdana; font-size: 8pt; color: #000000">
<input type="submit" value="Submit"  name="send" style="font-family: verdana; font-size: 8pt; color: #000000">
</font>


</body>
</html>

it might be that its not sending the info and my database is empty so the error comes up
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

and this is the file with the code to get the values and username and percent

Code: Select all

<?php
$value1=$_GET&#1111;'value1'];
$value2=$_GET&#1111;'value2'];
$value3=$_GET&#1111;'value3'];
$value4=$_GET&#1111;'value4'];
$username=$_GET&#1111;'username'];
$total=(($value1 + $value2 + $value3) / $value4);
$percent=$total * 10;
echo "<font face=verdana size=2 color=red>&#123;$_GET&#1111;'username']&#125;</font><br>";
echo "<font face=verdana size=2>Games Won </font><br>";
echo "<font face=verdana size=2 color=red>&#123;$_GET&#1111;'value1']&#125;</font><br>";
echo "<font face=verdana size=2>Games lost by someone reaching 100 mojo</font> <br>";
echo "<font face=verdana size=2 color=red>&#123;$_GET&#1111;'value2']&#125;</font><br>";
echo "<font face=verdana size=2>Games lost by zero </font> <br>";
echo "<font face=verdana size=2 color=red>&#123;$_GET&#1111;'value3']&#125;</font><br>";
echo "<font face=verdana size=2>Games Played</font> <br>";
echo "<font face=verdana size=2 color=red>&#123;$_GET&#1111;'value4']&#125;</font><br>";
echo "<font face=verdana size=2>Percent of the time you/someone quit </font><br>";
echo "<font face=verdana size=2 color=red>&#123;$percent&#125;%</font>";
?>
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

I saw that you said you have phpmyadmin. Run this in the Query window and tell me what you get:

Code: Select all

SELECT * FROM stats
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

Code: Select all

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/krunk/public_html/gtthing/gtthing3.php on line 25
Username: 

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/krunk/public_html/gtthing/gtthing3.php on line 29
Percentage:
same thing happens
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Try this version, it should at least tell you what's wrong.

Code: Select all

<?php
$db = mysql_connect('localhost', 'database', 'password') or die(mysql_error());
mysql_select_db('database') or die(mysql_error());
$sql = "SELECT * FROM stats";
$result = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($result)){
  $row = mysql_fetch_assoc($result);
  echo 'Username: '.$row['username']."<br>\n";
  echo 'Percentage: '.$row['percent']."<br>\n";
}
?>
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

You shouldn't get the same thing if you run what i said in the Phpmyadmin query window. The phpmyadmin query window is totally seperate from your page. If it is set up correctly you should get all the results in the table.

In the mean time, I agree with mark. Try what he has set up and tell us what you get.
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

i got this when i did marks code

Code: Select all

Table 'krunk_gtstat.stats' doesn't exist


but the table does exist, and both username and percent exist
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Check the case of it too, eg make sure it's krunk_gstat and stats and not something like Krunk_Gstat, Stats etc..
Post Reply