Computer Science Canada

Need help regarding this simple PHP/MySQL script

Author:  Velius [ Tue Mar 06, 2007 5:16 pm ]
Post subject:  Need help regarding this simple PHP/MySQL script

http://www.braffo.com/SPARTA.zip <=Link to my code

This program is suppose to add a "fact" to the database, but I get an error on line 6 stating I cant reassign $this on line 6.

If anyone could help me out, it would be much appreciated.

Another question, how can I echo out a random entry from the DB table?

Author:  PaulButler [ Tue Mar 06, 2007 6:51 pm ]
Post subject:  Re: Need help regarding this simple PHP/MySQL script

Velius @ Tue Mar 06, 2007 5:16 pm wrote:
http://www.braffo.com/SPARTA.zip <=Link to my code

This program is suppose to add a "fact" to the database, but I get an error on line 6 stating I cant reassign $this on line 6.


$this is a special variable in PHP that refers to the current object. Use another variable name.

Velius @ Tue Mar 06, 2007 5:16 pm wrote:

Another question, how can I echo out a random entry from the DB table?


Order them by the RAND() function

code:

SELECT [fieldnames] FROM [database] ORDER BY RAND() LIMIT 1;


or in your case:

code:

SELECT * FROM Chuck_Norris_sucks ORDER BY RAND() DESC LIMIT 1

Author:  Velius [ Tue Mar 06, 2007 10:02 pm ]
Post subject:  Re: Need help regarding this simple PHP/MySQL script

Thanks alot for your help~

but now I have a second problem. Whenever I try to add a fact here...

http://braffo.com/guest.php

Nothing happens, the fact counter doesnt go up like it should (its not writing anything to the DB), and my suspicion is that it has something to deal with the post script. Even if I type nothing, I dont get an error message like I should be getting.

code:
<?php
if (($REQUEST_METHOD=='POST')) {
for(reset($HTTP_POST_VARS);
                      $key=key($HTTP_POST_VARS);
                      next($HTTP_POST_VARS)) {
    $um = addslashes($HTTP_POST_VARS[$key]);
    $um = strtr($um, ">", " ");
    $um = strtr($um, "<", " ");
    $um = strtr($um, "|", " ");
    $$key = $fuck;
  }
   if ($message ) {$query = "INSERT INTO Chuck_Norris_sucks ";
    $query .= " (Facts) ";
    $query .= " values('$message')";
    mysql_pconnect("mysqlfire.lol.com","admin","password")
                   or die("Unable to connect to SQL server");
    mysql_select_db("fire") or die("Unable to select database");
    mysql_query($query) or die("Insert Failed!");
         } else {
         $notall = 1;
  }
}
?>


here's the second part

code:
<? if ($notall == 1) { ?>
<P><FONT COLOR="red">Please answer all fields</FONT></P>
<? } ?>
<FORM METHOD="POST" ACTION="guest.php">
<PRE>
Enter Message:
<TEXTAREA NAME="message" COLS="40" ROWS="8" WRAP="Virtual">
<? echo $message; ?>
</TEXTAREA>
<INPUT TYPE="submit" VALUE="Add">
</PRE>
</FORM>


code:
<?php
mysql_pconnect("mysqlfire.lol.com","admin","password")
                   or die("Unable to connect to SQL server");
mysql_select_db("fire") or die("Unable to select database");
$query = "SELECT * FROM Chuck_Norris_sucks ORDER BY RAND() DESC LIMIT 1";
echo $query;
?>


The output of the code is "SELECT * FROM Chuck_Norris_sucks ORDER BY RAND() DESC LIMIT 1". Something tells me I dont have the right idea.

Again, sorry for my noobishness. D:

Author:  octopi [ Tue Mar 06, 2007 10:47 pm ]
Post subject:  Re: Need help regarding this simple PHP/MySQL script

you need to use to execute your query, then you can access the valus returned from something like


code:

$res=mysql_query($query);
while($row=mysql_fetch_row($res)) {
  print_r($row); //will show the varibles, you can use one like $row[0];
}


: