Well, if it is exactly as you posted it, remove the ‘52’ from line #52. It is an extra line number!
Really!, Now you’re just breaking things out of the if() (cause we told you to) but you’re not rewriting the code to make sense. You’re still leaving the ! at the start of this line like it’s supposed to still be in the if()
[php]!$mysqli->query("CREATE TABLE test2[/php]
And you don’t have a ; at the end of the query code block, so it sees the next var and throws the error.
PHP works very logically, you need to think logically. Plan out the path that the script needs to take in order to accomplish the task. Like, this thing needs to happen first, then this, then if this happens do this.
Like ErnieAlex has said a few times and you still have not given us a reason for, why do you have all 3 of these queries in the first place. I can’t even fathom a instance that you should be dropping a table and instantly creating it again only to insert data to it. That makes absolutely no sense, unless you can give us a very specific reason for it. Your logic is highly flawed!
I totally agree with Fatsol…
Please explain why you delete the table and then create it? That is mostly the part we do not understand!
(3rd try to answer your question. I get part way through an answer, need to go get some information, and can’t get back into this program again. Don’t know how to do that yet. Now I have 2 copies of Fiirefox open; one with this reply and one with your question – seems to be working.)
The nested ||'s script was lifted from a tutorial mysqli site. I left in the deletion and creation of a table. It is not needed, but it simplified the unix display of a table to one row of data, which keeps the table headings visible over repeated test runs.
I’ve restarted the process of copying a valid script and modifying it. Here’s my current source:
http://www.phphelp.com/forum/mysql-database/(dispicable-err)-syntax-error-unexpected-t_variable/new/?topicseen#new
The script follows:
Here’s a fresh start at its modification: This abbreviated script tests only the first stage – accessing the DB, which throws an error …
<?php include_once ($_SERVER['DOCUMENT_ROOT'] . '/cgi-bin/cnnct2mysqli.php'); //In cnnct2mysqli.php the 4 parameters have single quote marks. $mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $mysqli->close(); ?>…“Connect failed: Access denied for user ‘ntelleckdb’.”
The line of new mysqli() with 4 parameters was typed with `, ', ", and no superscript marks. All produced connection failures naming one of the 4 parameters (usually server or user).
Of course, I’ve cut/pasted the include_once statement from my usual (working) scripts yet get this error. I’ve also replaced the include_once statement with 4 individual definition statements. No luck their either.
I’ve sent an email to my isp asking for corroboration of the 4 credential parameters. I’m waiting to hear.
I appreciate your patience.
usit
Update 9:45am 25July14
//My ISP informs: “Bear in mind that you are using mysql_select_db when opening the connection with mysqli.”
//The PHP manual says, re; ‘mysql_select_db’: “This extension is deprecated as of PHP 5.5.0.
// … The MySQLi or PDO_MySQL extension should be used.”
//I don’t see that I’m using mysql_select_db anywhere. Does $mysqli=new mysqli() not work?
//I know I’m confused, but haven’t discovered the root cause.
Here’s my latest test.
[php]include_once ($_SERVER[‘DOCUMENT_ROOT’] . ‘/cgi-bin/cnnct2mysqli.php’);
$mysqli = new mysqli($DBServer, $DBUser, DBPass, $DBName);
/* check connection */
if (mysqli_connect_errno()) {
printf(“Connect failed: %s\n”, mysqli_connect_error());
exit();
}
$mysqli->close();[/php]
usit Another week of going in circles.
You’re mixing oop mysqli and procedural mysqli. You’re connecting using oop but then try to use the procedural mysqi_connect_errono.
(2nd time on this reply. Lost 1st try when trying to check on an email announcing fastsol’s reply).
Thanks fastsol. I know better than mixing oo and procedural mysqli, but didn’t recognize that I had fallen into that trap. Thanks too for the reference. That’s a new one for me.
I’ve managed to get past the error in accessing the mysqli data base. Then two steps later it’s back. (Connect Error: 1045) – disallowed access. This time I’m not spending two days chasing corroboration of my mysqli credentials, since, although the error message pointed to that cause, it turned out to be a red herring.
Here’s what I have done – from the web …
<?php //testCreatInsrtBindExec.php include_once ($_SERVER['DOCUMENT_ROOT'] . '/cgi-bin/cnnct2mysqli.php'); //Previous failure ... //$mysqli = new mysqli($DBServer, $DBUser, DBPass, $DBName); //$link = mysqli_connect("$DBServer", "$DBUser", "$DBPass", "$DBName") ; //mysqli_error($link); //Latest trial (from the web) to get past mysqli connection ... //http://php.net/manual/en/mysqli-stmt.bind-param.php $mysqli = @new mysqli($DBServer, $DBUser, DBPass, $DBName); //Connect error 1045 if ($mysqli->connect_errno) { die('Connect Error: ' . $mysqli->connect_errno); } $code = 'DEU'; $language = 'Bavarian'; $official = "F"; $percent = 11.2; $stmt = $mysqli->prepare("INSERT INTO test2 VALUES (?, ?, ?, ?)"); $stmt->bind_param('sssd', $code, $language, $official, $percent); $stmt->execute(); /* check connection */ //if (mysqli_connect_errno()) { // printf("Connect failed: %s\n", mysqli_connect_error()); exit(); $mysqli->close(); ?>Ever appreciating your insights.
usit
If I can get this recommended code to work, I’ll switch to the original insert_into db code.
From what I’m reading in the manual, the @ here [php]@new[/php]Will produce the error code you have. Get rid of the @, it’s a terrible thing to use anyway. All it does it stop errors from showing up in the code. They only had it in the example code to show how it works to produce errors, not for real world use.
Yes, fastsol, but, I removed the @ to no avail. These seemingly misleading error messages that point to bad connection data but are only red herrings, are what caused me to use the word despicable. I would have removed the @ for php reasons but I’ve become paranoid trying to adapt to mysqli.
I’m presently searching the web for other tutorials.
Thanks,
usit
Check out phpacademy on youtube. Alex on there is very good and uses real world examples.
Update. Did it again – mixed oo and procedural syntax. So, added L.27 and got Access Denied errors. Tried different superscript marks on the 4 $DBx parameters in the included script and the usual errors (see Lines 10-13).
Hunting for the next correction.
usit
Sorry, forgot to include the script. Here it is
<?php //testCreatInsrtBindExec.php include_once ($_SERVER['DOCUMENT_ROOT'] . '/cgi-bin/cnnct2mysqli.php'); //Latest trial (to get past mysqli connection ... //http://php.net/manual/en/mysqli-stmt.bind-param.php $mysqli = new mysqli( $DBServer , $DBUser , DBPass , $DBName ); //L.9. Connect error 1045 //10. Access denied for user 'u-sit'@'localhost' //11. changed the 4 $DBx parameters to '$DBx' and got 'Unknown MySQL server host '$DBServer' (25) ' //12. changed '$DBx' to "$DBx" and got ... Access denied for user 'ntelleckdb'@'lsh1012.lsh.chicago.hostway //13. changed "$DBx" to $DBx and got ... Access denied for user 'ntelleckdb'@'lsh1012.lsh.chicago.hostway if ($mysqli->connect_errno) { die('Connect Error: ' . $mysqli->connect_errno); } $code = 'DEU'; $language = 'Bavarian'; $official = "F"; $percent = 11.2; $stmt = $mysqli->prepare("INSERT INTO test2 VALUES (?, ?, ?, ?)"); mysqli::query ($stmnt , $resultmode = MYSQLI_STORE_RESULT ); //L.27 //adding the above line caused 'Access denied for user 'u-sit'@'localhost'' $stmt->bind_param('sssd', $code, $language, $official, $percent); $stmt->execute(); exit(); $mysqli->close(); ?>OMG, really, you keep doing this, stop mixing up how you are running queries. Line 27 obviously won’t work cause a: you’re trying to run it with a var called $stmnt when the line above clearly uses $stmt, b: you’re using a prepare on $stmt so you MUST use the execute() which you already have a few lines lower.
Finally found the cause of error pointing to the mysqli credentials. Took the two lines L8 and L9 (below) from a working script and compared them with L4 and L5 of the previous script (reply above). L8 and L9 throw no errors whereas L4 and L5 throw Access denied. See testCompare.php
Now I can get back to the above script.
<?php //testCompare.php include_once ($_SERVER['DOCUMENT_ROOT'] . '/cgi-bin/cnnct2mysqli.php'); $mysqli = new mysqli($DBServer, $DBUser, $DBName, $DBPass ); //L4,L5 Access denied for user 'ntelleckdb'@'lsh1009.lsh.chicago.hostway Connect Error: 1045 // include_once ($_SERVER['DOCUMENT_ROOT'] . '/cgi-bin/cnnct2mysqli.php');//from working test4 // $mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName); //from working test4 //L8,L9 No errors thrown if ($mysqli->connect_errno) { die('Connect Error: ' . $mysqli->connect_errno); } $mysqli->close(); ?>Yes, I had found and corrected the $stmnt error and corrected it. Then the next day started working on the uncorrected version and never saw it.
u-sit
Back on the debug mysqli track.
Here’s the latest script, w/ L.17 commented out. This script reports 'Select returned 0 rows. ’ L. 23
I’m expecting 4 rows. Havn’t figured out what happened.
Thanks
USIT
Finally, I’m pleased to report that my several test scripts are working. Thanks to your help.
When I put the full table into the last working test script I get ‘syntax error, unexpected T_STRING’. This error I’m rather familiar with. However, in this case there are no missing ‘;’, so I focused on the db definition. As far as I can tell it looks OK. However, the compiler doesn’t seem to agree. Can anyone see the problem?
<?php // Apr2013_En/TmpTestFilesApr2013_En/TmpTestFiles/CreaTbl_test5.php include_once ($_SERVER['DOCUMENT_ROOT'] . '/cgi-bin/cnnct2mysqli.php'); $mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } //error: 'syntax error, unexpected T_STRING ' L.10 is the following line CREATE TABLE IF NOT EXISTS `ntelleckdb`.`test5` ( `id` INT(6) unsigned NOT NULL AUTO_INCREMENT, `amount` char(6) YES NULL DEFAULT '', `item_name` varchar(10) YES NULL, `email` varchar(40) YES NULL, `first_name` varchar(20), `last_name` varchar(40), `address1` varchar(100) YES NULL, `address2` varchar(100) YES NULL, `city` varchar(50) YES NULL, `state` varchar(6) YES NULL, `country` varchar(6) YES NULL, `zip` varchar(12) YES NULL, `mc_gross` varchar(6) YES NULL, `mc_fee` varchar(6) YES NULL, `tax` varchar(6) YES NULL, `invoiceNo` varchar(20) YES NULL, `payment_date` varchar(20) YES NULL, `s_h` varchar(6) YES NULL, `affiliation` varchar(40) YES NULL, `e_anncmnt` varchar(10) YES NULL, `hit` varchar(100) YES NULL, `USIT_txt` unum('True','False') YES FALSE, `updated` timestamp DATE, `created` timestamp YES 0000-00-00 00:00:00, PRIMARY KEY (invoiceNO), INDEX (id) ) //26 var, $amount = 1.00; //d $item_name = 'book'; //s $email = '[email protected]'; //s $first_name = 'Fname'; //s $last_name = 'LongerLastNames'; //s $address1 = 'Addr01'; //s $address2 = 'Addr02'; //s $city = 'cty'; //s $state = 'MI'; //s $country = 'US'; //s $zip = 48138; //i $mc_gross = 0.00; //d $mc_fee = 0.00; //d $tax = 0.00; //d $invoiceNo = 140630; //i $payment_date = 'yyyy-mm-dd'; //s $s_h = 0.00; //d $affiliation = 'retired'; //s $e_announcement = true; //s $hit = 123; //i $USIT_txt = 'true'; //s $quantity = 1; //i 22 vars to here $stmt = $mysqli->prepare("INSERT INTO test5 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); //var_dump($stmt); $stmt->bind_param('dsssssssss iddd is dss is i', `$amount`, `$item_name`, `$email`, `$first_name`, `$last_name`, `$address1`, `$address2`, `$city`, `$state`, `$country`, `$zip`, `$mc_gross`, `$mc_fee`, `$tax`, `$invoiceNo`, `$payment_date`, `$s_h`, `$affiliation`, `$e_anncmnt`, `$hit`, `$USIT_txt`, `$quantity`); $stmt->execute(); $atmt->close(); $mysqli->close(); exit; ?>This is where my original problem began.
Thanks for your interest.
usit
You can’t just put the CREATE TABLE string in the middle of the script, you need to put it inside a query() or assign it to a var or something. That string of text means nothing to php when it’s just sitting in the open like you have it.
Oh and also, STOP placing code in the forum unless you are going to use the proper code tags. It’s the PHP button in the reply menus.
(Think I’ve been here twice – but, still don’t find the error) Here’s the error … ‘Error creating table: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'YES NULL DEFAULT ‘’, item_name
char(10) YES NULL, email
char(40) YE’ at line 3’
‘line 3’ isn’t correct for the stated error. Those words occur on L.13.
[php] include_once ($_SERVER[‘DOCUMENT_ROOT’] . ‘/cgi-bin/cnnct2mysqli.php’);
$con = mysqli_connect($DBServer, $DBUser, $DBPass, $DBName); //this sequence req'd
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$sql="CREATE TABLE test5(
`id` INTEGER(6) UNSIGNED NOT NULL AUTO_INCREMENT,
`amount` char(6) YES NULL DEFAULT '', //L.13
`item_name` char(10) YES NULL,
`email` char(40) YES NULL,
`first_name` char(20),
`last_name` char(40),
`address1` char(100) YES NULL,
`address2` char(100) YES NULL,
`city` char(50) YES NULL,
`state` char(6) YES NULL,
`country` char(6) YES NULL,
`zip` char(12) YES NULL,
`mc_gross` char(6) YES NULL,
`mc_fee` char(6) YES NULL,
`tax` char(6) YES NULL,
`invoiceNo` char(20) YES NULL,
`payment_date` char(20) YES NULL,
`s_h` char(6) YES NULL,
`affiliation` char(40) YES NULL,
`e_anncmnt` char(10) YES NULL,
`hit` varchar(100) YES NULL,
`USIT_txt` unum('True','False') YES FALSE,
`updated` timestamp DATE,
`created` timestamp YES 0000-00-00 00:00:00,
PRIMARY KEY (invoiceNO),
INDEX (id)
)";
if (mysqli_query($con,$sql)) {
echo “Table test5 created successfully”;
} else {
echo "Error creating table: " . mysqli_error($con);
}
mysqli_close($con);
exit;[/php]
What’s wrong here?
Thanks
usit
Look at the syntax from your 2nd post in this topic.
Yes. Thanks fastsol.
Found my notes also. FIxed it and it’s now working.
So, I’ll close this ticket and thank you all for the help.
usit