无法更新MySQL数据库[关闭](Cant Update MySQL database [closed])
我正在尝试学习mysql并在更新/向表中添加数据时遇到一些问题
这是我的代码,运行此页面后,当我去phpmyadmin查看新数据是否显示时,我没有看到它。
<?php $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(!$conn) { die("Could not connect"); } $dbname = "test"; mysql_select_db($dbname, $conn); mysql_query("INSERT INTO 'test'.'table1' ('A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8') VALUES ('test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7', 'test8')"); mysql_close($conn); ?>
有人能告诉我这有什么不对吗?
I am trying to learn mysql and having some problem with updating/adding data to the table
this is my code and after running this page when I go to phpmyadmin to see if the new data showed up, i dont see it there.
<?php $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(!$conn) { die("Could not connect"); } $dbname = "test"; mysql_select_db($dbname, $conn); mysql_query("INSERT INTO 'test'.'table1' ('A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8') VALUES ('test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7', 'test8')"); mysql_close($conn); ?>
Can anyone tell me whats wrong with this ?
原文:https://stackoverflow.com/questions/3508067
最满意答案
你忘了关闭
minCutChan
因此main
已经进入范围并且所有go例程都已完成。
sync.WaitGroup
使用频道,您可以使用sync.WaitGroup
编辑 :要处理totalCount我会使用
atomic.AddInt64
看到新的更新示例:请参阅这些编辑的工作模拟示例: http : //play.golang.org/p/WyCQrWK5aa
package main import ( "fmt" "sync" "sync/atomic" ) type Graph struct { } func contractGraph(Graph) int { return 0 } func worker(wg *sync.WaitGroup, graph Graph, i int, minCutChan chan<- int) { defer wg.Done() for { count := atomic.AddInt64(&totalCount, -1) if count < 0 { break } fmt.Println("Worker Iteration", count) min_cut := contractGraph(graph) minCutChan <- min_cut } } func workerRunner(graph Graph, minCutChan chan int, workerCount int) { wg := new(sync.WaitGroup) wg.Add(workerCount) for i := 0; i < workerCount; i++ { go worker(wg, graph, i, minCutChan) } wg.Wait() close(minCutChan) } var totalCount int64 func main() { workerCount := 4 graph := Graph{} totalCount = 100 minCutChan := make(chan int, workerCount+1) go workerRunner(graph, minCutChan, workerCount) go func() { }() // read the resulting min cuts minCut := 0 for _minCut := range minCutChan { if minCut == 0 || _minCut < minCut { minCut = _minCut } } fmt.Println(minCut) }
更进一步的风格是在匿名函数中旋转工作者:
http://play.golang.org/p/nT0uUutQyS
包主
import ( "fmt" "sync" "sync/atomic" ) type Graph struct { } func contractGraph(Graph) int { return 0 } var totalCount int64 func workerRunner(graph Graph, minCutChan chan int, workerCount int) { var wg sync.WaitGroup wg.Add(workerCount) for i := 0; i < workerCount; i++ { go func() { defer wg.Done() for { count := atomic.AddInt64(&totalCount, -1) if count < 0 { break } fmt.Println("Worker Iteration", count) min_cut := contractGraph(graph) minCutChan <- min_cut } }() } wg.Wait() close(minCutChan) } func main() { workerCount := 4 totalCount = 100 graph := Graph{} minCutChan := make(chan int, workerCount+1) go workerRunner(graph, minCutChan, workerCount) // read the resulting min cuts minCut := 0 for _minCut := range minCutChan { if minCut == 0 || _minCut < minCut { minCut = _minCut } } fmt.Println(minCut) }
You forgot to close the
minCutChan
somain
is stuck into range and all the go routines have completed.to not use the channel you can use
sync.WaitGroup
EDIT: To handle the totalCount I would use
atomic.AddInt64
see the new updated examples:see a working mock example with these edits: http://play.golang.org/p/WyCQrWK5aa
package main import ( "fmt" "sync" "sync/atomic" ) type Graph struct { } func contractGraph(Graph) int { return 0 } func worker(wg *sync.WaitGroup, graph Graph, i int, minCutChan chan<- int) { defer wg.Done() for { count := atomic.AddInt64(&totalCount, -1) if count < 0 { break } fmt.Println("Worker Iteration", count) min_cut := contractGraph(graph) minCutChan <- min_cut } } func workerRunner(graph Graph, minCutChan chan int, workerCount int) { wg := new(sync.WaitGroup) wg.Add(workerCount) for i := 0; i < workerCount; i++ { go worker(wg, graph, i, minCutChan) } wg.Wait() close(minCutChan) } var totalCount int64 func main() { workerCount := 4 graph := Graph{} totalCount = 100 minCutChan := make(chan int, workerCount+1) go workerRunner(graph, minCutChan, workerCount) go func() { }() // read the resulting min cuts minCut := 0 for _minCut := range minCutChan { if minCut == 0 || _minCut < minCut { minCut = _minCut } } fmt.Println(minCut) }
even more in go style is to spin the workers inside an anonymous function:
http://play.golang.org/p/nT0uUutQyS
package main
import ( "fmt" "sync" "sync/atomic" ) type Graph struct { } func contractGraph(Graph) int { return 0 } var totalCount int64 func workerRunner(graph Graph, minCutChan chan int, workerCount int) { var wg sync.WaitGroup wg.Add(workerCount) for i := 0; i < workerCount; i++ { go func() { defer wg.Done() for { count := atomic.AddInt64(&totalCount, -1) if count < 0 { break } fmt.Println("Worker Iteration", count) min_cut := contractGraph(graph) minCutChan <- min_cut } }() } wg.Wait() close(minCutChan) } func main() { workerCount := 4 totalCount = 100 graph := Graph{} minCutChan := make(chan int, workerCount+1) go workerRunner(graph, minCutChan, workerCount) // read the resulting min cuts minCut := 0 for _minCut := range minCutChan { if minCut == 0 || _minCut < minCut { minCut = _minCut } } fmt.Println(minCut) }
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
是的,这很容易做到。 使用互斥锁来保护共享状态,在需要等待共享状态更改时使用条件变量,使用普通整数变量来跟踪共享状态。 当一个线程进入一个区域时,它应该: 获取互斥锁。 检查是否允许运行该区域。 虽然不允许,但在释放互斥锁时阻止条件变量。 增加该区域中的线程数。 释放互斥锁。 当一个线程拥有一个区域时,它应该: 获取互斥锁。 减少该区域中的线程数。 如果计数的更改可能允许线程进行,则广播条件变量。 释放互斥锁。 就这么简单。 Yes, this is very easy to do. Use a mutex ...
-
线程没有运行,为什么?(Threads not running, why?)[2024-05-06]
解决了......我一开始并没有意识到,但由于某种原因,现有的DLL调用了: DisableThreadLibraryCalls(hInstance); 这可以防止线程运行。 评论完这一切后,现在一切正常。 Solved...I didn't realise at first but for some reason the existing DLL had a call to: DisableThreadLibraryCalls(hInstance); This prevents the ... -
forkIO线程和操作系统线程(forkIO threads and OS threads)[2023-03-21]
首先,让我们谈一个简单的误解: 我知道这些用户线程最终由操作系统线程映射到操作系统。 实际上,Haskell运行时负责选择哪个Haskell从其池中的特定OS线程执行。 现在,问题,一次一个。 为什么Haskell线程可以将完全不同的东西映射到同一个OS线程? 目前忽略FFI,所有操作系统线程都在运行Haskell运行时,该运行时会跟踪Haskell线程列表。 运行时选择一个Haskell线程来执行,并跳转到代码中,直到线程返回运行时为止。 那时,运行时有机会继续执行相同的线程或选择另一个线程。 简而言之: ... -
Java线程:指定应该在运行函数中执行的内容(Java Threads: Specifying what should be executed in the run function)[2022-01-22]
这听起来像你想要的是一个单线程,它为整个程序执行所有数据库访问,在这种情况下,你不应该将事情传递给run方法。 因为当你有一个线程时,它所做的一切就是在启动时调用一次运行方法,并且该运行方法只是继续执行,直到它返回或线程被强制终止。 做这种事情的方法是在你的DBAdmin类中有一个同步队列。 然后像addCategory这样的方法会准备好他们的语句并将它们添加到这个共享队列中。 单个数据库线程一次处理一个队列中的语句,并且当语句完成时,它会通知将其添加到队列中的线程。 事实上,Java(最近的版本)至少包含 ... -
由与该函数绑定的函数创建的新线程还是独立的?(Are new threads created by a function tied to that function or are they independent?)[2024-05-09]
根据你的问题,线程是独立的操作系统级别的对象,它们与进程绑定在一起。 所以函数可以启动一个,不要等到线程完成。 Threads are independent OS-level objects, in terms of your question they are tied to process. So function can start one and don't wait until thread done. -
您可能使用多个实例,否则您在实例上同步的方法访问时不会出现同步问题。 要解决您的问题,您可以在所有实例共享的静态字段上进行同步。 在类中添加一个静态Object并进行同步,它应该解决并发访问的问题: public class YourClassThatHasTheProblem { ... private static final Object lock = new Object(); ... public void createTable(Utils utils, Str ...
-
在多个线程中运行一个函数(Run a function in several threads)[2023-12-28]
你忘了关闭minCutChan因此main已经进入范围并且所有go例程都已完成。 sync.WaitGroup使用频道,您可以使用sync.WaitGroup 编辑 :要处理totalCount我会使用atomic.AddInt64看到新的更新示例: 请参阅这些编辑的工作模拟示例: http : //play.golang.org/p/WyCQrWK5aa package main import ( "fmt" "sync" "sync/atomic" ) type Graph ... -
virtual List
:“函数评估需要运行所有线程”(virtual List [2023-07-12]:“The function evaluation requires all threads to run”) 异常消息显示您已将Blog和Post用作嵌套在Program类,而说明指出: 在Program.cs中的Program类定义下面添加以下两个类。 我个人将它们分成不同的文件,但这是另一回事。 鉴于他们是公共课程,我仍然对它失败感到惊讶,但听起来这就是问题所在。 至于“函数评估需要所有线程运行” - 这实际上并不是一个错误 ,它只是在评估属性需要使用各种线程时显示调试限制。 The exception message shows that you've used Blog and Post as classe ...