[英]write sql query in php
the following sql query works perfectly in phpAdmin. 以下sql查询可在phpAdmin中完美运行。
SELECT response_text FROM statement_responses WHERE response_code = "s1_r1"
it doesn't work without the speech marks around response_code value. 没有response_code值周围的语音标记,该功能将不起作用。
I'm attempting to use the value of response_text in php. 我正在尝试在php中使用response_text的值。
for ($x = 1; $x <= 9; $x++) {
$user_response = ${"statement_" . $x . "_response"};
$sql = "SELECT response_text FROM statement_responses WHERE response_code = \"$user_response\"";
$result = mysqli_query($sql);
$value = mysqli_fetch_object($result);
echo $user_response . "<br>";
echo $sql . "<br>";
echo $result . "<br>";
echo $value . "<br>";
}
The echoes allow me to see what each of the variables contains. 回声使我可以看到每个变量包含的内容。
I get the following: 我得到以下内容:
s1_r3 (the value of $user_response)
SELECT response_text FROM statement_responses WHERE response_code = "s1_r3" (the value of $sql - which is identical to the phpAdmin query that works.)
There are no values echoed for $result
or $value
. 没有为
$result
或$value
回应的$value
。
What am I doing wrong, please? 请问我做错了什么? Why am I not getting the values from the database into my php code?
为什么我不能将数据库中的值输入到我的PHP代码中?
The first parameter for the mysqli_query
should be the database connection created with mysqli_connect
. mysqli_query
的第一个参数应该是使用mysqli_connect
创建的数据库连接。 Similarily, the first parameter for the mysqli_fetch_object
, should be the result set identifier returned by mysqli_query
. 同样,
mysqli_fetch_object
的第一个参数应该是mysqli_query
返回的结果集标识符。
It is a good practice to check the return values from the functions you call. 优良作法是检查所调用函数的返回值。
firstly, thank you for all of the responses. 首先,感谢您的所有答复。 The following code works - ie it returns the value contained in 'response_text' column with the selected row identified by 'response_code' for use as the value of $response_text.
以下代码有效-即,它返回“ response_text”列中包含的值,并用“ response_code”标识选定行,以用作$ response_text的值。
for ($x = 1; $x <= 9; $x++) {
$user_response_pre = ${"statement_" . $x . "_response"};
$user_response = "\"'" . $user_response_pre . "'\"";
$sql = "SELECT response_text FROM statement_responses WHERE response_code = $user_response";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
$response_text = $row[0];
${"statement_" . $x . "_complete"} .= $response_text;
}
you can write like this 你可以这样写
for ($x = 1; $x <= 9; $x++) {
$user_response = ${"statement_" . $x . "_response"};
$sql = 'SELECT response_text FROM statement_responses WHERE response_code = "'.$user_response.'"';
$result = mysqli_query($sql);
$value = mysqli_fetch_object($result);
echo $user_response . "<br>";
echo $sql . "<br>";
echo $result . "<br>";
echo $value . "<br>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.