...

View Full Version : Unexpected $end/T_VARIABLE



iKettles
08-04-2008, 03:11 PM
Hey,
I'm using this tutorial/snippet type thing:
http://www.siteground.com/tutorials/php-mysql/display_table_data.htm
And modifying it for my own use. However I'm getting all sorts of errors thrown at me. This is the current code I'm using:


<?php
$username="username";
$password="password";
$database="databasename";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM newpilots";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$Fullname=mysql_result($result,$i,"Fullname");
$Callsign=mysql_result($result,$i,"Callsign");
$Flag=mysql_result($result,$i,"Flag);

$i++;
}
?>

I'm getting an unexpected $end on line 24 though :(
Can anyone tell what's wrong?
Thanks.

djm0219
08-04-2008, 03:14 PM
This line is missing a closing double quote.


$Flag=mysql_result($result,$i,"Flag");

iKettles
08-04-2008, 03:18 PM
Thanks so much! It's working fine now. Few more questions though, any reason why all the stuff I echo is displayed twice? Is this because of the loop? Also on my index page I include the file that connects to the database just below the body tag however when I echo $Fullname only one name shows instead of them all. Any reason for this?

djm0219
08-04-2008, 03:42 PM
This loop



$i=0;
while ($i < $num) {

$Fullname=mysql_result($result,$i,"Fullname");
$Callsign=mysql_result($result,$i,"Callsign");
$Flag=mysql_result($result,$i,"Flag");

$i++;
}
?>

will display the information as many times as there were rows found by your earlier search. You're only seeing one name because you are displaying the results of the last row retrieved. If you want all of them displayed try something like this:



mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM newpilots";
$result=mysql_query($query);

$myrow = mysql_fetch_assoc($result);

do {
$Fullname=$myrow['Fullname'];
$Callsign=$myrow['Callsign'];
$Flag=$myrow['Flag'];

} while ($myrow = mysql_fetch_assoc($result));
?>

iKettles
08-04-2008, 03:49 PM
Still seems to only want to display one result when using that code :(

djm0219
08-04-2008, 03:59 PM
How many rows are you expecting for the SELECT * FROM newpilots query? The do-while will retrieve rows and display them as long as there is data available from the query.

iKettles
08-04-2008, 04:39 PM
Think I may have found the issue. The code I'm using to insert stuff into the database uses the CURL function to pull XML data from a website. Every time I update the stats by executing my update.php file if the new pilots hasn't changed it just duplicates them and adds another row. The maximum and minimum amount of records in the table needs to be 5 with only 1 row for each record. How do I set this? Also could I use the update function to replace the current 5 records with the latest 5 records?

djm0219
08-04-2008, 04:43 PM
The number of records is something you have to control with your code. Updating the existing records should work as long as one of your columns is a "key" that you can use to relate what you already have with the data you are pulling.

iKettles
08-04-2008, 04:54 PM
The number of records is something you have to control with your code. Updating the existing records should work as long as one of your columns is a "key" that you can use to relate what you already have with the data you are pulling.
I've also tried using the replace section but it just keeps making duplicate rows. This is going to get out of hand as I'll be using a cron job to run the file every 30 minutes to update the stats database. If I use the UPDATE function will I get duplicate rows? If a new pilot joins will it still keep 5 records and not go to 6?

derzok
08-04-2008, 05:04 PM
Yes, you can use the update function to keep the existing rows. I usually avoid doing this because I like to keep a record of what has been in the database. Instead I usually set an extra field in the table "active" so that when I 'delete' things I'm actually just changing that field. Then I can do something like "SELECT mydata FROM mytable WHERE active='true'"

You could also just keep inserting things and ONLY select the latest 5 entries - that would be very easy to implement. Just append "ORDER BY column_name DESC LIMIT 5" to your query.

masterofollies
08-04-2008, 05:17 PM
Unexpected $end means your missing a single closing bracket }

iKettles
08-04-2008, 05:22 PM
Just FYI, this is the code I'm currently using to insert the data -


<?php
$ch = curl_init('http://www.vafinancials.com/xml/newpilots.php?id=504');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
foreach ($doc->pilot as $np) {


$con = mysql_connect("localhost","db_name","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("flycayco_vafsupdate", $con);

mysql_query("INSERT INTO newpilots (Callsign, FullName, Flag)
VALUES ('$np->cs.', '$np->name.', '$np->flag.')");

mysql_close($con);
}
?>


For the "ORDER BY column_name DESC LIMIT 5" does it matter what column I order by? There's no column for the date hired so I can't order by the latest entries, is there any order type within MYSQL I can order by to show only the latest entries? Also it's still choosing to only display 1 name when I echo the $Fullname variable outside of the main PHP code.

iKettles
08-04-2008, 05:57 PM
Got the update working but it seems to replace all of the records with the data of 1 of the pilots :S This is the code I'm using:


<?php
$ch = curl_init('http://www.vafinancials.com/xml/newpilots.php?id=504');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
foreach ($doc->pilot as $np) {


$con = mysql_connect("localhost","db_name","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("flycayco_vafsupdate", $con);

mysql_query("UPDATE newpilots SET Callsign = '$np->cs.', FullName = '$np->name.', Flag = '$np->flag.'");

mysql_close($con);
}
?>

I realise I haven't defined a WHERE because I want to update all of the records, not just specific ones. Is this causing it?

rafiki
08-04-2008, 06:22 PM
WHERE clause?

iKettles
08-04-2008, 06:28 PM
WHERE clause?
Yeah, that's probably what's causing it. I want to update all 5 records in the table (no specific ones) but when I don't define a WHERE clause it updates all the records with one pilot's data.

djm0219
08-04-2008, 06:32 PM
What part of the data you receive and have in the database is common between the 2 sources? If it is the FullName for example use a WHERE clause that limits the change to the entry with the same name.


mysql_query("UPDATE newpilots SET Callsign = '$np->cs.', FullName = '$np->name.', Flag = '$np->flag.' WHERE `FullName` = FullName = '$np->name.'");

(or something very close to that :))

iKettles
08-04-2008, 07:01 PM
What part of the data you receive and have in the database is common between the 2 sources? If it is the FullName for example use a WHERE clause that limits the change to the entry with the same name.


mysql_query("UPDATE newpilots SET Callsign = '$np->cs.', FullName = '$np->name.', Flag = '$np->flag.' WHERE `FullName` = FullName = '$np->name.'");

(or something very close to that :))
Really there's nothing common between the 2 sources, I want to update all the records no matter what they are. Could I possible try doing it where it updates the records that aren't on the data I get from the XML? This would also speed up the updating time.

iKettles
08-05-2008, 01:34 PM
Sorry for the bump, would just like to get this fixed :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum