在四个表

时间:2015-12-16 20:21:36

标签: php mysql

我有四个表j_music,j_topics,J_users,j_video,带有MySQL数据库结构:

J_music
id  artist  title   Views   Album art   Link        uploader
1   niyi    Love me 121 Love.jpg    Niyi-love.mp3    Admin
2   tolu    I am an ode 111 Toludope.jpg    Tolu-doe.mp3    admin
3   justin  Kind love   122 Justin-kind-love.jpg    Justin-kind-lovw.mp3    Admin

J_video
Id  artist  title   Downloads   video art   Link    uploader
1   vandeross   Sweet love  121 Love.jpg    Niyi-love.mp3   Admin
2   richie  Locked down 111 Toludope.jpg    Tolu-doe.mp3    Admin2
3   justin  A wonderful word    122 Justin-kind-love.jpg    Justin-kind-lovw.mp3    Admin

J_user
id  username    First name  Last name   userdp  password    country
1   niyi    faleye  niyi    Niyi-dp.jpg *****   Wales
2   tolu    abolarin    tolu    Tolu-dp.jpg ******* Nigeria
3   justin  gayle   justin  Justin-dp.jpg   ********    England

J_topics
id  title   subject Views   Topic image Date    creator
1   Justin beiber goes nude **************  121 Love.jpg    21-02-2013  mark
2   High crisis in the  ********    111 Toludope.jpg    21-02-2013  peter
3   Justin gatlin wins  ********    122 Justin-kind-love.jpg    21-02-2013  Steven

我想实现一个php和mysql搜索,在四个表中搜索特定查询“justin”,如果找到则输出结果。它应该与搜索查询匹配以下列; j_music中的艺术家和标题,j_video中的艺术家和艺术家和标题,j_users中的用户名,j_topics中的标题和主题:

(a)如果在音乐表中找到结果,则提供特殊链接 (b)如果在主题表中找到结果,则它提供特殊链接 (c)如果在视频表中找到结果,则提供特殊链接 (d)如果在users表中找到结果,则它提供一个特殊链接

结果集中有这样的东西

If found in music
$artist $title <a href=’music.php?id’>click here></a>”;

If found in video
$artist $title <a href=’video.php?id’>click here></a>”;

If found in topics
$title $subject <a href=’topic.php?topicid’>click here></a>”;

If found in users
$username $firstname <a href=’users.php?userid’>click here></a>”;

这是我用于单个表搜索和输出的代码:

echo"<form action='#' method='POST'><ul><li><input type='text' name='q' /><input type='submit' name='submit' value='Search' class='button'></center></li></ul>";
$q=$_REQUEST["q"];
if(isset($q) && !empty($q))
{
$q=trim($q);
$q=cleanvalues($q);
$q=strtoupper($q);
//echo"you searched for $q";
//$u="is";
$self=$_SERVER["PHP_SELF"];
$rowsperpage=2;
$range=2;
if(isset($_GET["currentpage"]) && is_numeric($_GET["currentpage"]))
{
$currentpage=(int)$_GET["currentpage"];
}
else
{
$currentpage=1;
}
$offset=($currentpage-1)*$rowsperpage;
$numrows=mysql_num_rows(mysql_query("SELECT * from j_users WHERE UPPER(username) LIKE '%$q%' OR UPPER(username) LIKE '%$q' OR UPPER(username) LIKE '$q%'"));
$totalpages=ceil($numrows/$rowsperpage);
if($currentpage>$totalpages)
{
$currentpage=$totalpages;
}
if($currentpage<1)
{
$currentpage=1;
}
$query=mysql_query("SELECT * from j_users WHERE UPPER(username) LIKE '%$q%' OR UPPER(username) LIKE '%$q' OR UPPER(username) LIKE '$q%' LIMIT $offset, $rowsperpage");
$num=mysql_num_rows($query);
if($num==0)
{
echo"No result found";
}
else
{
echo"Search Results For $q";
while($info=mysql_fetch_assoc($query))
{
$username=cleanvalues2($info["username"]);
$id=$info["userID"];
echo"<a href='profile.php?uid=$id'>$username</a>";
}
if($currentpage>1)
{
echo"<a href='$self?currentpage=1&q=$q'>First </a>";
$prevpage=$currentpage-1;
echo"<a href='$self?currentpage=$prevpage&q=$q'>Prev</a>";
}
for($x=($currentpage-$range); $x<(($currentpage+$range)+1); $x++)
{
if(($x>0) &&($x<=$totalpages))
{
if($x==$currentpage)
{
echo"[<font color='red'>$x</font>]";
}
else
{
echo"<a href='$self?currentpage=$x&q=$q'>[<b>$x</b>]</a>";
}
}
}
if($currentpage!=$totalpages)
{
$nextpage=$currentpage+1;
echo"<a href='$self?currentpage=$nextpage&q=$q'>Next</a>";
echo"<a href='$self?currentpage=$totalpages&q=$q'>Last</a>";
}}
}

1 个答案:

答案 0 :(得分:0)

伙计,你的代码真的不好。我建议你选择一个PHP框架并开始研究它,这将对你有所帮助。 Laravel 是我的建议。它易于学习且功能强大。

正如其他用户建议的那样,更喜欢使用 PDO 而不是 mysql _ * 功能。

如果你想做一些 SELECT ,这是一个使用 PDO MySQL 数据库的例子:

$pdo = new PDO('mysql:host=localhost;dbname=mydb', 'myuser', 'mypassword');

$artist = "Justin";

$query = $pdo->query("SELECT id, artist, title FROM j_music WHERE artist LIKE '%".$artist."%'");
$row   = $query->fetch(PDO::FETCH_ASSOC);

echo($row['artist']." ".$row['title']." <a href='music.php?'".$row['id'].">click here></a>");