Jump to content

Archived

This topic is now archived and is closed to further replies.

sambib

query runs successfully but reports a mysql error.....? *SOLVED*

Recommended Posts

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]

Share this post


Link to post
Share on other sites
[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....

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 successful
if($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 Connection
ldap_close($connect_id); 
echo "<p>$result</p>";
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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.....!!!!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.