SELECT Query不能运行MySQL

时间:2018-05-28 15:58:29

标签: php mysql pdo prepared-statement

我似乎无法使我的查询工作。当我使用$ sth-> execute();我确实得到了一个阅读,但据我所知,我的目标毫无价值。如果我使用查询,则不显示任何内容。请记住,在编码方面,我是初学者。此外,这是一个学校项目,仅此而已。

我不知道为什么第一行代码没有实现,所以请查看下面的行作为代码的一部分:

$team1 = $_POST['teamname'];
$sth = $pdo->prepare("SELECT odds1 FROM odds WHERE :team1=team1");
$sth->bindParam(':team1', $team1);
$sth->query();
$values = $sth->fetchAll(PDO::FETCH_ASSOC);                 
echo $values[odds1];

2 个答案:

答案 0 :(得分:4)

此代码应该崩溃并出现错误,所以如果您想知道为什么它“不起作用”,首先检查的是您的错误日志。 PHP将在那里输出各种警告和错误,以帮助您进行调试,所以如果你现在还不知道现在是什么时候找到它。

技术修复是query()是在语句句柄上运行的错误方法。而是致电execute()

$sth = $pdo->prepare("SELECT odds1 FROM odds WHERE :team1=team1");
$sth->bindParam(':team1', $team1);
$sth->execute();

你实际上可以最小化这个:

$sth = $pdo->prepare("SELECT odds1 FROM odds WHERE :team1=team1");
$sth->execute([ ':team1' => $team1 ]);

这是因为execute可以获取一系列参数。

SQL中的参数顺序通常是column=?,但您可以按任意顺序执行此操作,MySQL的比较是双向的。如果您将查询编写为:

,它将使您的代码更加传统
$sth = $pdo->prepare("SELECT odds1 FROM odds WHERE team1=:team1");

现在有一个列名如odds1team1通常表示您违反了Zero, One or Infinity RuleModeGameView(ScreenAdapter next_screen) { super(next_screen); atlas = new TextureAtlas("buttons.txt"); background = atlas.createSprite("background"); title = atlas.createSprite("cluedo"); title.scale((float) 1); size = (title.getHeight() + 100); singleplayer = atlas.createSprite("singleplayer"); singleplayer.setCenterX(500); multiplayer = atlas.createSprite("multiplayer"); batch = new SpriteBatch(); camera = new OrthographicCamera(); viewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera); viewport.apply(); camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0); camera.update(); stage = new Stage(); } @Override public ScreenAdapter renderAndUpdate(float delta) { this.render(delta); return this; } @Override public void show() { Gdx.input.setInputProcessor(stage); Drawable playDrawable = new SpriteDrawable(singleplayer); singleplayerButton = new ImageButton(playDrawable); Drawable exitDrawable = new SpriteDrawable(multiplayer); multiplayerButton = new ImageButton(exitDrawable); singleplayerButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { Cluedo.getInstance().setCurrentState(new SinglePlayerView(Cluedo.getInstance().getGameView())); } }); multiplayerButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { Cluedo.getInstance().setCurrentState(new MultiplayerView(Cluedo.getInstance().getGameView())); } }); stage.addActor(singleplayerButton); stage.addActor(multiplayerButton); } @Override public void render(float delta) { Gdx.gl.glClearColor(.1f, .12f, .16f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.begin(); background.draw(batch); title.setPosition((Gdx.graphics.getWidth() - title.getWidth()) / 2, Gdx.graphics.getHeight() - size); title.draw(batch); batch.end(); stage.setDebugAll(true); stage.act(); stage.draw(); } database normalization。正确的正常形式在一条记录与其他记录之间会有一对多的关系。

答案 1 :(得分:0)

更改

filter.setInitParameter("allowedHeaders",
"Cache-Control,If-Modified-Since,Pragma,Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin");

echo $values[odds1];