Query in withing query using result - php

I've had some problems in regards to how to solve the problem of having a query based on the results of the within query, the looping one. I've tried all the ways I know, but it aborts the while loop and breaks.
It just shows the result from the query within, which tells me that the within-query DOES work, but it won't paste its information nor will it paste the other elements (such as reputation and its style).
$sql4 = mysqli_query($con, "SELECT * FROM as_sound_comments WHERE sound_id='$soundid'");
if(mysqli_num_rows($sql4) > 0){
while($row = mysqli_fetch_assoc($sql4)) {
echo "<div class='comment-content-full'>
<div class='comment-info'>
<table style='width: 98%'>
<td>
<table>
<td><p class='item-comment-info-name'><a class='blink' href='#'>";
$comment_poster_id = $row['poster_id'];
$sql5 = mysqli_query($con, "SELECT * FROM as_account WHERE id='$comment_poster_id'");
$innerrow = mysqli_fetch_assoc($sql5);
$comment_poster_username = $innerrow['username'];
$comment_poster_reputation = $innerrow['reputationlevel'];
echo $comment_poster_username;
"</a></p></td>
<td><div class='item-comment-info-reputation nlgreen'>8</div></td>
</table>
</td>
<td style='width: 20%'>
<p class='item-comment-info-posttime'><abbr class='timeago' title='< ". $row['added'] . "'></abbr></p>
</td>
</table>
</div>
<div class='comment-content-avatarandbox'>
<div class='comment-avatar'>
<img class='item-user-info-comment-avatar' src='images/test_avatar.jpg' alt='Avatar'>
<div class='arrow-left'> </div>
<div class='arrow-left-behind'> </div>
</div>
<div class='comment-box'>
<p class='comment-box-text'>" . $row['comments'] . "</p>
</div>
</div>
</div>";
}}else{ echo "sorry we didn't find no nothing";}

answering, based off comments, to close the question. Anytime that you are doing a query in a query loop, you could use a JOIN to do 1 query -
$sql4 = mysqli_query($con, "SELECT * FROM as_sound_comments LEFT JOIN as_account ON as_account.id = as_sound_comments.poster_id WHERE as_sound_comments.sound_id='$soundid'");

Related

How do I make an individual url for each ticket?

I am trying to make a forum-like website from scratch and this is the first big problem that I encountered so far, I am trying to make an individual link with ?info[id] for each ticket/topic but I simply can't, no matter what id I put in the url, I see all of them instead of the specific one, you have all the code in the video.
Video Link
My Details.php Code
<?php
require('Includes/Header.php');
$query = "SELECT * FROM forum";
$result = mysqli_query($conn ,$query);
?>
<?php while($row = mysqli_fetch_array($result)) {?>
<div class="FormAfter">
<label>ID</label><br><br>
<span><?php echo htmlspecialchars($row['id']); ?></span><br><br>
<label>Titlu</label><br><br>
<span name="Titlu"><?php echo htmlspecialchars($row['titlu']);?></span><br><br>
<label>Categorie</label><br><br>
<span><?php echo htmlspecialchars($row['categorie']); ?></span><br><br>
<label>Descriere</label><br><br>
<span name="Descriere" cols="30" rows="10" readonly><?php echo htmlspecialchars($row['descriere']);?></span>
</div>
<?php
}
mysqli_close($conn);
?>
This is my Topics.php code
<?php
require('Includes/Header.php');
$query = "SELECT * FROM forum";
$result = mysqli_query($conn ,$query);
?>
<div class="TopicListBig">
<span id="IdTitlu">ID <strong>|</strong> Titlu <strong>|</strong> Categorie</span> <br> <br> <br>
</div>
<?php
while($row = mysqli_fetch_array($result)){ ?>
<div class="RandomSpan">
<span class="TopicList"><?php echo htmlspecialchars($row['id']); ?></span>
<span class="TopicList"><?php echo htmlspecialchars($row['titlu']); ?></span>
<span class="TopicList"><?php echo htmlspecialchars($row['categorie']); ?></span>
<span class="TopicList">Info</span><br><br>
</div>
<?php
}
mysqli_close($conn);
?>
Your SQL query is explicitly fetching all records: "SELECT * FROM forum" It's the exact same query in Details as it is in Topics and nowhere in the code for Details do you make use of the id parameter in the URL's query string.
What you're looking for in that SQL query is the WHERE keyword. For example:
SELECT * FROM forum WHERE id=?
Within your WHERE clause you identify the specific filter to find the exact record(s) you want. Then you bind your value to that parameter before executing the query. While that link shows the (generally preferred) object-oriented style, you can also use the procedural style you currently use. For example:
$query = mysqli_prepare($conn, 'SELECT * FROM forum WHERE id=?');
mysqli_stmt_bind_param($query, 's', $_GET['id']);
mysqli_stmt_execute($query);
$result = mysqli_stmt_get_result($query);
while ($row = mysqli_fetch_array($result)) {
// your output
}

how to view newest replies from my followed users

i am creating a following system in my website.
now when a user follows lets say three users and each user has lets say 3-4 messages.
when one of the users replies to a new message that has been sent to him i want this message to be the top message to be displayed.
what i am getting now is that the messages will go in stacks for each user even if it is a new message.
here is my php and queries:
$uid=$_SESSION['active_user_id'];
$usersFollowingsQuery = $db->prepare("SELECT mr.*, f.* FROM messages_reply mr INNER JOIN follow f on mr.from_id = f.followed_id
WHERE follower_id = ? ORDER BY follow_id desc");
$usersFollowingsQuery->bindValue(1,$uid);
$usersFollowingsQuery->execute();
if($usersFollowingsQuery->rowCount()==0){
echo"<div class ='forgot'>Follow users to find their content here</div>";
}
else{
echo"<div class ='forgot'>This is your feed</div>";
while($row3 =$usersFollowingsQuery->fetch()){
$followed_id = $row3['followed_id'];
$usersPicQuery = $db->prepare("SELECT * FROM users WHERE id=?");
$usersPicQuery->bindValue(1,$followed_id);
$usersPicQuery->execute();
$row2 = $usersPicQuery->fetch();
$d=$row['date'];
?>
<div Class="inside-card" style="width:95%; margin-top:24px;"
<td>
<div class="msg-body">
</a>
</img>
<p style="font-size:16px; color:#696969"><?php echo $row3['question']; ?></p>
<div class="msg-action">
<div>
<a href=<?php echo "'q=" . $row2['username'] . "'" ;?>> <img src=<?php echo "'" . $row2['picture'] . "'";?> width="32px" height="32px" >
<p ><?php echo "" . $row2['username'] . "" ;?></p></a>
<span class="msg-icon" ><?php echo timeAgo($d); ?></span>
</div>
<p ><?php echo $row3['answer']; ?></p>
</div>
</td>
</tr>
</div>
<?php
}
}
echo"</div>";
how can i modify my query such that whenever one of my followers replies to a new message it becomes the first message to be displayed regardless of who i followed first?
I solved the problem, only needed to add order by date desc

Getting rows from database but printing each one of them where I want

I have a number of rows I want to retrieve from the database but I want to print each row in the exact place I want and only even rows.
Is this possible? If yes, how can I achieve it?
<?php
$sql="Select * from plays where usertype=2 and idgame=$idgame";
$result=mysqli_query($link, $sql);
while($row = mysqli_fetch_assoc($result)) {
$aword = $row['word'];
} ?>
<body>
<div id="row">
<span class="attemp">1 :</span>
<span class="word"><?php print //I want to print here only the 2nd fetched row ?></span>
</div>
<div id="row">
<span class="attemp">2 :</span>
<span class="word"><?php print //I want to print here only the 4th fetched row ?></span>
</div>
</body>
If when retrieving the data, you store it into an array, you can then output the data as you need to (remembering that arrays are 0 based)...
$aword = [];
while($row = mysqli_fetch_assoc($result)) {
$aword[] = $row['word'];
} ?>
<body>
<div id="row">
<span class="attemp">1 :</span>
<span class="word"><?php echo $aword[1]; ?></span>
</div>
<div id="row">
<span class="attemp">2 :</span>
<span class="word"><?php echo $aword[3]; ?></span>
</div>
</body>
Also I would suggest that if you only want the one column from the data, then select just that column rather than *...
$sql="Select word from plays where usertype=2 and idgame=".(int)$idgame;
and the obligatory comment about using prepared statements to stop SQL injection and some common errors.
$query = "Select * from plays where usertype=2 and idgame=$idgame";
$result = mysqli_query(isConnect(),$query);
while($row = mysqli_fetch_array($result));
{
echo "$row[0]";
}
?>

PHP while loop different images

Okay let me clarify everything.
Chars table looks like http://prntscr.com/9v8jiv .
Here is the standard html
<div id="characterone">
<h1 class="charactername">Test_Name</h1>
<p class="characterstats">DOB: 12/01/1992</p>
<p class="characterstats">Origin: Mexican</p>
<p class="characterstats">Time in Los Santos: 56</p>
<button class="choosebutton">Choose</button>
</div>
<div id="charactertwo">
<h1 class="charactername">Angelo_Damce</h1>
<p class="characterstats">DOB: 12/01/1992</p>
<p class="characterstats">Origin: American</p>
<p class="characterstats">Time in Los Santos: 26</p>
<button class="choosebutton">Choose</button>
</div>
<div id="characterthree">
<h1 class="charactername">Moemen_Walid</h1>
<p class="characterstats">DOB: 12/01/1992</p>
<p class="characterstats">Origin: American</p>
<p class="characterstats">Time in Los Santos: 26</p>
<button class="choosebutton">Choose</button>
</div>
All I want to do is, to select someone's logged in characters and then he sees the 3 in front of him and then he chooses,
I began by normaling querying them
$getchars = mysqli_query($con,"SELECT * FROM `characters` where Username = '".$user."'") or die('Error: ' . mysqli_error($con));
$check = mysqli_num_rows($getchars);
Now I need to find a way so for every char the mysql query find, to be echo'ed or written in the html up.
Eg: echo ''.$row[1]["Name"].' ';
but in each div there should be a different echo
'.$row[2]["Name"].'
^ But of course this code isn't real. just trying to explain.
Ok Here is the complete answer for your question. Just replace the Name and Time Column names with the correct Column names in the database.
$i = 1;
while($rowget = mysqli_fetch_array($getchars))
{
$id1 = 'characterone';
$id2 = 'charactertwo';
$id3 = 'characterthree';
if($i<4){
echo'<div id="'.${'id'.$i}.'">';
echo '<h1 class="charactername">'.$rowchar["Name"].'</h1>
<p class="characterstats">DOB: '.$rowchar["Birthdate"].'</p>
<p class="characterstats">Origin: '.$rowchar["Origin"].'</p>
<p class="characterstats">Time in Los Santos: '.$rowchar["Skin"].'</p>
<button class="choosebutton">Choose</button>';
echo '</div>';
}
$i++;
}
just use a counter
$i=1;
while($rowget = mysqli_fetch_array($getchars)) {
$charpost = $rowget["Character"];
echo' <img src=$i.png> ';
$i++;
}

Different meaning in the mysql code?

$result=mysql_query("select * from dosyabegeni where veri_id='" . get_custom_field('dwcode') . "'");
Not Working
It says the number and the screen, but the application does not work
veri_id='" . get_custom_field('dwcode') . "'");
veri_id='" . echo get_custom_field('dwcode') . "'");
Working
veri_id='HelloTest'");
veri_id='1234567890'");
veri_id='" . $_GET['test'] . "'");
Main Codes
<?php
include('/home/emre2010/public_html/EntegreOz/DosyaBegeni/config.php');
$result=mysql_query("select * from dosyabegeni where veri_id='" .get_custom_field('dwcode') . "'");
while($row = mysql_fetch_array($result))
{
$sira_id=$row['sira_id'];
$veri_id=$row['veri_id'];
$begeni=$row['begeni'];
?>
<div class="reviewbox">
<div class="summarywrap">
<div class="summarywrapinner">
<div class="summary">
<div class="reviewsection"><div class="rating points">
<a href="#" class="begeni" id="<?php echo $sira_id; ?>">
<span style="color:#fff;" align="center"> <?php echo $begeni; ?> </span>
</a>
<p class="ratingtext">completed!</p></div>
</div><div class="clear"></div>
<div class="clear"></div>
</div>
<div class="ratingsummary"></div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
What's the problem?
Are you meaning to run the review box from within the while loop? If your SQL is only supposed to return one row, you really shouldn't have all that stuff within the while loop. I don't see an end bracket. If you are then you're doing it pretty archaically. You should put the information from the while loop into an array instead and then run a separate loop in your content, but that's not really going to solve the problem. The only issue you could have is either not returning rows because they don't exist, or an error in your SQL.
If your SQL query isn't returning any rows then you need to do two things. One, echo out the sql query. And two, print any possible errors.
echo $sql.'<br />';
print(mysql_error());
You never know, you might have misspelled a column on the table of the db itself.
If that's not working, then print the $row out.
Outside of your while loop do:
print_r($row);

Categories