我试过这个功能,它的工作原理:
$stmt=mysqli_stmt_init($con);
if (mysqli_stmt_prepare($stmt,"INSERT INTO `files` ( `Name`) VALUES (?) "))
{
// Bind parameters
mysqli_stmt_bind_param($stmt,"s",$file_name);
//mysqli_stmt_bind_param($stmt,"s",$extension);
// Execute query
mysqli_stmt_execute($stmt);
// Bind result variables
mysqli_stmt_bind_result($stmt,$district);
// Fetch value
mysqli_stmt_fetch($stmt);
printf("%s is in files %s",$file_name,$files);
}
Run Code Online (Sandbox Code Playgroud)
但是如何使它适用于多个参数,如下所示:
if (mysqli_stmt_prepare($stmt,"INSERT INTO `files` ( `FName`,`LName`) VALUES (?,?) "))
Run Code Online (Sandbox Code Playgroud) 我有一个表格作品集,时间戳设置为自动更新.
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
Run Code Online (Sandbox Code Playgroud)
PHP中的PDO语句不会导致时间戳更新.
$statement = $this->connection->prepare("
INSERT INTO folio(publication, productId)
VALUES(:publication, :productId)
ON DUPLICATE KEY UPDATE
id=LAST_INSERT_ID(id), publication=:publication, productId=:productId");
Run Code Online (Sandbox Code Playgroud)
以下手动方法有效但不可取.
$statement = $this->connection->prepare(
"INSERT INTO folio(publication, productId)
VALUES(:publication, :productId)
ON DUPLICATE KEY UPDATE
id=LAST_INSERT_ID(id), publication=:publication, productId=:productId, timestamp=NOW()");
Run Code Online (Sandbox Code Playgroud)
更新:这是我的作品集表结构
CREATE TABLE `folio` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`publication` varchar(255) DEFAULT NULL,
`productId` varchar(255) DEFAULT NULL,
`timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_folio` (`publication`,`productId`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
Run Code Online (Sandbox Code Playgroud)
更新2:将timestamp设置为not null后的表结构 …
我有一个奇怪的问题.我有一个简单的PHP脚本,它使用PDO从数据库中获取所有国家/地区,然后将结果返回为json.当我使用fetch函数而不是fetchAll时,一切都按预期工作.当我print_r时,数据就在那里.
不起作用:
$sql = "SELECT * FROM countries";
if($stmt = $_db->prepare($sql))
{
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
header("content-type:application/json");
echo json_encode($data);
exit();
}
Run Code Online (Sandbox Code Playgroud)
作品:
$sql = "SELECT * FROM countries";
if($stmt = $_db->prepare($sql))
{
$stmt->execute();
$data = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();
header("content-type:application/json");
echo json_encode($data);
exit();
}
Run Code Online (Sandbox Code Playgroud)
print_r的结果:
Array
(
[0] => Array
(
[id] => 1
[name] => Afghanistan
)
[1] => Array
(
[id] => 2
[name] => Åland Islands
)
[2] => Array
(
[id] => 3
[name] => Albania
) …Run Code Online (Sandbox Code Playgroud) 我有两个phpmyadmin版本4.2.7,PHP版本是PHP 版本 5.4.24,其中一个 phpmyadmin 允许空值,另一个不接受空值
phpmyadmin - 1
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
Run Code Online (Sandbox Code Playgroud)
phpmyadmin - 2
在第二个表中,我没有给出 column3 的值,其错误如下
我收到错误:
General error: 1364 Field 'column3' doesn't have a default value。
尽管我没有设置nullforphpmyadmin-1但它工作正常。我该如何解决这个问题?有什么建议吗?
INSERT INTO table_name (column1,column2,...)
VALUES (value1,value2,...);
Run Code Online (Sandbox Code Playgroud) 我有一个列participants,其中包含一个类似于"99005|99001|99002|99001999|99004"用户登录的值。
我到底想要的是匹配"99001"而不匹配"99001999"。
这是我的方法:
SELECT * FROM `bv_sklad_products` WHERE `stage`=4 AND `participants` REGEXP ('^([^\|]+(\|))*(99001|99005)((\|)[^\|]+)*$') AND `start_date` BETWEEN '2015-07-09' AND '2015-07-10' ORDER BY `id` DESC LIMIT 0,100
Run Code Online (Sandbox Code Playgroud)
我得到的错误信息:
Got error 'empty (sub)expression' from regexp
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我有2个MySQL表:subscriptions与groups.
列sid在表中subscriptions包含负值,但列id在表中groups包含相同的值,但是正面的。
我需要一些方法sid在加入表之前将值转换为正值groups,或者将表中的所有值id视为负值的查询
以下是我的查询。它不返回任何结果,因为列sid包含负值,但列id包含正值
$tmp = mysqli_query($con1,"
SELECT s.uid
, s.sid
, g.title
FROM subscriptions s
LEFT
JOIN groups g
ON s.sid = g.id
WHERE s.uid = $id
LIMIT 0,20
") or die('Error8');
Run Code Online (Sandbox Code Playgroud) 这是我的表名 tblPE
PEID |idnum | PE_DATE_EXAM | ATTENDANCE
1 | 39 | 2014-08-01 | PRESENT
2 | 42 | 2014-08-10 | ABSENT
3 | 39 | 2014-08-12 | PRESENT
4 | 43 | 2014-08-05 | PRESENT
5 | 42 | 2014-07-15 | NULL
6 | 39 | 2014-07-03 | ABSENT
7 | 41 | 2014-08-01 | PRESENT
Run Code Online (Sandbox Code Playgroud)
我想选择最大 PE_DATE_EXAM 值,其中 idnum = 39 且 ATTENDANCE=PReSENT
结果应该是:
PEID |idnum | PE_DATE_EXAM | ATTENDANCE
-------------------------------------------
3 | 39 | 2014-08-12 | PRESENT
Run Code Online (Sandbox Code Playgroud) 此代码出现问题
<?php
define('DB_USER','root');
define('DB_PASSWORD','censored');
define('DB_HOST','localhost');
define('DB_NAME','censored');
$dbc = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
?>
Run Code Online (Sandbox Code Playgroud)
我已经安装了 php7.0-mysqli 但我仍然收到这个错误信息
PHP Fatal error: Uncaught Error: Call to undefined function mysqli() in /var/www/html/actions/create_account.php:29\nStack trace:\n#0 {main}\n thrown in /var/www/html/actions/create_account.php on line 29, referer: http://localhost/register.php
Run Code Online (Sandbox Code Playgroud)
我php artisan migrate:rollback在终端中使用然后错误消息如下
H:\wamp_server\www\cms>php artisan migrate:rollback
[Illuminate\Database\QueryException] SQLSTATE[42S21]:列已经存在:1060 列名重复'is_admin'(SQL:alter table
postsaddis_adminint not null)[PDOException] SQLSTATE[42S21]:列已存在:1060 列名重复“is_admin”
下面的迁移代码
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddIsAdminColumnToPostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function (Blueprint $table) {
//
$table->integer('is_admin')->unsigned();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('posts', function (Blueprint $table) { …Run Code Online (Sandbox Code Playgroud) 即时通讯使用谷歌recaptcha和我的验证方式是通过ajax,一切正常,我得到了回应
{
success: "false/true"
}
Run Code Online (Sandbox Code Playgroud)
但我得到一个错误,说"使用未定义的不断成功 - 假设'成功'在第30行,任何想法,帮助,建议,建议?
这是我在控制器上的代码(用于传达google recaptcha API的代码)
$captcha=$request->input('g-recaptcha-response');
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=mysitekey&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success == false) //this is the line 30
{
return 'You are spammer ! Get the out';
}else{
return 'Thanks for posting comment.';
}
Run Code Online (Sandbox Code Playgroud)
这是第30行"$ response.success == false"