mysql_query无法在最新的php版本中运行
I am trying to insert data in to MySQL Database, but have been stuck on Data insertion.
I am using Version 3.0.0
of WampServer and PHP 5.6.16
, phpMyAdmin 4.5.2
.
Everything else are ok, I can UPDATE, DELETE
...Except INSERT INTO
.
Code
<?php
$con=@mysql_connect('localhost','root','');
mysql_query('SET NAMES utf8;');
mysql_select_db('mydb',$con);
//////////////////////////////////
if(isset($_POST['post_files'])) {
$files = $_POST['files'];
$title = $_POST['title'];
//////////////////////////////////
$query = "INSERT INTO files (`file`, `title`) VALUES ('$files', '$title')";
if(mysql_query($query))
{
echo'OK, Data Inserted.';
}
else
{
echo 'Sorry! Operation failed.';
}
}
mysql_close();
?>
These Bunch of codes were working fine in previous PHP versions.
</div>
我正在尝试将数据插入MySQL数据库,但一直停留在数据插入上。 p> \ n
我正在使用WampServer的版本 其他一切都还可以,我可以 代码 strong> p>
p> 这些代码 在以前的PHP版本中运行良好。 p>
div> 3.0.0 code>和PHP
5.6.16 code>,phpMyAdmin
4.5.2 code>。 p >
UPDATE,DELETE code> ...除
INSERT INTO code>。 p>
&lt;?php
$ con = @ mysql_connect('localhost','root','');
mysql_query('SET NAMES utf8;');
mysql_select_db('mydb',$ con);
/// ///////////////////////////////
(isset($ _ POST ['post_files'])){
$ files = $ _POST ['files'];
$ title = $ _POST ['title'];
//////////////////////////////////
$ query =“INSERT INTO files( `file`,`title`)VALUES('$ files','$ title')“;
(mysql_query($ query))
{
echo'OK,Data Inserted。';
}
else
{
echo'抱歉! 操作失败。';
}
}
mysql_close();
?&gt;
code> pre>
div>
div>
instead of mysql
use mysqli
For instance:
-
mysql_connect
will be replaced bymysqli_connect
-
mysql_query
will be replaced bymysqli_query($con, $query)
- and so on
Note that, for some functions, you may need to check the parameters carefully: Maybe there are some differences here and there -- but not that many, I'd say: both mysql and mysqli are based on the same library (libmysql ; at least for PHP <= 5.2)
For instance :
with mysql, you have to use the mysql_select_db
once connected, to indicate on which database you want to do your queries
mysqli, on the other side, allows you to specify that database name as the fourth parameter to mysqli_connect
.
Still, there is also a mysqli_select_db
function that you can use, if you prefer.
For more information read this article : PHP MySQL to MySQLi