Jump to content


Photo

A little MySQL trouble


  • Please log in to reply
17 replies to this topic

#1 Phil

Phil

    Force Majeure

  • Network Leaders
  • 7,976 posts
  • Location:Switzerland
  • Projects:Revora, C&C:Online
  •  Thought Police
  • Division:Revora
  • Job:Network Leader
  • Donated
  • Association

Posted 15 January 2007 - 10:14 PM

Well, I'm learning how to use PHP and MySQL. PHP goes well, now I'm stuck with MySQL, using some script (which is used in the book I read) to send the query via msqli, which works perfectly. But I encountered problems when trying to insert strings.

Let's say I use this command:

INSERT INTO test1 SET name='DLotS';
then I get the following error:

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 '\'DLotS\'' at line 1


Judging from my pretty non-existing knowledge about MySQL, I'd say it's a configuration problem with masking characters or so, but tbh I have no idea how to solve this... Oh and yes, name is defined as varchar. Can anyone help me or at least try to?

Thanks in advance,
DLotS

Edited by Dark Lord of the Sith, 17 January 2007 - 08:19 PM.

revorapresident.jpg
My Political Compass

Sieben Elefanten hatte Herr Dschin
Und da war dann noch der achte.
Sieben waren wild und der achte war zahm
Und der achte war's, der sie bewachte.


#2 Destroyer

Destroyer

    Byte me

  • Members
  • 2,305 posts
  • Location:My back pocket
  • Projects:Having fun
  •  Just this guy, you know?

Posted 15 January 2007 - 11:49 PM

I believe you're confusing two commands.

You could do
INSERT INTO test1 (name) VALUES ('DLotS');

Or, if you already had a name in there, but wanted to change all names to DLotS
UPDATE test1 SET name='DLotS';

Or to specifically swap Jesus with DLotS
UPDATE test1 SET name='DLotS' WHERE name='Jesus';

I hope I got all those right.

#3 Mastermind

Mastermind

    Server Technician

  • Undead
  • 7,014 posts
  • Location:Cambridge, MA
  • Projects:MasterNews 3
  •  The Man Behind the Curtain

Posted 16 January 2007 - 01:13 AM

Yeah, those all look correct.
Posted Image

Well, when it comes to writing an expository essay about counter-insurgent tactics, I'm of the old school. First you tell them how you're going to kill them. Then you kill them. Then you tell them how you just killed them.

Too cute! | Server Status: If you can read this, it's up |

#4 Phil

Phil

    Force Majeure

  • Network Leaders
  • 7,976 posts
  • Location:Switzerland
  • Projects:Revora, C&C:Online
  •  Thought Police
  • Division:Revora
  • Job:Network Leader
  • Donated
  • Association

Posted 16 January 2007 - 03:24 PM

According to my book, my version works too, along with the INSERT [...] VALUES [...]. Just to be sure I tested the other one too, but still it's the same result.

My command (book is German, that's why the names are German too):

INSERT INTO staedte (postleitzahl, name) VALUES ('20095', 'Hamburg'), ('59821', 'Arnsberg');
and the error I get:

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 '\'20095\', \'Hamburg\'), (\'59821\', \'Arnsberg\')' at line 1


Am I still doing something wrong?

revorapresident.jpg
My Political Compass

Sieben Elefanten hatte Herr Dschin
Und da war dann noch der achte.
Sieben waren wild und der achte war zahm
Und der achte war's, der sie bewachte.


#5 Blodo

Blodo

    The one who disagrees

  • Project Team
  • 3,002 posts
  • Location:Eastern Europe
  • Projects:siteMeister, Mental Omega
  •  The wise guy

Posted 16 January 2007 - 05:40 PM

Tried removing the ';' from the end of the query? Also try and add just one at a time and see how that goes. I know, pointless suggestions but tbh it's a lot of trial and error.

ARGUMENT FROM CREATION, a.k.a. ARGUMENT FROM PERSONAL INCREDULITY (I)
(1) If evolution is false, then creationism is true, and therefore God exists.
(2) Evolution can't be true, since I lack the mental capacity to understand it; moreover, to accept its truth would cause me to be uncomfortable.
(3) Therefore, God exists.


#6 Taurus85

Taurus85
  • Project Team
  • 68 posts
  • Location:Finland
  • Projects:Robot Storm testing, YR:Warzone

Posted 16 January 2007 - 06:42 PM

According to my book, my version works too, along with the INSERT [...] VALUES [...]. Just to be sure I tested the other one too, but still it's the same result.

My command (book is German, that's why the names are German too):

INSERT INTO staedte (postleitzahl, name) VALUES ('20095', 'Hamburg'), ('59821', 'Arnsberg');
and the error I get:

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 '\'20095\', \'Hamburg\'), (\'59821\', \'Arnsberg\')' at line 1


Am I still doing something wrong?

You can put only one value at time.. Edit that like this:

In php:
INSERT INTO staedte (postleitzahl, name) VALUES ('20095', 'Hamburg');
INSERT INTO staedte (postleitzahl, name) VALUES ('59821', 'Arnsberg');

MySql console:
INSERT INTO `staedte` (postleitzahl, name) VALUES ('20095', 'Hamburg');
INSERT INTO `staedte` (postleitzahl, name) VALUES ('59821', 'Arnsberg');

Edited by Taurus85, 16 January 2007 - 06:51 PM.

"Hasty can't do, skilled don't rush"
"Life is like a camel; you can make it do anything except back up." - Marcelene Cox
"It's not who I am underneath, but what I do that defines me." - Batman begins

Posted Image

#7 Blodo

Blodo

    The one who disagrees

  • Project Team
  • 3,002 posts
  • Location:Eastern Europe
  • Projects:siteMeister, Mental Omega
  •  The wise guy

Posted 16 January 2007 - 07:21 PM

It CAN insert multiple rows.

http://dev.mysql.com.../en/insert.html for reference.
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

ARGUMENT FROM CREATION, a.k.a. ARGUMENT FROM PERSONAL INCREDULITY (I)
(1) If evolution is false, then creationism is true, and therefore God exists.
(2) Evolution can't be true, since I lack the mental capacity to understand it; moreover, to accept its truth would cause me to be uncomfortable.
(3) Therefore, God exists.


#8 Phil

Phil

    Force Majeure

  • Network Leaders
  • 7,976 posts
  • Location:Switzerland
  • Projects:Revora, C&C:Online
  •  Thought Police
  • Division:Revora
  • Job:Network Leader
  • Donated
  • Association

Posted 16 January 2007 - 07:40 PM

Well, after kinda jumping over my own stupidity I finally tried using the direct way (cmd -> mysql.exe) and noticed that it's no problem to INSERT data there. So I guess there must be some problem with the PHP script I'm using (maybe a typo?). I'll try using the version from the book's CD. If it still doesn't work, I'll get back to you guys.

Thanks for all the help, it's really appreciated if one is still a n00b in these things :)

revorapresident.jpg
My Political Compass

Sieben Elefanten hatte Herr Dschin
Und da war dann noch der achte.
Sieben waren wild und der achte war zahm
Und der achte war's, der sie bewachte.


#9 Taurus85

Taurus85
  • Project Team
  • 68 posts
  • Location:Finland
  • Projects:Robot Storm testing, YR:Warzone

Posted 16 January 2007 - 07:58 PM

Can you post that script what you have been trying to use ?
"Hasty can't do, skilled don't rush"
"Life is like a camel; you can make it do anything except back up." - Marcelene Cox
"It's not who I am underneath, but what I do that defines me." - Batman begins

Posted Image

#10 Phil

Phil

    Force Majeure

  • Network Leaders
  • 7,976 posts
  • Location:Switzerland
  • Projects:Revora, C&C:Online
  •  Thought Police
  • Division:Revora
  • Job:Network Leader
  • Donated
  • Association

Posted 16 January 2007 - 08:04 PM

Yes, after not finding this part of the script on the CD I'll have no other choice than bugging you guys again. And yeah, just ignore the German stuff. I must add that creating and dropping databases and tables was absolutley no problem.

<?php

 include_once('user.php');

function table_output($result)
{
	$head = true;
	echo '<table border="1"';
	
	while($row = mysqli_fetch_assoc($result))
	{
		if ($head == true)
		{
			echo '<tr>';
			foreach($row as $key=>$val)
			{
				echo '<th>'.$key.'</th>';
			}
			echo '</tr>';
			reset($row);
			$head = false;
		}
		echo '<tr>';
		foreach($row as $val)
		{
			echo '<td>'.$val.'</td>';
		}
		echo '</tr>';
	}
	echo '</table>';
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>
<title> MySQL Console</title>
</head>

<body>

<h1>MySQL Konsole</h1>

<form action="" method="post">
<textarea name="query" cols="50" rows="10">
<?php echo htmlspecialchars($_POST['query']); ?>
</textarea>
<br />
<input type="reset" name="reset" value="Reset" />
<input type="submit" name="submit" value="Submit" />
</form>
<hr />

<h3> Log: </h3>

<?php

$phpmysql_database = 'adresskartei1';


if(isset($_POST['query']) && !empty($_POST['query']))
{
	//create connection
	$link = mysqli_connect('localhost', $phpmysql_user, $phpmysql_pwd, $phpmysql_database);
	
	if(!$link)
	{
		echo '<h3>Verbindungsfehler: '.mysqli_connect_error().'</h3>';
		die();
	}
	else
	{
		//submit request
		$result = mysqli_query($link, $_POST['query']);
		
		// event processing
		if(is_object($result))
		{
			// SELECT
			table_output($result);
			echo 'Zeilen im Ergebnis: '.mysqli_num_rows($result);
		}
		elseif($result == true && mysqli_affected_rows($link) != -1)
		{
			// INSERT / UPDATE / DELETE
			echo 'Zeilen veraendert: '.mysqli_affected_rows($link);
		}
		else
		{
			// error
			echo '<h3>Fehler: '.mysqli_error($link).'</h3>';
		}
		
		// end connection
		mysqli_close($link);
		
	}
	
	
}
else
{
	echo '<h3>Es wurde keine MySQL-Anfrage definiert!</h3>';
}

?>

I can only hope I get to solve this, or else it'll be really crappy to continue learning...

revorapresident.jpg
My Political Compass

Sieben Elefanten hatte Herr Dschin
Und da war dann noch der achte.
Sieben waren wild und der achte war zahm
Und der achte war's, der sie bewachte.


#11 Taurus85

Taurus85
  • Project Team
  • 68 posts
  • Location:Finland
  • Projects:Robot Storm testing, YR:Warzone

Posted 16 January 2007 - 08:43 PM

Your script includes some function what i have not used yet so i can't directly help with that script..

Hopefully these of script will give some help.. These scripts are in the use at my finnish site..

list.php
<?php
include("navi.html");
// Connect info to database
  $connect = mysql_connect("localhost","user","pass") or
  die("Time out !");
  
// Select database "members"
  mysql_select_db("members",$connect) or
  die("Database not found !");

// Geting all info from cinfo-table
$query = "SELECT * FROM cinfo ORDER BY lastname,firstname";

// Suoritetaan kysely
$search = mysql_query($query, $contact) or
die("Error in query !");

echo "<h1>Members list</h1>";
echo "<p>Members count: " . mysql_num_rows($search) .".</p>";
echo "<p><strong>Infor update need by pressing the link.</strong></p>";
echo "<table border=1>";
echo "<tr>";
echo "<td><b>Line</b></td>";
echo "<td><b>Lastname</b></td>";
echo "<td><b>Firstname</b></td>";
echo "<td><b>Email</b></td>";
echo "</tr>";
$j = 1;
for ($i = 0; $i < mysql_num_rows($search); $i++)
{
	$line = mysql_result($search, $i, "line");
	$lastname = mysql_result($search, $i, "lastname");
	$firstname = mysql_result($search, $i, "firstname");
	$email = mysql_result($search, $i, "email");
	echo "<tr><td><a href=\"infor/index.php?id=$line\" target=\"_blank\">$j</a></td>";
	echo "<td>$lastname</td>";
	echo "<td>$firstname</td>";
	echo "<td>$email</td></tr>";
	$j++;
}
echo "</table>";
// Close the connect
mysql_close($connect);
?>

add-form.php
<form action="add-script.php" method="GET">
<h1>Add member</h1>
<p>Lastname:<br>
  <input type="text" name="lastname" size="30">
</p>
<p>Firstname:<br>
  <input type="text" name="firstname" size="30">
</p>
<p>Email:<br>
	<input type="text" name="email" size="40">
</p>
<p> <input type="submit" value="Save">
	<input type="reset" value="Reset">
</p>
</form>

add-script.php
<?php
include("navi.html");
// Get info from form
$fristname = $_GET['fristname'];
$lastname = $_GET['lastname'];
$email = $_GET['email'];

// Connect info to database
  $connect = mysql_connect("localhost","user","pass") or
  die("Time out !");
  
// Select database "members"
  mysql_select_db("members",$connect) or
  die("Database not found !");

// Adding info to cinfo-table
$query = "INSERT INTO cinfo (lastname, firstname, email)
VALUES ('$lastname', '$firstname', '$email')";

// Quering
$search = mysql_query($query, $connect) or
die("Error in query ! Infos not added.");
 echo "Member have been added.";
// Printing tables info at the end
// Close database connect.
mysql_close($connect);
?>

"Hasty can't do, skilled don't rush"
"Life is like a camel; you can make it do anything except back up." - Marcelene Cox
"It's not who I am underneath, but what I do that defines me." - Batman begins

Posted Image

#12 Phil

Phil

    Force Majeure

  • Network Leaders
  • 7,976 posts
  • Location:Switzerland
  • Projects:Revora, C&C:Online
  •  Thought Police
  • Division:Revora
  • Job:Network Leader
  • Donated
  • Association

Posted 16 January 2007 - 10:00 PM

Hmm, I'll try to use it later, but I fear that I'm not good enough to change it to my likings yet... for now I'll be using mysql.exe to learn the MySQL commands. There is a msqli chapter in my book where I'll certainly learn enough to write my own script which works.
An exception is if someone finds an obvious mistake in the one above (I doubt it though).

In either case: Thanks a lot for all the help (and the script) :)

revorapresident.jpg
My Political Compass

Sieben Elefanten hatte Herr Dschin
Und da war dann noch der achte.
Sieben waren wild und der achte war zahm
Und der achte war's, der sie bewachte.


#13 poedguy

poedguy
  • Members
  • 200 posts
  • Location:Perth, Western Australia
  •  One of CNC Guild's founders

Posted 17 January 2007 - 01:18 AM

I prefer to use the plain mysql commands with PHP instead of the mysqli commands. The standard mysql commands are generally more reliable than the newer mysqli commands.

#14 Phil

Phil

    Force Majeure

  • Network Leaders
  • 7,976 posts
  • Location:Switzerland
  • Projects:Revora, C&C:Online
  •  Thought Police
  • Division:Revora
  • Job:Network Leader
  • Donated
  • Association

Posted 17 January 2007 - 05:13 PM

Well, I only use very few mysqli commands to transfer my normal MySQL commands to the program itself... apparently what I'm asking of this script is already too much, *sigh*, cmd then...

revorapresident.jpg
My Political Compass

Sieben Elefanten hatte Herr Dschin
Und da war dann noch der achte.
Sieben waren wild und der achte war zahm
Und der achte war's, der sie bewachte.


#15 DCoder

DCoder

    One of the Few

  • Members
  • 331 posts
  • Location:Lithuania, Central Europe
  •  Old Grumpy Bastard

Posted 17 January 2007 - 06:45 PM

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 '\'20095\', \'Hamburg\'), (\'59821\', \'Arnsberg\')' at line 1

Disable magic_quotes_gpc.
Ares - How do you want to improve YR today?
What's stopping you from migrating to Ares? We can't implement it unless you tell us!

ModEnc - C&C Modding Encyclopedia | Modders' Quote Database | Yet Another C&C Modding Forum

#16 Phil

Phil

    Force Majeure

  • Network Leaders
  • 7,976 posts
  • Location:Switzerland
  • Projects:Revora, C&C:Online
  •  Thought Police
  • Division:Revora
  • Job:Network Leader
  • Donated
  • Association

Posted 17 January 2007 - 06:56 PM

I've disabled it in php.ini and php5.ini, but it still gives me the same error. Is there something else I have to do or does the problem lie somewhere else?

EDIT: I've just checked phpinfo, and it says that the magic_quotes_gpc is still on... how do I turn it off? (I'm using xampp 1.5.1)

Edited by Dark Lord of the Sith, 17 January 2007 - 07:11 PM.

revorapresident.jpg
My Political Compass

Sieben Elefanten hatte Herr Dschin
Und da war dann noch der achte.
Sieben waren wild und der achte war zahm
Und der achte war's, der sie bewachte.


#17 DCoder

DCoder

    One of the Few

  • Members
  • 331 posts
  • Location:Lithuania, Central Europe
  •  Old Grumpy Bastard

Posted 17 January 2007 - 07:17 PM

Reboot Apache.
Ares - How do you want to improve YR today?
What's stopping you from migrating to Ares? We can't implement it unless you tell us!

ModEnc - C&C Modding Encyclopedia | Modders' Quote Database | Yet Another C&C Modding Forum

#18 Phil

Phil

    Force Majeure

  • Network Leaders
  • 7,976 posts
  • Location:Switzerland
  • Projects:Revora, C&C:Online
  •  Thought Police
  • Division:Revora
  • Job:Network Leader
  • Donated
  • Association

Posted 17 January 2007 - 08:10 PM

I can't believe how stupid I am sometimes... I edited the wrong php.ini :p

Anyway, it works now! Yay! It is at least related to what I thought it may be in the beginning, but I would have never found out how to correct this without your help. Thanks a lot, DCoder, you're awesome :D

revorapresident.jpg
My Political Compass

Sieben Elefanten hatte Herr Dschin
Und da war dann noch der achte.
Sieben waren wild und der achte war zahm
Und der achte war's, der sie bewachte.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users