<?php
function get_user_id($username) {
return mysql_result(mysql_query("Select id From users Where username = '" . mysql_real_escape_string($username) . "'"), 0);
}
$sql = "select * from rating
WHERE user_id=" . get_user_id($myusername) . "
ORDER BY punkte ASC";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
$catid = $row['rating_id'];
$catname = $row['song_id'];
echo "<li id='item_$catid' class='ui-state-default'><span class='ui-icon ui-icon-arrowthick-2-n-s'></span>$catname</li>";
}
?>
UPDATE:
Sorry, I found the mistake, it was quite stupid:
$catid = $row['rating_id'];
$catname = $row['song_id'];
It should be:
$catid = $row['song_id'];
$catname = $row['song_name'];
So, thanks to all! As always: You can't figure it out before you post a question to Stackoverflow :)
For your new question, you can solve it with a inner join:
select s.song_name from rating r
inner join songs s on s.song_name_id = r.song_id
Inner join
http://www.w3schools.com/sql/sql_join_inner.asp
Yes,
$catid = $row['rating_id']; //rating id is not related to song
TO
$catid = $row['song_id'];
$catname = $row['song_name'];
Related
I am building an android app that uses geo location. I am trying to improve my overall app to improve its smoothness while running. I am using volly to connect to a php page on my web sever where the php page can then access my phpmyadmin database. My php page for updating locations is a horrible mess and I was hoping it can be fixed with the right sql query.
Lets get down to it.
So I have a table named users
and a table named friends
In this particular example david is friends with mark and jack. Also to clarify mark and jack are friends with david.
What I need to do is Write a query if given a user ID say for example 3 that will produce a table of that person and his friends ID, cordsV1, cordsV2 without any duplicate IDs in the table.
I was able to get this to work with using loops and variables ect but as I said it is a horrible mess.
Here is my current all sql query attempt:
SELECT DISTINCT ID, cordsV1, cordsV2 FROM `friends`,`users` WHERE user_one_ID = 1 AND status = 1;
HOWEVER this just returns all of the user IDs from the user table. I am really bad with sql so if someone could point me in the right direction it would be much appreciated.
Here is my horrible mess of code if you were wondering:
<?php error_reporting(E_ALL | E_STRICT); ?>
<?php
$THIS_USER_ID = $_GET['THIS_USER_ID'];
try {
$one = 1;
$db = new PDO("");
$sql = "SELECT * FROM friends WHERE user_one_ID = '" . $THIS_USER_ID . "' AND status = '" . $one . "' OR user_two_ID = '" . $THIS_USER_ID . "' AND status = '" . $one . "'";
$rows = $db->query($sql)
->fetchAll(PDO::FETCH_ASSOC);
$printMe = [];
foreach($rows as $row){
$printMe[] = $row;
}
$jsonArr = json_encode($printMe);
$characters = json_decode($jsonArr, true);
// Getting the size of the sample array
$size = sizeof($characters);
$neg = -1;
$sql2 = "SELECT * FROM users WHERE ID = '" . $neg . "'";
$sql3 = "";
$sql4 = "";
for ($x = 0; $x < $size; $x++ ){
if ($characters[$x]['user_one_ID'] == $THIS_USER_ID && $characters[$x]['status'] == 1){
$hold = $characters[$x]['user_two_ID'];
$sql3 = $sql3 . " OR ID = '" . $hold . "'";
} else if($characters[$x]['user_two_ID'] == $THIS_USER_ID && $characters[$x]['status'] == 1) {
$hold = $characters[$x]['user_one_ID'];
$sql4 = $sql4 . " OR ID = '" . $hold . "'";
}
}
$sql5 = $sql2 . $sql3 . $sql4;
$sql7 = "SELECT * FROM users WHERE ID = '" . $THIS_USER_ID . "'";
$printMe2 = [];
$rows3 = $db->query($sql7)
->fetchAll(PDO::FETCH_ASSOC);
foreach($rows3 as $row3){
$printMe2[] = $row3;
}
$rows2 = $db->query($sql5)
->fetchAll(PDO::FETCH_ASSOC);
foreach($rows2 as $row2){
$printMe2[] = $row2;
}
$jsonArr2 = json_encode($printMe2);
echo $jsonArr2;
$db = null;
} catch(PDOException $ex) {
die(json_encode(array('outcome' => false, 'message' => 'Unable to connect')));
}
?>
Get the user-data
SELECT
*
FROM
users
WHERE ID = ?
Get the user-data of friends
SELECT
users.*
FROM
friends
JOIN
users ON users.ID = friends.user_two_ID
WHERE
friends.user_one_ID = ?
Better use prepared statements, or your app wont be alive very long due to SQL-Injections.
You also want to have a look at meaningful names.
I have two tables - home and posts;
home.artid is equal to posts.id.
Want select id, artid, inde from home PLUS title from posts
where home.pos is slider:
$items = "";
$st = $db->query("select home.id, home.artid, home.inde
from home
join posts on home.artid = posts.id
where home.pos = 'slider'
order by home.inde asc");
while ($row = $st->fetch()){
$items .= "<div class='slidertitle'>" . $row['posts.title'] . "</div>\n";
}
echo $items;
Error:
Undefined index posts.title...
Any help?
If you want to select posts.title then select it
$st = $db->query("select home.id, home.artid, home.inde,
posts.title
from home
join posts on home.artid = posts.id
where home.pos = 'slider'
order by home.inde asc");
// and it would be called just `title`
while ($row = $st->fetch()){
$items .= "<div class='slidertitle'>" . $row['title'] . "</div>\n";
}
echo $items;
I have 3 tables:
Table: album
Columns: id, name, description, author, path, image
Table: albumconnect
Columns: id, imageid, albumid
Table: albumimages
Columns: id, path
And I'm trying to replace all those unnecessary queries with a single JOIN query:
<?php
$albumID = $_SERVER['QUERY_STRING'];
$realAlbumID = substr($albumID, 1);
$realestAlbumID = str_replace('%20', ' ', $realAlbumID);
$sql = "SELECT * FROM album WHERE id='$realestAlbumID'";
$result = mysqli_query($conn, $sql);
$getResult = mysqli_fetch_assoc($result);
$albumPath = $getResult['path'];
$sql2 = "SELECT * FROM albumconnect WHERE albumid='$realestAlbumID'";
$result2 = mysqli_query($conn, $sql2);
while ($row = $result2->fetch_assoc()){
$imageId = $row['imageid'];
$sql3 = "SELECT * FROM albumimages WHERE id='$imageId'";
$result3 = mysqli_query($conn, $sql3);
$getResult3 = mysqli_fetch_assoc($result3);
$imagePath = $getResult3['path'];
echo '<div class="imageContainerAlbums"><li class="listAlbums"><img class="specificAlbumThumnails" src="'.$albumPath.$imagePath.'" alt="Random image" /></li></div>';
};
?>
Now the JOIN query I've come up with based on stuff I've read online is this:
$sql5 = "SELECT * FROM album
JOIN albumconnect ON albumconnect.albumid=album.id
JOIN albumimages ON albumimages.id=albumconnect.imageid
WHERE id='$realestAlbumID'";
$result5 = mysqli_query($conn, $sql5);
However, when I try to var_dump the contents, it prints Null so I assume my query is incorrect but I can't figure out the correct way to do it.
Should be something like this. I didn't test it. As #Difster correctly said, SQL engine doesn't know which id should it reference. So, define table aliases and prefix the referenced columns with them. Then define unique aliases for the column names too. Otherwise your sql statement were almost perfect.
<?php
$albumID = $_SERVER['QUERY_STRING'];
$realAlbumID = substr($albumID, 1);
$realestAlbumID = str_replace('%20', ' ', $realAlbumID);
$sql = "SELECT
alb.name AS album_name,
alb.description AS album_description,
alb.author AS album_author,
alb.path AS album_path,
alb.image AS album_image,
ali.path AS image_path
FROM album AS alb
LEFT JOIN albumconnect AS alc ON alc.albumid = alb.id
LEFT JOIN albumimages AS ali ON ali.id = alc.imageid
WHERE alb.id = '$realestAlbumID'";
$result = mysqli_query($conn, $sql);
while ($row = $result->fetch_assoc()) {
$albumPath = $row['album_path'];
$imagePath = $row['image_path'];
echo '<div class="imageContainerAlbums">';
echo '<li class="listAlbums">';
echo '<img class="specificAlbumThumnails" src="' . $albumPath . $imagePath . '" alt="Random image" />';
echo '</li>';
echo '</div>';
}
EDIT 1:
The columns from the sql statement which you don't use later are optional. So, you don't need to select all columns if you don't need them later.
It maybe that you are also becoming rows with NULL values for alc or ali tables. It means that not all albums have images. Then you must give us values you have in the tables, so that we can provide you the proper further WHERE conditions like WHERE ali IS NOT NULL. This answer of me is just the starting point for you.
EDIT 2:
This version is ok too. I just changed the sql statement.
<?php
$albumID = $_SERVER['QUERY_STRING'];
$realAlbumID = substr($albumID, 1);
$realestAlbumID = str_replace('%20', ' ', $realAlbumID);
$sql = "SELECT
alb.name AS album_name,
alb.description AS album_description,
alb.author AS album_author,
alb.path AS album_path,
alb.image AS album_image,
ali.path AS image_path
FROM albumimages AS ali
LEFT JOIN albumconnect AS alc ON alc.imageid = ali.id
LEFT JOIN album AS alb ON alb.id = alc.albumid
WHERE alb.id = '$realestAlbumID'";
$result = mysqli_query($conn, $sql);
while ($row = $result->fetch_assoc()) {
$albumPath = $row['album_path'];
$imagePath = $row['image_path'];
echo '<div class="imageContainerAlbums">';
echo '<li class="listAlbums">';
echo '<img class="specificAlbumThumnails" src="' . $albumPath . $imagePath . '" alt="Random image" />';
echo '</li>';
echo '</div>';
}
I've asked this question before, but got no answers, so I'm asking it again but this time, I will be more specific.
I have an online forum which I created from scratch with php and mysql, I've implemented the image uploading part naming the image by the topic id, from the posts table, Now I'm having problems displaying the images by pulling the name and the extension from the image table and attaching it to the topic id to be displayed. Now this is the code snippet for displaying topics (viewtopic.php)
$sql = "
SELECT SQL_CALC_FOUND_ROWS p.id
, p.subject
, p.body
, p.date_posted
, p.date_updated
, u.name as author
, u.id as author_id
, u.signature as sig
, c.count as postcount
, p.forum_id as forum_id
, f.forum_moderator as 'mod'
, p.update_id
, u2.name as updated_by
FROM forum_forum f
JOIN forum_posts p
ON f.id = p.forum_id
JOIN forum_users u
ON u.id = p.author_id
LEFT
JOIN forum_users u2
ON u2.id = p.update_id
LEFT
JOIN forum_postcount c
ON u.id = c.user_id
WHERE $topicid IN (p.topic_id,p.id)
ORDER
BY p.topic_id
, p.date_posted
LIMIT $start,$limit;
";
$result = mysqli_query($sql, $conn)
or die(mysql_error() . "<br>" . $sql);
while ($row = mysqli_fecth_array($result) )
{
echo "<p>($body) . "</p>";
echo $sig;
}
Now after echo ($body) if I run this query;
$sql = "SELECT * FROM images WHERE name = '$name'";
$result = mysqli_query($sql) or die('Could not SELECT image data ' . mysql_error());
while ($therow = mysql_fetch_array($result))
{
$image_name = $therow["name"] ;
$ext = $therow["extension"];
}
?>
<img src="images/<?php echo $image_name.$ext; ?>" >
Help me, how do i get images to be displayed?
Try replace this:
while ($row = mysqli_fecth_array($result) )
{
echo "<p>($body) . "</p>";
echo $sig;
}
to
while ($row = mysqli_fetch_array($result) )
{
echo "<p>($body)" . "</p>";
echo $sig;
}
and this
$sql = "SELECT * FROM images WHERE name = '$name'";
$result = mysqli_query($sql) or die('Could not SELECT image data ' . mysql_error());
while ($therow = mysql_fetch_array($result))
{
$image_name = $therow["name"] ;
$ext = $therow["extension"];
}
?>
to
$sql = "SELECT * FROM images WHERE name = '$name'";
$result = mysqli_query($sql) or die('Could not SELECT image data ' . mysql_error());
while ($therow = mysqli_fetch_array($result))
{
$image_name = $therow["name"];
$ext = $therow["extension"];
}
?>
this is my code, it displays all the categories like
A
-A.1
-A.2
B
-B.1
-B.2
And now i want it displays only
A.1
A.2
B.1
B.2
Any idea?
function current_categories($m,$id){
$cat_array=array();
$sql = "select * from `category`";
$query = DB::Query($sql);
while($row = DB::fetch_array($query)){
$cat_array[] = array($row['id'],$row['name'],$row['classid'],$row['sort']);
}
if($id==""){
$id=0;
}
$n = str_pad('',$m,'-',STR_PAD_LEFT);
$n = str_replace("-"," ",$n);
for($i=0;$i<count($cat_array);$i++){
if($cat_array[$i][2]==$id){
echo "<li>"."".$cat_array[$i][1]."</li>";
current_categories($m+2,$cat_array[$i][0]);
}
}
}
I don't know what value you put in as classid for the toplevel classid values. NULL or -1 perhaps?
function second_level_categories(){
$sql = " SELECT lvl2.* FROM `category` as lvl1 "
." JOIN `category` as lvl2"
." ON lvl1.id = lvl2.parent_id"
." WHERE lvl1.parent_id=VALUE_FOR_TOP_LEVEL"
." ORDER BY lvl1.sort ASC, lvl2.sort ASC"
$result = DB::Query($sql);
while($category = DB::fetch_array($result)){
echo '<li><a href="/team/index.php?gid='.$category['id'].'">"';
echo $category['name'];
echo '</a></li>';
}
}