首页 \ 问答 \ 无法更新MySQL数据库[关闭](Cant Update MySQL database [closed])

无法更新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
更新时间:2024-03-23 15:03

最满意答案

你忘了关闭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 so main 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)
}

相关问答

更多
  • 是的,这很容易做到。 使用互斥锁来保护共享状态,在需要等待共享状态更改时使用条件变量,使用普通整数变量来跟踪共享状态。 当一个线程进入一个区域时,它应该: 获取互斥锁。 检查是否允许运行该区域。 虽然不允许,但在释放互斥锁时阻止条件变量。 增加该区域中的线程数。 释放互斥锁。 当一个线程拥有一个区域时,它应该: 获取互斥锁。 减少该区域中的线程数。 如果计数的更改可能允许线程进行,则广播条件变量。 释放互斥锁。 就这么简单。 Yes, this is very easy to do. Use a mutex ...
  • 解决了......我一开始并没有意识到,但由于某种原因,现有的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 ...
  • 首先,让我们谈一个简单的误解: 我知道这些用户线程最终由操作系统线程映射到操作系统。 实际上,Haskell运行时负责选择哪个Haskell从其池中的特定OS线程执行。 现在,问题,一次一个。 为什么Haskell线程可以将完全不同的东西映射到同一个OS线程? 目前忽略FFI,所有操作系统线程都在运行Haskell运行时,该运行时会跟踪Haskell线程列表。 运行时选择一个Haskell线程来执行,并跳转到代码中,直到线程返回运行时为止。 那时,运行时有机会继续执行相同的线程或选择另一个线程。 简而言之: ...
  • 这听起来像你想要的是一个单线程,它为整个程序执行所有数据库访问,在这种情况下,你不应该将事情传递给run方法。 因为当你有一个线程时,它所做的一切就是在启动时调用一次运行方法,并且该运行方法只是继续执行,直到它返回或线程被强制终止。 做这种事情的方法是在你的DBAdmin类中有一个同步队列。 然后像addCategory这样的方法会准备好他们的语句并将它们添加到这个共享队列中。 单个数据库线程一次处理一个队列中的语句,并且当语句完成时,它会通知将其添加到队列中的线程。 事实上,Java(最近的版本)至少包含 ...
  • 根据你的问题,线程是独立的操作系统级别的对象,它们与进程绑定在一起。 所以函数可以启动一个,不要等到线程完成。 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 ...
  • 你忘了关闭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 ...
  • 异常消息显示您已将Blog和Post用作嵌套在Program类,而说明指出: 在Program.cs中的Program类定义下面添加以下两个类。 我个人将它们分成不同的文件,但这是另一回事。 鉴于他们是公共课程,我仍然对它失败感到惊讶,但听起来这就是问题所在。 至于“函数评估需要所有线程运行” - 这实际上并不是一个错误 ,它只是在评估属性需要使用各种线程时显示调试限制。 The exception message shows that you've used Blog and Post as classe ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)