For 2023 (and 2024 ...) - we are now fully retired from IT training. We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |
error message...don't know why
Posted by bschultz (bschultz), 6 March 2003 I'm getting the following error message on a script, and I can't find out why. here's the message: Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in (file name) on line 58 Here's the code: Code:<?php
if ($submit == "click"){ // The submit button was clicked! // Get the input for the game then store it in the database. $connection = mysql_connect ("", "", ""); if ($connection == false){ echo mysql_errno().": ".mysql_error()."<BR>"; exit; }
$query = "insert into game1 values ('$T1', '$T2', '$T3', '$T4', '$T5', '$T6', '$T7', '$T8',
'$T9', '$T10', '$T11', '$T12', '$T13', '$T14', '$T15', '$T16', '$T17', '$T18', '$T19', '$T20',
'$T21', '$T22', '$T23', '$T24', '$T25', '$T26', '$T27', '$T28', '$T29', '$T30', '$T31', '$T32',
'$T33', '$T34', '$T35', '$T36', '$T37', '$T38', '$T39', '$T40', '$T41', '$T42', '$T43', '$T44',
'$T45', '$T46', '$T47', '$T48', '$T49', '$T50', '$T51', '$T52', '$T53', '$T54', '$T55', '$T56',
'$T57', '$T58', '$T59', '$T60', '$T61', '$T62', '$T63', '$T64', '$T65', '$T66', '$T67', '$T68',
'$T69', '$T70', '$T71', '$T72', '$T73', '$T74', '$T75', '$T76', '$T77', '$T78', '$T79', '$T80',
'$T81', '$T82', '$T83', '$T84', '$T85', '$T86', '$T87', '$T88', '$T89', '$T90', '$T91', '$T92',
'$T93', '$T94', '$T95', '$T96', '$T97', '$T98', '$T99', '$T100', '$T101', '$T102', '$T103', '$T104', '$T105', '$T106', '$T107', '$T108', '$T109', '$T110', '$T111', '$T112', '$T113', '$T114', '$T115', '$T116', '$T117', '$T118', '$T119', '$T120', '$T121', '$T122', '$T123', '$T124', '$T125', '$T126', '$T127', '$T128', '$T129', '$T130', '$T131', '$T132', '$T133', '$T134', '$T135', '$T136', '$T137', '$T138', '$T139', '$T140', '$T141', '$T142', '$T143', '$T144', '$T145', '$T146', '$T147', '$T148', '$T149', '$T150', '$T151', '$T152', '$T153', '$T154', '$T155', '$T156', '$T157', '$T158', '$T159', '$T160', '$T161', '$T162', '$T163', '$T164', '$T165', '$T166', '$T167', '$T168', '$T169', '$T170', '$T171', '$T172', '$T173', '$T174', '$T175', '$T176', '$T177', '$T178', '$T179', '$T180', '$T181', '$T182', '$T183', '$T184', '$T185', '$T186', '$T187', '$T188', '$T189', '$T190', '$T191', '$T192', '$T193', '$T194', '$T195', '$T196', '$T197', '$T198', '$T199', '$T200', '$T201', '$T202', '$T203', '$T204', '$T205', '$T206', '$T207', '$T208', '$T209', '$T210', '$T211', '$T212', '$T213', '$T214', '$T215', '$T216', '$T217', '$T218', '$T219', '$T220', '$T221', '$T222', '$T223', '$T224', '$T225', '$T226', '$T227', '$T228', '$T229', '$T230', '$T231', '$T232', '$T233', '$T234', '$T235', '$T236', '$T237', '$T238', '$T239', '$T240', '$T241', '$T242', '$T243', '$T244', '$T245', '$T246', '$T247', '$T248', '$T249', '$T250', '$T251', '$T252', '$T253', '$T254', '$T255', '$T256', '$T257', '$T258', '$T259', '$T260', '$T261', '$T262', '$T263', '$T264', '$T265', '$T266', '$T267', '$T268', '$T269', '$T270', '$T271', '$T272', '$T273', '$T274', '$T275', '$T276', '$T277', '$T278', '$T279', '$T280', '$T281', '$T282', '$T283', '$T284', '$T285', '$T286', '$T287', '$T288', '$T289', '$T290', '$T291', '$T292', '$T293', '$T294', '$T295', '$T296', '$T297', '$T298', '$T299', '$T300', '$T301', '$T302', '$T303', '$T304', '$T305', '$T306', '$T307', '$T308', '$T309', '$T310', '$T311', '$T312', '$T313', '$T314', '$T315', '$T316', '$T317', '$T318', '$T319', '$T320', '$T321', '$T322', '$T323', '$T324', '$T325', '$T326', '$T327', '$T328', '$T329', '$T330', '$T331', '$T332', '$T333', '$T334', '$T335', '$T336', '$T337', '$T338', '$T339', '$T340', '$T341', '$T342', '$T343', '$T344', '$T345', '$T346', '$T347', '$T348', '$T349', '$T350', '$T351', '$T352', '$T353', '$T354', '$T355', '$T356', '$T357', '$T358', '$T359', '$T360', '$T361', '$T362', '$T363', '$T364', '$T365', '$T366', '$T367', '$T368', '$T369', '$T370', '$T371', '$T372', '$T373', '$T374', '$T375', '$T376', '$T377', '$T378', '$T379', '$T380', '$T381', '$T382', '$T383', '$T384', '$T385', '$T386', '$T387', '$T388', '$T389', '$T390', '$T391', '$T392', '$T393', '$T394', '$T395', '$T396', '$T397', '$T398', '$T399', '$T400',)";
$result = mysql_db_query ("game1_database", $query); if ($result){ echo "Success!"; } else{ echo mysql_errno().": ".mysql_error()."<BR>"; }
mysql_close (); } else{ echo " <html><body> rest of html goes here |
|
Any ideas? Yes, Graham, I've decided to go away from the fwrite php script, and use a mysql solution to this problem...for the rest of you, check out the php forum for the history on this. Thanks everyone! Posted by admin (Graham Ellis), 6 March 2003 I'm a little worried by the comma in: '$T400',)"; but that should generate a MySQL error rather than a PHP parse error ... have I counted right - is that line 58?? You might like to tie down the problem further by commenting out sections of code using /* to */ to isolate it to a single statement - bear in ming that "error on line 58" really means "error on or before line 58" ... the problem could be unbalanced quotes that only come to light at that point, for example Posted by bschultz (bschultz), 10 March 2003 I got that problem fixed...but I seem to have another. The table 'game1' on the database 'softball' has 400 fields named T1-T400. The following code (which I got from a mysql tutorial) doesn't work...any ideas? Code:<?php
if ($submit) {
// process form
$db = mysql_connect("", "", "");
mysql_select_db("softball_database",$db);
$sql = "INSERT INTO softball (T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,T17,T18,T19,T20,T21,T22,T23,T24,T25,T26,T27,T28,T29,T30,T31,T32,T33,T34,T35,T36,T37,T38,T39,T40,T41,T42,T43,T44,T45,T46,T47,T48,T49,T50,T51,T52,T53,T54,T55,T56,T57,T58,T59,T60,T61,T62,T63,T64,T65,T66,T67,T68,T69,T70,T71,T72,T73,T74,T75,T76,T77,T78,T79,T80,T81,T82,T83,T84,T85,T86,T87,T88,T89,T90,T91,T92,T93,T94,T95,T96,T97,T98,T99,T100,T101,T102,T103,T104,T105,T106,T107,T108,T109,T110,T111,T112,T113,T114,T115,T116,T117,T118,T119,T120,T121,T122,T123,T124,T125,T126,T127,T128,T129,T130,T131,T132,T133,T134,T135,T136,T137,T138,T139,T140,T141,T142,T143,T144,T145,T146,T147,T148,T149,T150,T151,T152,T153,T154,T155,T156,T157,T158,T159,T160,T161,T162,T163,T164,T165,T166,T167,T168,T169,T170,T171,T172,T173,T174,T175,T176,T177,T178,T179,T180,T181,T182,T183,T184,T185,T186,T187,T188,T189,T190,T191,T192,T193,T194,T195,T196,T197,T198,T199,T200,T201,T202,T203,T204,T205,T206,T207,T208,T209,T210,T211,T212,T213,T214,T215,T216,T217,T218,T219,T220,T221,T222,T223,T224,T225,T226,T227,T228,T229,T230,T231,T232,T233,T234,T235,T236,T237,T238,T239,T240,T241,T242,T243,T244,T245,T246,T247,T248,T249,T250,T251,T252,T253,T254,T255,T256,T257,T258,T259,T260,T261,T262,T263,T264,T265,T266,T267,T268,T269,T270,T271,T272,T273,T274,T275,T276,T277,T278,T279,T280,T281,T282,T283,T284,T285,T286,T287,T288,T289,T290,T291,T292,T293,T294,T295,T296,T297,T298,T299,T300,T301,T302,T303,T304,T305,T306,T307,T308,T309,T310,T311,T312,T313,T314,T315,T316,T317,T318,T319,T320,T321,T322,T323,T324,T325,T326,T327,T328,T329,T330,T331,T332,T333,T334,T335,T336,T337,T338,T339,T340,T341,T342,T343,T344,T345,T346,T347,T348,T349,T350,T351,T352,T353,T354,T355,T356,T357,T358,T359,T360,T361,T362,T363,T364,T365,T366,T367,T368,T369,T370,T371,T372,T373,T374,T375,T376,T377,T378,T379,T380,T381,T382,T383,T384,T385,T386,T387,T388,T389,T390,T391,T392,T393,T394,T395,T396,T397,T398,T399,T400)
VALUES ('$T1', '$T2', '$T3', '$T4', '$T5', '$T6', '$T7', '$T8', '$T9', '$T10', '$T11', '$T12', '$T13', '$T14', '$T15', '$T16', '$T17', '$T18', '$T19', '$T20', '$T21', '$T22', '$T23', '$T24', '$T25', '$T26', '$T27', '$T28', '$T29', '$T30', '$T31', '$T32', '$T33', '$T34', '$T35', '$T36', '$T37', '$T38', '$T39', '$T40', '$T41', '$T42', '$T43', '$T44', '$T45', '$T46', '$T47', '$T48', '$T49', '$T50', '$T51', '$T52', '$T53', '$T54', '$T55', '$T56', '$T57', '$T58', '$T59', '$T60', '$T61', '$T62', '$T63', '$T64', '$T65', '$T66', '$T67', '$T68', '$T69', '$T70', '$T71', '$T72', '$T73', '$T74', '$T75', '$T76', '$T77', '$T78', '$T79', '$T80', '$T81', '$T82', '$T83', '$T84', '$T85', '$T86', '$T87', '$T88', '$T89', '$T90', '$T91', '$T92', '$T93', '$T94', '$T95', '$T96', '$T97', '$T98', '$T99', '$T100',
'$T101', '$T102', '$T103', '$T104', '$T105', '$T106', '$T107', '$T108', '$T109', '$T110', '$T111', '$T112', '$T113', '$T114', '$T115', '$T116', '$T117', '$T118', '$T119', '$T120', '$T121', '$T122', '$T123', '$T124', '$T125', '$T126', '$T127', '$T128', '$T129', '$T130', '$T131', '$T132', '$T133', '$T134', '$T135', '$T136', '$T137', '$T138', '$T139', '$T140', '$T141', '$T142', '$T143', '$T144', '$T145', '$T146', '$T147', '$T148', '$T149', '$T150', '$T151', '$T152', '$T153', '$T154', '$T155', '$T156', '$T157', '$T158', '$T159', '$T160', '$T161', '$T162', '$T163', '$T164', '$T165', '$T166', '$T167', '$T168', '$T169', '$T170', '$T171', '$T172', '$T173', '$T174', '$T175', '$T176', '$T177', '$T178', '$T179', '$T180', '$T181', '$T182', '$T183', '$T184', '$T185', '$T186', '$T187', '$T188', '$T189', '$T190', '$T191', '$T192', '$T193', '$T194', '$T195', '$T196', '$T197', '$T198', '$T199', '$T200',
'$T201', '$T202', '$T203', '$T204', '$T205', '$T206', '$T207', '$T208', '$T209', '$T210', '$T211', '$T212', '$T213', '$T214', '$T215', '$T216', '$T217', '$T218', '$T219', '$T220', '$T221', '$T222', '$T223', '$T224', '$T225', '$T226', '$T227', '$T228', '$T229', '$T230', '$T231', '$T232', '$T233', '$T234', '$T235', '$T236', '$T237', '$T238', '$T239', '$T240', '$T241', '$T242', '$T243', '$T244', '$T245', '$T246', '$T247', '$T248', '$T249', '$T250', '$T251', '$T252', '$T253', '$T254', '$T255', '$T256', '$T257', '$T258', '$T259', '$T260', '$T261', '$T262', '$T263', '$T264', '$T265', '$T266', '$T267', '$T268', '$T269', '$T270', '$T271', '$T272', '$T273', '$T274', '$T275', '$T276', '$T277', '$T278', '$T279', '$T280', '$T281', '$T282', '$T283', '$T284', '$T285', '$T286', '$T287', '$T288', '$T289', '$T290', '$T291', '$T292', '$T293', '$T294', '$T295', '$T296', '$T297', '$T298', '$T299', '$T300',
'$T301', '$T302', '$T303', '$T304', '$T305', '$T306', '$T307', '$T308', '$T309', '$T310', '$T311', '$T312', '$T313', '$T314', '$T315', '$T316', '$T317', '$T318', '$T319', '$T320', '$T321', '$T322', '$T323', '$T324', '$T325', '$T326', '$T327', '$T328', '$T329', '$T330', '$T331', '$T332', '$T333', '$T334', '$T335', '$T336', '$T337', '$T338', '$T339', '$T340', '$T341', '$T342', '$T343', '$T344', '$T345', '$T346', '$T347', '$T348', '$T349', '$T350', '$T351', '$T352', '$T353', '$T354', '$T355', '$T356', '$T357', '$T358', '$T359', '$T360', '$T361', '$T362', '$T363', '$T364', '$T365', '$T366', '$T367', '$T368', '$T369', '$T370', '$T371', '$T372', '$T373', '$T374', '$T375', '$T376', '$T377', '$T378', '$T379', '$T380', '$T381', '$T382', '$T383', '$T384', '$T385', '$T386', '$T387', '$T388', '$T389', '$T390', '$T391', '$T392', '$T393', '$T394', '$T395', '$T396', '$T397', '$T398', '$T399', '$T400' )";
$result = mysql_query($sql);
echo "Thank you! Information entered.\n";
?>
|
|
Posted by admin (Graham Ellis), 12 March 2003 Check the return values from each of your mysql function calls to see which one it is failing in. For example, instead of writing $db = mysql_connect("", "", ""); write if (! $db = mysql_connect("", "", "")) die ("Unable to connect"); and this will help you find straight off which call to look at. I note you're using three empty strings as parameters for mysql_connect, which means that you'll be using a default mysql server, a default user name, and a default password. These are very likely to be "localhost", the name of the user under which PHP is accessing the database (likely to be something like apache or webserver - NOT your own user name!), and an empty password - although the defaults can be configured. I wonder if you've set up your softball_database under your own user name on MySQL, and don't have it set up with an "apache" or "webserver" user?? As a side issue, I'm always a little uncomfortable when I see something repeated many times in a piece of code; if you see something repeated many times, chances are that there's a much easier way to do it . Rather than writing T1 all the way through to T400, why not write a loop to put all the names into a variable, and use that? This code Code:#!/usr/bin/php -q <?php $j1 = join (", T",range(1,400)); print "(T$j1)"; ?> |
|
generates: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43, T44, T45, T46, T47, T48, T49, T50, T51, T52, T53, T54, T55, T56, T57, T58, T59, T60, T61, T62, T63, T64, T65, T66, T67, T68, T69, T70, T71, T72, T73, T74, T75, T76, T77, T78, T79, T80, T81, T82, T83, T84, T85, T86, T87, T88, T89, T90, T91, T92, T93, T94, T95, T96, T97, T98, T99, T100, T101, T102, T103, T104, T105, T106, T107, T108, T109, T110, T111, T112, T113, T114, T115, T116, T117, T118, T119, T120, T121, T122, T123, T124, T125, T126, T127, T128, T129, T130, T131, T132, T133, T134, T135, T136, T137, T138, T139, T140, T141, T142, T143, T144, T145, T146, T147, T148, T149, T150, T151, T152, T153, T154, T155, T156, T157, T158, T159, T160, T161, T162, T163, T164, T165, T166, T167, T168, T169, T170, T171, T172, T173, T174, T175, T176, T177, T178, T179, T180, T181, T182, T183, T184, T185, T186, T187, T188, T189, T190, T191, T192, T193, T194, T195, T196, T197, T198, T199, T200, T201, T202, T203, T204, T205, T206, T207, T208, T209, T210, T211, T212, T213, T214, T215, T216, T217, T218, T219, T220, T221, T222, T223, T224, T225, T226, T227, T228, T229, T230, T231, T232, T233, T234, T235, T236, T237, T238, T239, T240, T241, T242, T243, T244, T245, T246, T247, T248, T249, T250, T251, T252, T253, T254, T255, T256, T257, T258, T259, T260, T261, T262, T263, T264, T265, T266, T267, T268, T269, T270, T271, T272, T273, T274, T275, T276, T277, T278, T279, T280, T281, T282, T283, T284, T285, T286, T287, T288, T289, T290, T291, T292, T293, T294, T295, T296, T297, T298, T299, T300, T301, T302, T303, T304, T305, T306, T307, T308, T309, T310, T311, T312, T313, T314, T315, T316, T317, T318, T319, T320, T321, T322, T323, T324, T325, T326, T327, T328, T329, T330, T331, T332, T333, T334, T335, T336, T337, T338, T339, T340, T341, T342, T343, T344, T345, T346, T347, T348, T349, T350, T351, T352, T353, T354, T355, T356, T357, T358, T359, T360, T361, T362, T363, T364, T365, T366, T367, T368, T369, T370, T371, T372, T373, T374, T375, T376, T377, T378, T379, T380, T381, T382, T383, T384, T385, T386, T387, T388, T389, T390, T391, T392, T393, T394, T395, T396, T397, T398, T399, T400) Posted by bschultz (bschultz), 12 March 2003 Graham...you are a genius! It's hard to believe that I've been struggling so much with this (thinking my test table was a root user) and finding out that it wasn't! I also like your hint about the loop...one question though...how can I make it output this: ('$T1', '$T2', '$T3')? I tried this: Code:$j2= join (", $T",range(1,400)); print "($T$j2,)"; |
|
and it didn't work. Thank you though for all the help on this! As someone that's trying to learn PHP and MySQL, I must say that there are WAY too many ways to do the same thing! Thanks, again, Graham! Posted by admin (Graham Ellis), 12 March 2003 Within a double quoted string $ followed by letters is taken as a variable whose contents must be substituted in. If you really want a $ in the string, you specify \$ or you use a single quoted rather than a double quoted string. The join you're asking for is $j2a = join("', '\$T",range(1,400)); which, however, will leave you with loads of $ characters in the string and you'll want to replace the variabble name with the contents of the variable. One way (of many, of course) to do this is to use the eval function. Here's an example, with a couple of variables set up for testing: Code:$T10 = "ten"; $T123 = "one two three";
$j2a = join("', '\$T",range(1,400)); $j2 = eval("return(\"'\$T$j2a'\");"); print $j2; |
|
Running that code I got: '', '', '', '', '', '', '', '', '', 'ten', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'one two three', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' Now - I would rather have my incoming data in an array rather than 400 separate variables. Are you using PHP version 4.1 or greater? If so, they'll already by in an array called either $_POST or $_GET, and you should use that - not only will it help your coding be less complex, but it will also be good for the future when the old deprecated global variable method is removed. Posted by bschultz (bschultz), 12 March 2003 I'm using the following: Apache 1.3.24 PHP 4.3.0 MySQL 3.23.49 all under a Windows OS. It's hard to believe I'm still this confused AFTER I got this to work (with a lot of help from you)! Brian Posted by admin (Graham Ellis), 14 March 2003 How true it is that there are many ways of doing things in Open Source languages, and what a B****d that makes learning them Have a look at the "Learning to Program" board, where I've just posted a longer piece on the subject - it's much more of a general commentary by me, so really doesn't belong in the board for any particular language.
This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
|