sql语句带问号代表什么?又应该怎么写?
我尝试一下:<?php$conn=mysql_connect('localhost','root','123')ordie(mysql_error());mysql_qu...
我尝试一下:
<?php
$conn = mysql_connect('localhost', 'root', '123') or die(mysql_error());
mysql_query("set names 'utf8'");
mysql_select_db("model");
$sql = "select * from `test` where id=?";
$query = mysql_query($sql) or die(mysql_error());
while($arr = mysql_fetch_array($query)){
print_r($arr);
}
运行报错:You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near '?' at
line 1
求解:带?的SQL语句该怎么写 展开
<?php
$conn = mysql_connect('localhost', 'root', '123') or die(mysql_error());
mysql_query("set names 'utf8'");
mysql_select_db("model");
$sql = "select * from `test` where id=?";
$query = mysql_query($sql) or die(mysql_error());
while($arr = mysql_fetch_array($query)){
print_r($arr);
}
运行报错:You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near '?' at
line 1
求解:带?的SQL语句该怎么写 展开
展开全部
带问号的是pdo里面的参数,如果不用pdo,就要组装成完整的sql语句
追问
你好,我用了PDO来链接数据库,但是还是报错
$sql = $db->query("select * from `test` where id=?");
print_r($sql->fetchall());
报错:Fatal error: Call to a member function fetchall() on a non-object
追答
带参pdo不是这么写的
$db=new PDO($dsn); //dsn自己写
//预备一个语句
$sql="select * from `test` where id=?";
$statment=$db->prepare($sql);
//pdo的参数格式,1.可以全部用?表示,参数按数组索引替换
$params=array(1);
$result = $statement->execute($params);
//2.可以用键值对表示
$sql="select * from `test` where id=:id";
$statment=$db->prepare($sql);
$params=array(':id'=>1);
$result = $statement->execute($params);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询