Mysql 搜索查询问题
Mysql query issue with search
在我之前的个人资料视图中,它会抓取搜索到的 ID 并使用以下查询显示它:
$dn = mysql_query('select firstname, lastname, age, id, background from users where id="'.$id.'"');
然而,我目前的做法是通过搜索 url 查看活动页面。所以 url 是 socialnetwk 所以
$dn = mysql_query('select eventname, about, url, comment, post, msg, member_id, author_id, id from events where url="'.$id.'"');
我不确定如何解决这个问题,因为我已经使用了 sequel Pro,而且我似乎需要在 url 名称周围加上“ ”。我怎么不知道如何将其包含在查询中
URL是一列,不是实际的URL
代码如下:
<?php
//We check if the users ID is defined
if(isset($_GET['id']))
{
$id = intval($_GET['id']);
//We check if the user exists
$dn = mysql_query('select eventname, about, url, comment, post, msg, member_id, author_id, id from events where url="'.$id.'"');
if(mysql_num_rows($dn)>0)
{
$dnn = mysql_fetch_array($dn);
//We display the user datas
if($dnn['id']!='')
{
}
else
{
echo 'This user dont have an avatar.';
}
?>
已更新以匹配您编辑的代码:
在使用准备语句的情况下:
<?php
//We check if the users ID is defined
if(isset($_GET['id'])){
$id = intval($_GET['id']);
//We check if the user exists
$dn = 'select eventname, about, comment, post, msg,
member_id, author_id, id from events where url=?';
if($stmt=$dbc->prepare($dn)){
$stmt->bind_param('s',$id); //your URL is a string
$stmt->execute(); //returns false if fails
$stmt->bind_result($eventname, $about, $comment, $post,
$msg, $member_id, $author_id, $id); //don't need to
//bind the url, since you already know it
$stmt->fetch();
$stmt->free_result();
if($stmt->num_rows>0) {
//We display the user datas
echo "$eventname, $about, $comment ..."; // the bound results
}
$stmt->close();
$dbc->close();
}
if($dnn['id']!=''){
// do something here
} else {
echo 'This user dont have an avatar.';
}
?>
这假定 $dbc
是您的数据库连接。
*注意,您将 $_GET['id'] 值更改为整数,但它也是一个 URL(字符串)。这需要协调才能使您的代码正常运行 *
在我之前的个人资料视图中,它会抓取搜索到的 ID 并使用以下查询显示它:
$dn = mysql_query('select firstname, lastname, age, id, background from users where id="'.$id.'"');
然而,我目前的做法是通过搜索 url 查看活动页面。所以 url 是 socialnetwk 所以
$dn = mysql_query('select eventname, about, url, comment, post, msg, member_id, author_id, id from events where url="'.$id.'"');
我不确定如何解决这个问题,因为我已经使用了 sequel Pro,而且我似乎需要在 url 名称周围加上“ ”。我怎么不知道如何将其包含在查询中
URL是一列,不是实际的URL
代码如下:
<?php
//We check if the users ID is defined
if(isset($_GET['id']))
{
$id = intval($_GET['id']);
//We check if the user exists
$dn = mysql_query('select eventname, about, url, comment, post, msg, member_id, author_id, id from events where url="'.$id.'"');
if(mysql_num_rows($dn)>0)
{
$dnn = mysql_fetch_array($dn);
//We display the user datas
if($dnn['id']!='')
{
}
else
{
echo 'This user dont have an avatar.';
}
?>
已更新以匹配您编辑的代码:
在使用准备语句的情况下:
<?php
//We check if the users ID is defined
if(isset($_GET['id'])){
$id = intval($_GET['id']);
//We check if the user exists
$dn = 'select eventname, about, comment, post, msg,
member_id, author_id, id from events where url=?';
if($stmt=$dbc->prepare($dn)){
$stmt->bind_param('s',$id); //your URL is a string
$stmt->execute(); //returns false if fails
$stmt->bind_result($eventname, $about, $comment, $post,
$msg, $member_id, $author_id, $id); //don't need to
//bind the url, since you already know it
$stmt->fetch();
$stmt->free_result();
if($stmt->num_rows>0) {
//We display the user datas
echo "$eventname, $about, $comment ..."; // the bound results
}
$stmt->close();
$dbc->close();
}
if($dnn['id']!=''){
// do something here
} else {
echo 'This user dont have an avatar.';
}
?>
这假定 $dbc
是您的数据库连接。
*注意,您将 $_GET['id'] 值更改为整数,但它也是一个 URL(字符串)。这需要协调才能使您的代码正常运行 *