我有一个.csv
文件,它将通过以下PHP代码将数据插入表中
while (($col = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$quer = "INSERT INTO table1(username, password) VALUES('$col[0]','$col[1]')";
mysql_query($quer);
}
用户名是主键,因此它应该是唯一的。现在,如果表已经包含“abc”作为用户名并且.csv
文件在用户名列中再次插入“abc”,则查询将不会被执行,因为它是主要的!
现在我如何插入所有唯一用户名并显示未从.csv
文件中插入的用户名?
答案 0 :(得分:1)
$quer="INSERT IGNORE into table1(username,password)values('$col[0]','$col[1]')";
if(mysql_affected_rows()==0) echo $col[0];
答案 1 :(得分:1)
执行此操作的最佳方法是首先使用SELECT
查询检查表,以查看主键是否已存在,然后采取相应措施。
或者,如果您想对可能导致冲突的任何行进行更改,可以使用INSERT ... ON DUPLICATE KEY UPDATE ...
,解释here。然后,您可以使用affected-rows
函数检查mysqli_affected_rows
值,具体如下:
对于
ON DUPLICATE KEY UPDATE
,如果将行作为新行插入,则每行affected-rows
值为1;如果现有行更新,则每行INSERT IGONORE
值为0;如果现有行设置为当前行,则每行{0}值。
关于INSERT IGNORE
的说明:我建议不要使用任何涉及mysql_*
的解决方案,因为在防止主键冲突发生任何错误的同时,这也会隐藏任何其他错误错误/警告。这是一件坏事。
关于#!/bin/bash
# exclude branches regex, configure as "(branch1|branch2|etc)$"
excludes_default="(master|next|ag/doc-updates)$"
excludes="__NOTHING__"
includes=
merged="--merged"
local=1
remote=1
while [ $# -gt 0 ]; do
case "$1" in
-i) shift; includes="$includes $1" ;;
-e) shift; excludes="$1" ;;
--no-local) local=0 ;;
--no-remote) remote=0 ;;
--all) merged= ;;
*) echo "Unknown argument $1"; exit 1 ;;
esac
shift # next option
done
if [ "$includes" == "" ]; then
includes=".*"
else
includes="($(echo $includes | sed -e 's/ /|/g'))"
fi
current_branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
if [ "$current_branch" != "master" ]; then
echo "WARNING: You are on branch $current_branch, NOT master."
fi
echo -e "Fetching branches...\n"
git remote update --prune
remote_branches=$(git branch -r $merged | grep -v "/$current_branch$" | grep -v -E "$excludes" | grep -v -E "$excludes_default" | grep -E "$includes")
local_branches=$(git branch $merged | grep -v "$current_branch$" | grep -v -E "$excludes" | grep -v -E "$excludes_default" | grep -E "$includes")
if [ -z "$remote_branches" ] && [ -z "$local_branches" ]; then
echo "No existing branches have been merged into $current_branch."
else
echo "This will remove the following branches:"
if [ "$remote" == 1 -a -n "$remote_branches" ]; then
echo "$remote_branches"
fi
if [ "$local" == 1 -a -n "$local_branches" ]; then
echo "$local_branches"
fi
read -p "Continue? (y/n): " -n 1 choice
echo
if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
if [ "$remote" == 1 ]; then
remotes=$(git remote)
# Remove remote branches
for remote in $remotes
do
branches=$(echo "$remote_branches" | grep "$remote/" | sed "s/$remote\/\(.*\)/:\1 /g" | tr -d '\n')
git push $remote $branches
done
fi
if [ "$local" == 1 ]; then
# Remove local branches
locals=$(echo "$local_branches" | sed 's/origin\///g' | tr -d '\n')
if [ -z "$locals" ]; then
echo "No branches removed."
else
git branch -d $(echo "$locals" | tr -d '\n')
fi
fi
fi
fi
库函数的说明:不要使用它们!从PHP 5.5开始,它们是deprecated,从PHP 7开始是removed!如果您正在学习教程,请考虑将其报告给@ halfer' Awooga数据库。
答案 2 :(得分:1)
正如您所说library(shiny)
ui <- fluidPage(
titlePanel("Test Dashboard "),
sidebarLayout(
sidebarPanel(
uiOutput("data1"), ## uiOutput - gets the UI from the server
uiOutput("data2")
),
mainPanel()
))
server <- function(input, output){
Region<- c("Americas", "Asia Pacific","Asia Pacific", "EMEA", "EMEA")
Country<- c("Mexico", "China","India", "Germany", "Spain" )
Rating<- c(5,3,3,2,4)
book3<- data.frame(Region, Country, Rating, stringsAsFactors = F)
## renderUI - renders a UI element on the server
## used when the UI element is dynamic/dependant on data
output$data1 <- renderUI({
selectInput("data1", "Select Region", choices = c(book3$Region))
})
## input dependant on the choices in `data1`
output$data2 <- renderUI({
selectInput("data2", "select country", choices = c(book3$Country[book3$Region == input$data1]))
})
}
shinyApp(ui, server)
表中的username
列。并且您希望显示那些未插入的记录(由于重复的用户名)。如下所示: -
primary key
注意: - 请阅读@Alex答案并按照说明操作。必要
有关$failed_data = array(); // create an empty array
while (($col = fgetcsv($handle, 1000, ",")) !== FALSE) {
$quer="INSERT into table1(username,password)values('$col[0]','$col[1]')";
if(!mysql_query($quer)){ // check if query run
$failed_data[] = array($col[0],$col[1];) // if not assign failed value to array
}
}
echo "<pre/>";print_r($failed_data);//print the array
库函数的说明:请勿使用它们!从PHP 5.5开始,它们是deprecated,从PHP 7开始是removed!如果您正在学习教程,请考虑将其报告给@ halfer的Awooga数据库。