sambib Posted October 26, 2006 Share Posted October 26, 2006 Hi,I'm running a query with php that drops a table (it's going to be created again and populated later in the page). It works fine as the table is dropped however a myqsl error is reported back to the page:"[i]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 '1' at line 1[/i]"any ideas why this would happen....?[code]<?php $sql = "DROP TABLE contacts"; $query = mysql_query($sql); if (@mysql_query($query)) { //set the variable to the required string $result = "<p>the query was successful: contacts table dropped.</p>"; } else { //set the variable to the required string $result = "<p>could not drop the table: " . mysql_error() . " </p>"; } echo "$result";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25138-query-runs-successfully-but-reports-a-mysql-error-solved/ Share on other sites More sharing options...
.josh Posted October 26, 2006 Share Posted October 26, 2006 ...and there are no other queries running in your script anywhere? Quote Link to comment https://forums.phpfreaks.com/topic/25138-query-runs-successfully-but-reports-a-mysql-error-solved/#findComment-114610 Share on other sites More sharing options...
sambib Posted October 26, 2006 Author Share Posted October 26, 2006 [quote author=Crayon Violent link=topic=112756.msg457802#msg457802 date=1161842364]...and there are no other queries running in your script anywhere?[/quote]No that's the only script on the page, there are others but they're commented out.the page drops the table, re-creates it and then runs an ldap query which feeds a mysql query to a database.it works really well, I just get the errors. the scripts look OK don't they I've been looking at them all day and am a bit blinded by them right now.... Quote Link to comment https://forums.phpfreaks.com/topic/25138-query-runs-successfully-but-reports-a-mysql-error-solved/#findComment-114619 Share on other sites More sharing options...
.josh Posted October 26, 2006 Share Posted October 26, 2006 well you've only shown 1 query in your code above which drops it. where's the part that recreates it and stuff?also, in the code above, you're actually running your drop table query twice. since you don't have an 'if exists' in your query, that might actually be where your error is coming from, as it is trying to drop something that doesn't exist, the 2nd time around. Quote Link to comment https://forums.phpfreaks.com/topic/25138-query-runs-successfully-but-reports-a-mysql-error-solved/#findComment-114620 Share on other sites More sharing options...
sambib Posted October 26, 2006 Author Share Posted October 26, 2006 I did try the IF EXISTS but got the same result. here's the full page without comments. like i said it does the job but still get the error messages[code]<?php $sql = "DROP TABLE IF EXISTS contacts"; $query = mysql_query($sql); if (@mysql_query($query)) { //set the variable to the required string $result = "<p>the query was successful: contacts table dropped.</p>"; } else { //set the variable to the required string $result = "<p>could not drop the table: " . mysql_error() . " </p>"; } echo "$result"; $sql = "CREATE TABLE contacts (id INT(5) UNSIGNED NOT NULL AUTO_INCREMENT, date TIMESTAMP, surname VARCHAR(32) NOT NULL, firstName VARCHAR(120) NOT NULL, email VARCHAR(120) NOT NULL, PRIMARY KEY(id))"; $query = mysql_query($sql); if (@mysql_query($query)) { //inform the table was created echo "<p>the query was successful: contacts table re-created.</p>"; } else { //inform the user that the attempt was not successful echo "<p>could not create the table: " . mysql_error() . " </p>"; }$LDAP_SERVER = "xxx.xxx.xxx.xxx";$LDAP_ROOT_DN = "ldap stuff here"; //Connect to LDAP$connect_id = ldap_connect($LDAP_SERVER);if($connect_id) { //Authenticate $bind_id = ldap_bind($connect_id, "ldap stuff here"); //Perform Search $search_id = ldap_search($connect_id, "ldap stuff here", "ldap stuff here"); //Assign Result Set to an Array $result_array = ldap_get_entries($connect_id, $search_id); } else { //Echo Connection Error echo "Could not connect to LDAP server"; } //Sort results if search was successfulif($result_array) { for($i=0; $i<count($result_array); $i++) { $format_array[$i][0] = strtolower(stripslashes($result_array[$i]["sn"][0])); $format_array[$i][1] = strtolower($result_array[$i]["gn"][0]); $format_array[$i][2] = strtolower(stripslashes($result_array[$i]["mail"][0])); } //Sort array sort($format_array, "SORT_STRING"); for($i=0; $i < count($format_array); $i++) { $sn = ucwords(addslashes($format_array[$i][0])); $gn = ucwords($format_array[$i][1]); $email = addslashes($format_array[$i][2]); if($sn && $gn && $email) { //echo "<p>data ready</p>"; $query = "INSERT INTO contacts SET surname = '$sn', firstName = '$gn', email = '$email'"; //If the query was successful if (@mysql_query($query)) { //inform the user the records were added $result = "<p>the query was successful</p>"; } else { //inform the user that the attempt was not successful $result = "<p>could not query the database: " . mysql_error() . " </p>"; } } } } else { echo "Result set empty for query: " . $ldap_query . ""; }//Close Connectionldap_close($connect_id); echo "<p>$result</p>";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25138-query-runs-successfully-but-reports-a-mysql-error-solved/#findComment-114621 Share on other sites More sharing options...
.josh Posted October 26, 2006 Share Posted October 26, 2006 okay if (@mysql_query($query)) {you are trying to run a query on a result source. should change that to $sql not $query. but you're still running the same query twice. just get rid of the previous one. Quote Link to comment https://forums.phpfreaks.com/topic/25138-query-runs-successfully-but-reports-a-mysql-error-solved/#findComment-114623 Share on other sites More sharing options...
sambib Posted October 26, 2006 Author Share Posted October 26, 2006 [quote author=Crayon Violent link=topic=112756.msg457815#msg457815 date=1161847407]okay if (@mysql_query($query)) {you are trying to run a query on a result source. should change that to $sql not $query. but you're still running the same query twice. just get rid of the previous one. [/quote]Aha....!thanks alot for your promt help I understand where I went wrong. cheers.....!!!! Quote Link to comment https://forums.phpfreaks.com/topic/25138-query-runs-successfully-but-reports-a-mysql-error-solved/#findComment-114626 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.