I'm trying to learn php and ran into a problem. I'd appreciate any help.
我正在学习php,遇到了一个问题。我感谢任何帮助。
I have a database that looks like:
我有一个这样的数据库:
ID USER AGE
1 name1 18
2 name2 19
3 name3 20
etc etc
I want to delete multiple records. This is the code I am using for that:
我想删除多条记录。这是我使用的代码:
<?php
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("photo",$dbhandle)
or die("Could not select examples");
//execute
echo "CONNECTED";
echo "</br>";
//Action
$delete_query = ("DELETE FROM _users WHERE age in(17,18,19)");
$result = mysql_query($delete_query);
echo 'Deleted';
mysql_close($dbhandle);
?>
When I am deleting by querying the age column, it deletes multiple rows okay.
当我通过查询age列进行删除时,它会删除多个行。
However, when I do this:
然而,当我这样做的时候:
$delete_query = ("DELETE FROM _users WHERE USER in(name1,name2,name3)");
Nothing happens. Is it because those are strings? How can I fix it?
什么也不会发生。是因为这些是弦吗?我怎样才能修好它呢?
Thanks!
谢谢!
6 个解决方案
#1
2
The query is incorrect:
查询是不正确的:
DELETE FROM _users WHERE USER in ('name1','name2','name3');
strings must be quoted, otherwise MySQL will see them as field names (or keywords). You have no error handling in your script, or you'd have seen the error messages. Always write a query as follows:
必须引用字符串,否则MySQL会将它们视为字段名(或关键字)。在脚本中没有错误处理,或者您已经看到了错误消息。始终编写如下查询:
$result = mysql_query(...) or die(mysql_error());
this'll show you exactly WHY the query failed (or just continue on if there's nothing wrong).
这将向您展示查询失败的确切原因(如果没有任何问题,则继续执行)。
#2
1
DELETE FROM _users WHERE USER in(name1,name2,name3)
MySQL thinks that name1, name2, and name3 are columns. Quote them.
MySQL认为name1、name2和name3是列。引用它们。
#3
1
use single quote '
使用单引号'
$delete_query = ("DELETE FROM _users WHERE `USER` in('name1','name2','name3')");
#4
0
You need to encase the strings in quotes: WHERE USER in('name1', 'name2', 'name3')
需要将字符串括在引号中:WHERE USER in('name1', 'name2', 'name3')
#5
0
Users is a string, you need to quote the values:
用户是一个字符串,你需要报价的值:
$delete_query = ("DELETE FROM _users WHERE USER in('name1','name2','name3')");
#6
0
Yes, it's because they are strings. Strings need to be quoted properly, otherwise MySQL won't see them as strings. Use
是的,因为它们是字符串。字符串需要正确引用,否则MySQL不会将它们视为字符串。使用
$delete_query = ("DELETE FROM _users WHERE USER in('name1','name2','name3')");
#1
2
The query is incorrect:
查询是不正确的:
DELETE FROM _users WHERE USER in ('name1','name2','name3');
strings must be quoted, otherwise MySQL will see them as field names (or keywords). You have no error handling in your script, or you'd have seen the error messages. Always write a query as follows:
必须引用字符串,否则MySQL会将它们视为字段名(或关键字)。在脚本中没有错误处理,或者您已经看到了错误消息。始终编写如下查询:
$result = mysql_query(...) or die(mysql_error());
this'll show you exactly WHY the query failed (or just continue on if there's nothing wrong).
这将向您展示查询失败的确切原因(如果没有任何问题,则继续执行)。
#2
1
DELETE FROM _users WHERE USER in(name1,name2,name3)
MySQL thinks that name1, name2, and name3 are columns. Quote them.
MySQL认为name1、name2和name3是列。引用它们。
#3
1
use single quote '
使用单引号'
$delete_query = ("DELETE FROM _users WHERE `USER` in('name1','name2','name3')");
#4
0
You need to encase the strings in quotes: WHERE USER in('name1', 'name2', 'name3')
需要将字符串括在引号中:WHERE USER in('name1', 'name2', 'name3')
#5
0
Users is a string, you need to quote the values:
用户是一个字符串,你需要报价的值:
$delete_query = ("DELETE FROM _users WHERE USER in('name1','name2','name3')");
#6
0
Yes, it's because they are strings. Strings need to be quoted properly, otherwise MySQL won't see them as strings. Use
是的,因为它们是字符串。字符串需要正确引用,否则MySQL不会将它们视为字符串。使用
$delete_query = ("DELETE FROM _users WHERE USER in('name1','name2','name3')");