...

View Full Version : Coding in variables?



nobackseat88
03-14-2008, 01:40 AM
It won't let me do this correctly? Im am trying to put a code in variables. Is this possible? Havn't gotten it to work. Let me know what you think.

What the following code is trying to accomplish is to connect to a database, get info whether or not something is set, and then depeding on whether or not it is set, display random events, where one event is trying to update something in a database.



<?php
mysql_connect("localhost", "database", "pass") or die(mysql_error());
echo "Connected to MySQL<br />";
$lala = mysql_query("SELECT * FROM event");
while($row = mysql_fetch_array($lala))
{
$txt = $row['event'];
if ($txt == "Y"){
$display = "no";
}
else {
$display = "yes";
}
if ($display == "yes"){

$quotes[] = "Random event 1
$result = mysql_query("UPDATE event SET event='Y' WHERE event='N'")
or die("Could not update");


$result = mysql_query("SELECT * FROM event WHERE event='Y'")
or die(mysql_error());";
$quotes[] = "Random Event 2";
$quotes[] = "Random Event 3";
$quotes[] = "Random Event 5";
$quotes[] = "Random Event 6";
$quotes[] = "Random Event 7";

srand ((double) microtime() * 1000000);
$randomquote = rand(0,count($quotes)-1);

echo "<p>" . $quotes[$randomquote] . "</p>";
}
else { echo "You've already got the random event!";
}
?>




I know there's something wrong this:

Line 15 $quotes[] = "Random event 1
Line 16 $result = mysql_query("UPDATE event SET event='Y' WHERE
Line 17 event='N'")
Line 18 or die("Could not update");


But I don't know what to do. If it put it as an echo statement inside the variable it messes it up more. In any event, I am getting this error:
Parse error: syntax error, unexpected T_STRING in /home/username/public_html/test/event.php on line 15

flynch01
03-14-2008, 01:58 AM
You didn't close the "" for the first quote. I don't know how you missed that. So I added "; to the end. The if($txt check is worthless because you could just put the code in if the text equals Y. SO I removed that too. Here you go:


<?php
mysql_connect("localhost", "database", "pass") or die(mysql_error());
echo "Connected to MySQL<br />";
$lala = mysql_query("SELECT * FROM event");
while($row = mysql_fetch_array($lala))
{
$txt = $row['event'];
if ($txt == "Y"){
$quotes[] = "Random event 1
$result = mysql_query("UPDATE event SET event='Y' WHERE event='N'")
or die("Could not update");

$result = mysql_query("SELECT * FROM event WHERE event='Y'")
or die(mysql_error());";
$quotes[] = "Random Event 2";
$quotes[] = "Random Event 3";
$quotes[] = "Random Event 5";
$quotes[] = "Random Event 6";
$quotes[] = "Random Event 7";

srand ((double) microtime() * 1000000);
$randomquote = rand(0,count($quotes)-1);

echo "<p>" . $quotes[$randomquote] . "</p>";
} else {
echo "You've already got the random event!";
}
}
?>

nobackseat88
03-14-2008, 02:01 AM
Okay, thanks will try it. The ending quote " slipped right past me. The mysql queries are to update the database when that random event is pulled from the code and displayed.

nobackseat88
03-14-2008, 02:05 AM
Still doesn't work!

Parse error: syntax error, unexpected T_STRING in /home/username/public_html/test/event.php on line 10

<?php
mysql_connect("localhost", "database", "pass") or die(mysql_error());
echo "Connected to MySQL<br />";
$lala = mysql_query("SELECT * FROM event");
while($row = mysql_fetch_array($lala))
{
$txt = $row['event'];
if ($txt == "Y"){
$quotes[] = "Random event 1 <<< ERROR!
$result = mysql_query("UPDATE event SET event='Y' WHERE event='N'")
or die("Could not update");

$result = mysql_query("SELECT * FROM event WHERE event='Y'")
or die(mysql_error());";
$quotes[] = "Random Event 2";
$quotes[] = "Random Event 3";
$quotes[] = "Random Event 5";
$quotes[] = "Random Event 6";
$quotes[] = "Random Event 7";

srand ((double) microtime() * 1000000);
$randomquote = rand(0,count($quotes)-1);

echo "<p>" . $quotes[$randomquote] . "</p>";
} else {
echo "You've already got the random event!";
}
}
?>


Where the <<< ERROR text is, the words to the left can't be in the middle of the php coding, doesn't work, generates error. And when I try to put it in a echo statement in the variable, it messes it up, as I said in the first post. So I don't know how to do this. :(

flynch01
03-14-2008, 02:09 AM
CLOSE IT! :D

$quotes[] = "Random event 1";


";

";

";

";

nobackseat88
03-14-2008, 02:12 AM
I want to include this code in the quotes[] variable!!!

$result = mysql_query("UPDATE event SET event='Y' WHERE event='N'")
or die("Could not update");

$result = mysql_query("SELECT * FROM event WHERE event='Y'")
or die(mysql_error());";

flynch01
03-14-2008, 02:15 AM
Code in quotes won't execute, but you can try it if you like. My point remains the same though. Theres no end quote at the end after die:


$quotes[] = "Random event 1 <<< ERROR!
$result = mysql_query("UPDATE event SET event='Y' WHERE event='N'")
or die("Could not update")";//Added " before ;

And considering the code is inside a quote, you need to escape the '"' so it's actually:



$quotes[] = "Random event 1 <<< ERROR!
$result = mysql_query(\"UPDATE event SET event='Y' WHERE event='N'\")
or die(\"Could not update\")";//Added " before ;

All it will do is echo that code though, it wont return a result.

nobackseat88
03-14-2008, 02:17 AM
so it's not possible for it to contact the database and such inside a random event variable?

flynch01
03-14-2008, 02:20 AM
Oh I just saw how you included both mysql in there so my quote was wrong, it's because you weren't escaping your "s. Anyway no you can't contact the database inside a variable. You can however, just add the code after the quotes. Like so:




$quotes[] = "Random event 1";
$quotes[] = "Random Event 2";
$quotes[] = "Random Event 3";
$quotes[] = "Random Event 5";
$quotes[] = "Random Event 6";
$quotes[] = "Random Event 7";
mysql_query("UPDATE event SET event='Y' WHERE event='N'")
or die("Could not update");
mysql_query("SELECT * FROM event WHERE event='Y'")
or die(mysql_error());


That will contact the database.

nobackseat88
03-14-2008, 02:22 AM
Okay, thanks anyway. I only wanted the code to excute in that random event only. I've seen it before tho.

nobackseat88
03-14-2008, 02:23 AM
What if I put it in an if statement, like if the random event chosen was the mysql one, then excute the code!

EDIT: Nevermind, it's in an array.

flynch01
03-14-2008, 02:24 AM
I'm confused as to what you mean. But you said nevermind, if you want more help though im watching the thread.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum