Home > Backend Development > PHP Tutorial > php数据库输出问题

php数据库输出问题

WBOY
Release: 2016-06-06 20:15:19
Original
977 people have browsed it

<code>$result = mysql_query('"SELECT * FROM jo_post
WHERE id='.$id.'"');
</code>
Copy after login
Copy after login

这样为什么无法输出数据库内容?

回复内容:

<code>$result = mysql_query('"SELECT * FROM jo_post
WHERE id='.$id.'"');
</code>
Copy after login
Copy after login

这样为什么无法输出数据库内容?

PHP中三种主要的MySQL连接方式(5.4开始驱动底层实现都是mysqlnd):
http://php.net/manual/zh/mysqli.overview.php
mysqli(首选) pdo_mysql(建议) mysql(不建议)
PHP7已经不支持mysql扩展了,推荐楼主改用mysqli扩展.
另外,楼主直接拼接字符串作为SQL查询条件,存在SQL注入风险,建议先intval转为整型,或者使用预处理绑定参数查询.

<code><?php //MySQLi普通查询:
$db = @new mysqli('127.0.0.1','user','pass','dbname',3306);
var_export($db->query('SELECT * FROM post WHERE id='.intval($id))->fetch_all());
//MySQLi绑定参数查询:
$db = @new mysqli('127.0.0.1','user','pass','dbname',3306);
$stmt = $db->prepare('SELECT * FROM posts WHERE id=?'); //预处理
$stmt->bind_param('i', $id); //绑定参数(可以防止SQL注入)
$stmt->execute(); //查询
var_export($stmt->get_result()->fetch_all());</code>
Copy after login

$result = mysql_query("SELECT * FROM jo_post WHERE id=$id");

你这跟mysql的交谈方式不对啊
你先试试echo '"SELECT * FROM jo_postWHERE id='.$id.'"';
然后让mysql执行一下
select语句应该是个关键字吧,你把他当作字符串了

按你的写发,你执行的是 "SELECT * FROM jo_post WHERE id=xx" 包括了这两个引号

将你$result变量,用echo语句输出到屏幕,然后复制出来,在mysql管理界面(常用phpmyadmin)中打到对应的数据表,打开SQL菜单,粘贴这个变量的SQL语句,执行一下,就可以知道这个语句对不对,一般SQL语句书写要用双引号和单引号时,双引号在外,单引号在内。你输出不了数据内容,就是SQL语法出错了。

弄清楚单引号和双引号的用法就知道了。

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template