[英]How to set result SQL distinct query to one or different variables?
I'm creating a mobile library app, and for one function of the app I am trying to receive the bookID
for all books checked out by a certain user. 我正在创建一个移动图书馆应用程序,对于该应用程序的一项功能,我试图接收某个用户签出的所有图书的bookID
。 I would like to be able to echo
back the results from the query in a string
format (preferably with spaces in between each separate book id) so I can deal with the data later on within the app. 我想能够echo
回从查询结果中string
格式(最好在每个单独的书ID之间的空格),这样我就可以在应用程序中处理数据以后。
Many of the answers I have found online have simply shown how to execute the query, but not how to use the data afterwards. 我在网上找到的许多答案只是简单地说明了如何执行查询,但没有说明之后如何使用数据。 Sorry if this is a simple question to answer, I am a huge novice. 抱歉,如果这是一个简单的问题,我是一个新手。
<?php
require "conn.php";
$email = $_POST["email"];
$mysql_qry = "SELECT * FROM user_data WHERE email like '$email'";
$mysql_qry2 = "SELECT DISTINCT(bookID) AS bookID FROM books_checked_out
WHERE userID LIKE $user_id ORDER BY bookID DESC";
$result = mysqli_query($conn, $mysql_qry);
if(mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$user_id = $row["user_id"];
$result2 = mysqli_query($conn, $mysqlqry2);
}
else
{
echo "Error, user name not found";
}
$conn->close;
?>
You could append your results into an array and display values using implode()
: 您可以将结果附加到数组中,并使用implode()
显示值:
<?php
require "conn.php";
$email = $_POST["email"]; // You may test here : if (isset($_POST['email']))
$mysql_qry = "SELECT * FROM user_data WHERE email = '$email'";
$result = mysqli_query($conn, $mysql_qry);
if(mysqli_num_rows($result) > 0)
{
$row = mysqli_fetch_assoc($result);
$user_id = $row["user_id"];
$mysql_qry2 = "SELECT DISTINCT(bookID) AS bookID FROM books_checked_out
WHERE userID = $user_id ORDER BY bookID DESC";
$result2 = mysqli_query($conn, $mysql_qry2);
if(mysqli_num_rows($result2) > 0)
{
$ids = [];
while ($row = mysqli_fetch_assoc($result2)) {
$ids[] = $row['bookID'] ;
}
echo implode(" ", $ids) ; // print list of ID
}
else
{
echo "No books checked out!";
}
}
else
{
echo "Error, user name not found";
}
$conn->close;
NB : I used your code here, but, you should have to look to parameterized queries to prevent SQL injections. 注意 :我在这里使用了您的代码,但是,您必须查看参数化查询以防止SQL注入。
Your query $mysql_qry2
should be defined after to get $user_id
. 您的查询$mysql_qry2
应该在获取$user_id
之后定义。
Your LIKE $user_id
could be replaced by =
. 您的LIKE $user_id
可以用=
代替。
First thing first, always sanitize your data: 首先,始终清理数据:
$email = filter_var( $_POST['email'], FILTER_SANITIZE_EMAIL );
$user_id = preg_replace( "#[0-9]#", '', $row['user_id'] );
DISTINCT bookID
instead of DISTINCT(bookID)
DISTINCT bookID
代替DISTINCT(bookID)
From your query: $mysql_qry2 = "SELECT DISTINCT(bookID) AS bookID FROM books_checked_out WHERE userID LIKE $user_id ORDER BY bookID DESC";
从您的查询中: $mysql_qry2 = "SELECT DISTINCT(bookID) AS bookID FROM books_checked_out WHERE userID LIKE $user_id ORDER BY bookID DESC";
If you're not getting any result or the returned result is empty but the user_id
does exist, then I think the query format is wrong. 如果您没有得到任何结果,或者返回的结果为空,但是user_id
确实存在,那么我认为查询格式是错误的。
ORDER BY
: The query may be correct but mysql returned an empty result because the result order does not match. 更改ORDER BY
:查询可能是正确的,但是mysql返回了空结果,因为结果顺序不匹配。 Try this 尝试这个
"SELECT DISTINCT bookID AS bookID FROM books_checked_out WHERE userID LIKE $user_id ORDER BY userID DESC";
"SELECT DISTINCT bookID AS bookID FROM books_checked_out WHERE userID LIKE $user_id ORDER BY `primary_key_here` DESC";
Replace <strong>`primary_key_here`</strong> with the primary key name.
Run the query without conditionals and inspect the result 在没有条件的情况下运行查询并检查结果
$query = mysqli_query( $conn, "SELECT bookID FROM books_checked_out DESC" );
var_dump( $query );
Use the result to inspect the rest of the query. 使用结果检查其余查询。
Rather than using your own protocol/format use something like JSON or xml in your response to the request. 您对请求的响应不是使用自己的协议/格式,而是使用JSON或xml之类的东西。
This will give you better maintainability in the long run and allow you to easily handle the response in the browser with javascript, and most browsers will give you a nice display of JSON objects in the dev console. 从长远来看,这将为您提供更好的可维护性,并允许您使用javascript在浏览器中轻松处理响应,并且大多数浏览器都可以在dev控制台中很好地显示JSON对象。
You'll have to extract the user id from the result of the first query or you could do a joined query instead. 您必须从第一个查询的结果中提取用户ID,或者可以执行联接查询。
$email = validate($POST['email']); //where validate() will try to prevent sql injection
//joined query
$query =
" SELECT bookID FROM user_data
INNER JOIN books_checked_out on user_data.user_id = books_checked_out.userID
WHERE user_data.email='$email'
";
//not sure whether that should be user_id or userID looks like you have mixed conventions
//books_checked_out.userID vs user_data.user_id ... check your database column names
//loop through results
// may be empty if user email doesn't exist or has nothing checked out
$result = $conn->query($query);
while($row = $result->fetch_assoc()){
$response[] = ['bookID'=>$row['bookID']];
}
echo json_encode($response);
When receiving the result in php you can use json_decode() or in javascript/ajax it will automatically be available in your result variable. 在php中收到结果时,您可以使用json_decode()或在javascript / ajax中,结果变量中将自动提供它。
if things aren't working as expected it can be a good idea to echo the actual sql. 如果事情没有按预期进行,则最好是回显实际的sql。 In this case 在这种情况下
echo 'SQL IS: '.$query;
and test it against your database directly (phpmyadmin/MySQL-Workbench) to see if you get any results or errors. 并直接针对您的数据库进行测试(phpmyadmin / MySQL-Workbench),以查看是否得到任何结果或错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.