1 to many mysql statment plus showing images and text - php

Hi everyone I have a little problem with this one to many statment. I am adding images in the mysql database and in a folder that part is perfect. But when I want to display them that is my tricky part. I don't know where is my problem in the way I've created the database or in my code. So here are both:
My two tables are text and imagess
In the text table:
text_id - primary
text_field - unique
In the imagess table:
id - primary
name
title
path
My code is this:
<?php
$result = mysql_query("SELECT * FROM text text_id, imagess id WHERE (text_id=id AND text_field=title) ");
while($row = mysql_fetch_assoc($result)){
echo '<br>№'.$row['text_id'].'<br>';
echo''.$row['title'].'<br>'
.$row['text_field'];
echo'<br><img src="'.$row['path'].'?url='.$row['path'].'/>';
}
?>
My goal is to learn how to display the 1 title + text with 2-3 images. I hope someone could explain me where are my mistakes because from the book I am reading there is not such thing.
Thank you in advance.

In a 1-to-many relation, all you have to ignore multiples occurences of the "1".
You also have to join your imagess table on a foreign key.
There are errors in your query, "FROM text text_id" means you create an alias on text named text_id.
<?php
$currentTextId = -1;
$result = mysql_query("SELECT * FROM text, imagess WHERE (text.text_id=imagess.text_id) ORDER BY text.text_id");
while($row = mysql_fetch_assoc($result)){
if($row['text_id']!=$currentTextId){
$currentTextId=$row['text_id']
echo '<br>№'.$row['text_id'].'<br>';
}
echo''.$row['title'].'<br>'.$row['text_field'];
echo'<br><img src="'.$row['path'].'?url='.$row['path'].'/>';
}
?>

Related

How to detect url from plain text from mysql table and echo in table row

Hello Everyone,
I"m retrieving values from column called 'comment' of mysql table by echo. But This column has both plain text and some url also. When displaying value, I"m looking forward to identify the link from text and make it clickable.
Here is my code
$postid= $_GET['post_id'];
$sql = mysql_query("SELECT * from tblcomment as c join tbluser as u on c.user_Id=u.user_Id where post_Id='$postid' order by datetime");
$num = mysql_num_rows($sql);
if($num>0){
while($row=mysql_fetch_assoc($sql)){
echo "<p class='well'>".$row['comment']."</p>";
}
}
Thanks in advance.

join issue MYSQL/PHP

i have a bit of a problem, ive never used JOIN before, and this is the first time, and i got into some problems:
<?php
//$count to keep the counter go from 0 to new value
$count=0;
//I need to select the level for the users building first, meanwhile i also
//need to get the money_gain from the table buildings, which is a table that
//is common for each member, which means it doesnt have a userid as the other table!
//And then for each of the buildings there will be a $counting
$qu1 = mysql_query("SELECT building_user.level,buildings.money_gain FROM building_user,buildings WHERE building_user.userid=$user");
while($row = mysql_fetch_assoc($qu1))
{
$count=$count+$row['level'];
echo $row['level'];
}
?>
So basically ive heard that u should tie them together with a common column but, in this case thir isnt any.. im just lost right now?
EDIT Oh right, I need the right level to be taken out with the correct money_gain too, in building_user its 'buildingid'and in buildings, its just 'id'! have no idea how to make a common statement though!
From your edit,
SELECT building_user.level,buildings.money_gain FROM building_user,buildings WHERE building_user.userid=$user AND building_user.buildingid = building.id;
You essentially get the records for the user joining them at the building id
Performance-wise, joins are a better choice but for lightweight queries such as this, the query above should work fine.
You can also give each column a neater name
SELECT building_user.level as level,buildings.money_gain as money gain FROM building_user,buildings WHERE building_user.userid=$user AND building_user.buildingid = building.id;
and access them as
$level = $row['level'];
$gain = $row['gain'];
i think you problem is, that MySQL doesn't know how to JOIN the two Tables. Therefor you have to tell MySQL how to do that.
Example
SELECT * FROM TABLE1 INNER JOIN Table1.col1 ON TABLE2.col2;
where col1 and col2 are the columns to join (a unique identifier)
<?php
$count=0;
$qu1 = mysql_query("SELECT bu.level, bs.money_gain FROM building_user bu, buildings bs WHERE bu.userid=$user");
while($row = mysql_fetch_assoc($qu1))
{
$count=$count+$row['level'];
echo $row['level'];
}
?>
Try this
$qu1 = mysql_query("SELECT building_user.level,buildings.money_gain
FROM building_user
JOIN buildings ON building_user.building_id = buildings.id
WHERE building_user.userid=$user");
building_user.building_id is the foreght key of table building_user and
buildings.id is the primary key of table buildings

PHP MYSQL looking for a way to group rows that have same value in certain column

I have a table named records with the columns: recordid, artist, album, description and coverimg. Some of the albums in the table have the same artist.
On my site I would like to display it like this:
-artist1
album1
album2
- artist2
album3
-artist3
album4
album5
If I use GROUP BY it just shows one row per artist. Is there any other way to group them without resorting to relational tables?
Because now I have a form to insert and update the table through my site, and I have no idea how I would have to code the forms to make this work with relational tables.
try this
select artist,group_concat(album) from albums group by artist
gives you
artist1 | album1,album2,album3
artist2 | albumx,albumy
You can split the second column in PHP e.g. using explode(row[1])
That should help you a bit
You can do it without a group by:
<?php
$query = 'Select artist, album
FROM records
ORDER BY artist'
$result = mysql_query($query);
$artist = '';
while ($line = mysql_fetch_assoc($result)){
// only show artist when it's an other artist then the previous one
if ($line['artist'] != $artist){
echo $line['artist'].'<br/>';
$artist = $line['artist'];
}
echo $line['album'].'<br/>';
}
?>
I know I should not be using mysql_* functions anymore, please choose mysqli_* or PDO....

Remove mySQL row if 1 field is found

I have a mySQL table called "clients" with this structure:
Name | Phone | ID
Now I am using $_GET to extract an ID from a URL, then I would like to delete the entire row (name, phone and ID) from the database that has that ID.
How woudl I do this last part (selecting the row and removing it)? I've tried some stuff but I am starting with php and mysql and it's kind of confusing.
Thank you for your help.
I hope this could help you out to understand better :)
<?php
$Action=$_REQUEST['a'];
$ID=$_REQUEST['id'];//i assume your ID's are numeric so to avoid injections check for numeric
if(is_numeric($ID)){
if($Action=="del" and $ID){
mysql_query("DELETE FROM clients WHERE id=".$ID);
}
$q=mysql_query("SELECT * FROM clients WHERE id=".$ID);
while($qd = mysql_fetch_array($q)){
echo "Name: ".$qd['name']."<br>Phone: ".$qd['phone']."<br><a href='?a=del&id=".$qd['id']."'>Delete</a><br><br>";
}
}
?>

Foreach or Inner join? -- that is the PHP question

I have 2 tables. One (artWork) with all the data I want to pull from, including 2 cols of id's. The other (sharedWork) has the same 2 id cols that are also in the first -- but none of the essential data I want to echo out. Objective: use the id's in both table to filter out row in the first (artWork). See below in the code what I tried that didn't work
I also tried to figure out an inner join that would accomplish the same. No luck there either. Wondering which would be the best approach and how to do it.
thanks
Allen
//////// Get id's first ///////////
$QUERY0="SELECT * FROM artWork WHERE user_id = '$user_id' ";
$res0 = mysql_query($QUERY0);
$num0 = mysql_num_rows($res0);
if($num0>0){
while($row = mysql_fetch_array($res0)){
$art_id0 = $row['art_id'];
}
}
$QUERY1="SELECT * FROM shareWork WHERE user_id = '$user_id' ";
$res1 = mysql_query($QUERY1);
$num1 = mysql_num_rows($res1);
if($num1>0){
while($row = mysql_fetch_array($res1)){
$art_id = $row['art_id'];
}
}
$art_id2 = array_merge($art_id0, $art_id1);
foreach ($art_id2 as $art_id3){
$QUERY="SELECT * FROM artWork WHERE art_id = '$art_id3' ";
// echo "id..".$art_id0;
$res = mysql_query($QUERY);
$num = mysql_num_rows($res);
if($num>0){
while($row = mysql_fetch_array($res)){
$art_title = $row['art_title'];
$art_id = $row['art_id'];
etc................and so on
.........to....
</tr>";
}
}
}
Don't query your database inside a loop unless you absolutely have to.
Everytime you query the database, you're using disk I/O to read through the database and return your record. Disk I/O is the slowest read on a computer, and will be a massive bottleneck for your application.
If you run larger queries upfront, or at least outside of a loop, you will hit your disk less often, improving performance. Your results from larger queries will be held in memory, which is considerably faster than reading from disk.
Now, with that warning out of the way, let's address your actual problem:
It seems you're trying to grab records from artWork where the user is the primary artist, or the user was one of several artists to work on a group project. artWork seems to hold the id of the primary artist on the project whereas shareWork is probably some sort of many-to-many lookup table which associates user ids with all art projects they were a part of.
The first thing I should ask is whether or not you even need the first query to artWork or if the primary artist should have a record for that art_id in shareWork anyway, for having worked on the project at all.
If you don't need the first lookup, then the query becomes very easy: just grab all of the users art_ids from shareWork table and use that to lookup the his or her records in the main artWork table:
SELECT artWork.*
FROM artWork
WHERE art_id IN
(SELECT art_id
FROM shareWork
WHERE user_id = $user)
If you do need to look in both tables, then you just add a check in the query above to also check for that user in the artWork table:
SELECT artWork.*
FROM artWork
WHERE
user_id = $user
OR art_id IN
(SELECT art_id
FROM shareWork
WHERE user_id = $user)
This will get you all artWork records in a single query, rather than.. well, a lot of queries, and you can do your mysql_fetch_array loop over the results of that one query and be done with it.

Categories