- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
通常我将此代码用于回显页面行。它的工作很好。
$query = "SELECT * FROM table WHERE id = '$id' ";
$result = mysqli_query($db_mysqli, $query);
$row = mysqli_fetch_assoc($result);
$page = $row['page'];
echo $page;
bind_param
此代码用于回显页面行。但是不行,怎么办?
$stmt = $db_mysqli->prepare("SELECT * FROM table WHERE id = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
$result = $stmt->get_result();
$page = $row['page'];
echo $page;
最佳答案
问题描述:mysqli_result
方法返回的对象 get_result
看起来像这样:
mysqli_result Object
(
[current_field] => 0
[field_count] => 3
[lengths] =>
[num_rows] => 1
[type] => 0
)
如您所见,此对象仅公开有关您需要从中引用数据的记录集的一些属性(字段数、行数等)。因此,您不能直接从中引用字段值。
mysqli_result
中定义的方法之一。类(
fetch_all
、
fetch_array
、
fetch_assoc
等):
//...
$result = $stmt->get_result();
$row = $result->fetch_array(MYSQLI_ASSOC);
$page = $row['page'];
//...
与
$row
表示获取的记录并作为这样的数组:
Array
(
[id] => 13
[page] => 21
...
)
更多详情请阅读
The mysqli_result
class .
mysqli
访问数据库所需的所有组件。延期。它呈现了必须从用户列表中获取一条或多条记录的情况 - 保存在名为
users
的数据库表中。 .每个用户由其
id
描述,
name
和
age
.
<?php
require 'connection.php';
// Assign the values used to replace the sql statement markers.
$id = 10;
/*
* The SQL statement to be prepared. Notice the so-called markers,
* e.g. the "?" signs. They will be replaced later with the
* corresponding values when using mysqli_stmt::bind_param.
*
* @link http://php.net/manual/en/mysqli.prepare.php
*/
$sql = 'SELECT
id,
name,
age
FROM users
WHERE id = ?';
/*
* Prepare the SQL statement for execution - ONLY ONCE.
*
* @link http://php.net/manual/en/mysqli.prepare.php
*/
$statement = $connection->prepare($sql);
/*
* Bind variables for the parameter markers (?) in the
* SQL statement that was passed to prepare(). The first
* argument of bind_param() is a string that contains one
* or more characters which specify the types for the
* corresponding bind variables.
*
* @link http://php.net/manual/en/mysqli-stmt.bind-param.php
*/
$statement->bind_param('i', $id);
/*
* Execute the prepared SQL statement.
* When executed any parameter markers which exist will
* automatically be replaced with the appropriate data.
*
* @link http://php.net/manual/en/mysqli-stmt.execute.php
*/
$statement->execute();
/*
* Get the result set from the prepared statement.
*
* NOTA BENE:
* Available only with mysqlnd ("MySQL Native Driver")! If this
* is not installed, then uncomment "extension=php_mysqli_mysqlnd.dll" in
* PHP config file (php.ini) and restart web server (I assume Apache) and
* mysql service. Or use the following functions instead:
* mysqli_stmt::store_result + mysqli_stmt::bind_result + mysqli_stmt::fetch.
*
* @link http://php.net/manual/en/mysqli-stmt.get-result.php
* @link https://stackoverflow.com/questions/8321096/call-to-undefined-method-mysqli-stmtget-result
*/
$result = $statement->get_result();
/*
* Fetch data and save it into an array:
*
* Array
* (
* [id] => 10
* [name] => Michael
* [age] => 18
* )
*
* @link https://secure.php.net/manual/en/mysqli-result.fetch-array.php
*/
$user = $result->fetch_array(MYSQLI_ASSOC);
/*
* Free the memory associated with the result. You should
* always free your result when it is not needed anymore.
*
* @link http://php.net/manual/en/mysqli-result.free.php
*/
$result->close();
/*
* Close the prepared statement. It also deallocates the statement handle.
* If the statement has pending or unread results, it cancels them
* so that the next query can be executed.
*
* @link http://php.net/manual/en/mysqli-stmt.close.php
*/
$statement->close();
/*
* Close the previously opened database connection.
*
* @link http://php.net/manual/en/mysqli.close.php
*/
$connection->close();
// Reference the values of the fetched data.
echo 'User id is ' . $user['id'] . '<br/>';
echo 'User name is ' . $user['name'] . '<br/>';
echo 'User age is ' . $user['age'] . '<br/>';
选项 2) 获取多条记录:
<?php
require 'connection.php';
$id1 = 10;
$id2 = 11;
$sql = 'SELECT
id,
name,
age
FROM users
WHERE
id = ?
OR id = ?';
$statement = $connection->prepare($sql);
$statement->bind_param('ii', $id1, $id2);
$statement->execute();
$result = $statement->get_result();
/*
* Fetch data and save it into an array:
*
* Array
* (
* [0] => Array
* (
* [id] => 10
* [name] => Michael
* [age] => 18
* )
*
* [1] => Array
* (
* [id] => 11
* [name] => Harry
* [age] => 59
* )
* )
*
* @link http://php.net/manual/en/mysqli-result.fetch-all.php
*/
$users = $result->fetch_all(MYSQLI_ASSOC);
$result->close();
$statement->close();
$connection->close();
// Reference the values of the fetched data.
foreach ($users as $key => $user) {
echo 'User id is ' . $user['id'] . '<br/>';
echo 'User name is ' . $user['name'] . '<br/>';
echo 'User age is ' . $user['age'] . '<br/>';
echo '<hr/>';
}
connection.php:
<?php
// Db configs.
define('HOST', 'localhost');
define('PORT', 3306);
define('DATABASE', 'tests');
define('USERNAME', 'root');
define('PASSWORD', 'root');
/*
* Error reporting.
*
* Also, define an error handler, an exception handler and, eventually,
* a shutdown handler function to handle the raised errors and exceptions.
*
* @link https://phpdelusions.net/articles/error_reporting Error reporting basics
* @link http://php.net/manual/en/function.error-reporting.php
* @link http://php.net/manual/en/function.set-error-handler.php
* @link http://php.net/manual/en/function.set-exception-handler.php
* @link http://php.net/manual/en/function.register-shutdown-function.php
*/
error_reporting(E_ALL);
ini_set('display_errors', 1); /* SET IT TO 0 ON A LIVE SERVER! */
/*
* Enable internal report functions. This enables the exception handling,
* e.g. mysqli will not throw PHP warnings anymore, but mysqli exceptions
* (mysqli_sql_exception).
*
* MYSQLI_REPORT_ERROR: Report errors from mysqli function calls.
* MYSQLI_REPORT_STRICT: Throw a mysqli_sql_exception for errors instead of warnings.
*
* @link http://php.net/manual/en/class.mysqli-driver.php
* @link http://php.net/manual/en/mysqli-driver.report-mode.php
* @link http://php.net/manual/en/mysqli.constants.php
*/
$mysqliDriver = new mysqli_driver();
$mysqliDriver->report_mode = (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// Create a new db connection.
$connection = new mysqli(HOST, USERNAME, PASSWORD, DATABASE, PORT);
// Set the desired connection charset
$connection->set_charset('utf8mb4');
测试数据:
id name age
---------------
9 Julie 23
10 Michael 18
11 Harry 59
创建表语法:
CREATE TABLE `users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
关于php - 如何使用bind_param php mysqli选择行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49355934/
这个问题已经有答案了: php, mysql - Too many connections to database error (6 个回答) 已关闭 8 年前。 伙计们,我的 opencart 网站
这是我想要的,但不能让它与新的 MySQLi 一起工作,只是因为我的主机没有所有新的 php 等... 但是一定有某种解决方案,或者这就是 MYSQLI 能做的? 请不要谈论 PDO,因为即使是丑陋的
这个问题在这里已经有了答案: Is there a way to see a prepared query as it will be executed on the database? [dupli
Warning: mysqli::query(): Couldn't fetch mysqli in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\
Warning: mysqli::query(): Couldn't fetch mysqli in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\
Warning: mysqli::query(): Couldn't fetch mysqli in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\
如何在扩展类中实现 mysqli? 我正在上传图像并将其存储在 MySQL 数据库中,但出现此错误: Notice: Undefined variable: mysqli in ...ecc/ecc/
我终于切换到 mysqli 了。 但是我发现了显着的性能差异。 我有一个脚本,可以进行大约 25.000 个查询。该脚本使用 mysqli 和 mysqlnd 作为驱动程序需要 15 秒10 秒使用
这个问题已经有答案了: Why can't I run two mysqli queries? The second one fails [duplicate] (2 个回答) 已关闭 4 年前。 我
这个问题已经有答案了: mysqli::query(): Couldn't fetch mysqli (4 个答案) 已关闭 6 年前。 这是我当前的代码: query("SELECT * FROM
这个问题在这里已经有了答案: What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must
我正在尝试连接到我的数据库,但当我使用 127.0.0.1 而不是本地主机时出现错误。 Warning: mysqli::mysqli(): (HY000/2002): A connection at
使用 WHM 更新 mysql 后我的网站面临这个问题。 Warning: mysqli::mysqli(): Headers and client library minor version mis
我试图通过 MySQLi 将简单数据插入到我的 MySQL 表中,但是它拒绝插入,并且没有报告任何错误消息。 我想强调的是,当直接输入 PhpMyAdmin 时,此查询功能正常(当然,替换了变量) r
我正在使用用户断言函数,例如: debug_assert ( gettype($ob)=='object', "Not an object " .print_r($ob
我有一个每天只有大约 100 人访问的站点,但是当我以用户身份登录时收到此错误消息: Warning: mysqli::mysqli() [mysqli.mysqli]: (42000/1203):
我创建了一个 foreach 循环来将数据添加到 MySQL 数据库,但在将第一行添加到数据库后,我收到错误“mysqli::query(): Couldn't fetch mysqli”。 PHP
我正在使用 mysqli 实现一些基本的 getter 函数,并且正在考虑一种在错误检查中变得懒惰但仍然正确的方法。 所以我写了这种类型的代码段 if(!mysqli_stmt_bind_param(
我已经阅读了在线 php 手册,但我仍然不确定这两个函数的工作方式:mysqli::commit 和 mysqli::rollback。 我要做的第一件事是: $mysqli->autocommit(
这个问题在这里已经有了答案: What is the difference between single-quoted and double-quoted strings in PHP? (7 个答
我是一名优秀的程序员,十分优秀!