SQL query not returning array - php

Working on getting a page to build off of an array that is returned from a DB to post a story, not sure what it is not working. The page URL looks like this: https://ohcrap.ninja/games/ps4/article.php?id=1
Here is the code that should be generating the content:
<?php
$id = $_GET['id'];
$query = mysqli_query($con,'SELECT * FROM `PS4` WHERE `id` =' .$id) or die(mysqli_error($con));
while ($row = mysqli_fetch_array($query));
// Echo page content
echo "<div class='col s12 m12 l12'>";
echo "<div class='card small grey darken-3'>";
echo "<div class='card-stacked'>";
echo "<div class='card-content'>";
echo "$id";
echo "<span class='card-title'>" . $row['title'] . "</span>";
echo "<hr color='black'>";
echo "<P>By:<i> " . $row['author'] . "</i></P>";
echo "<P>Published: " . $row['published'] . "</P>";
echo "<br>";
echo "<P class='truncate'>" . $row['story'] . "</P>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
?>

Your while loop is not doing anything useful, because you're immediately ending it with that ;.
while ($row = mysqli_fetch_array($query)) {
// all those echoes
}

Related

How to get a post in php where use sql like that :

How do i get the row id?
I'm doing a site of music, i need do the admin page,this part is when the admin gonna edit the albums. I need the id to specify what music gonna change the informations.
<?php
$db = mysqli_connect("localhost", "root", "", "datamusic");
$result = mysqli_query($db, "SELECT * FROM musics");
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img src='images/" . $row['Image'] . "' >";
echo "<p>" . $row['Band/Singer'] . "</p>";
echo "<form method='POST' action='editInformations.php'><p name=" . $row['id'] . ">ID = " . $row['id'] . "</p></form>";
echo "<a href='editInformations.php'><button type='submit'>Editar</button></a>";
echo "</div>";
}
?>
I try something like that, but doesn't works
//editInformations.php
$id =$_POST[$row['id']];
echo $id;
Get edit page GET funciton. You can put id for edit.
editInformations.php?id=".$row['id']."
Change code with this.
<?php
$db = mysqli_connect("localhost", "root", "", "datamusic");
$result = mysqli_query($db, "SELECT * FROM musics");
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img src='images/" . $row['Image'] . "' >";
echo "<p>" . $row['Band/Singer'] . "</p>";
echo "<form method='POST' action='editInformations.php'><p name=" . $row['id'] . ">ID = " . $row['id'] . "</p></form>";
echo "<a href='editInformations.php?id=".$row['id']."'><button type='submit'>Editar</button></a>";
echo "</div>";
}
?>
//editInformations.php
$id =$_GET['id'];
echo $id;

How to display two distinct MySQL tables on same PHP page

I have two sql tables I'm reading from and I would like to display each table as its own, separate table. First the internal, and then the external. And I'd like to have a simple title prefacing each one. For instance:
INTERNAL
Table here
EXTERNAL
Table here
What's happening though is the INTERNAL and EXTERNAL titles keep appearing
on the same line, or both come before the tables. Like this:
INTERNAL
EXTERNAL
Internal Table here
External Table here
I've tried including the titles as part of the php tags. Those are the currently commented out lines. And I've also tried adding the html outside of the php tags like this:
<br><br>
<b>INTERNAL</b>
<br><br>
<?php Internal Table here ?>
And then:
<br><br>
<b>EXTERNAL</b>
<br><br>
<?php External Table here ?>
But that still results in both titles appearing first, before the tables. Does the html get processed prior to the php? I must be interpreting this too linearly, like a script because it's certainly not being processed in the order in which it is coded on my page.
This is my code as is, which has the titles commented out. The tables appear sandwiched together which tells me they are not being interpreted as two distinct elements on the page. Do I need to put each one in it's own tag?
<?php
$internal = getInternalNetworkTable();
if ($internal->num_rows > 0){
//echo " <b>INTERNAL</b>";
echo "<table cellspacing=\"0\">";
echo "<tr>";
echo " <th><b>Device</b></th>";
echo " <th><b>Label</b></th>";
echo " <th><b>Address</b></th>";
echo " <th><b>Type</b></th>";
echo " <th><b>DisplayNumber</b></th>";
echo " <th><b>Wiki</b></th>";
echo "</tr>";
while ($row = $internal->fetch_assoc()){
echo "<tr>";
echo ("<td>" . $row["Device"] ."</td>");
echo ("<td>" . $row["Label"]."</td>");
echo ("<td>" . $row["Address"]."</td>");
echo ("<td>" . $row["Type"]."</td>");
echo ("<td>" . $row["DisplayNumber"]."</td>");
echo ("<td>" . $row["Wiki"]."</td>");
echo "</tr>";
}
} else {
echo "No results founds";
}
?>
<?php
$external = getExternalNetworkTable();
if ($external->num_rows > 0){
//echo " <b>EXTERNAL</b>";
echo "<table cellspacing=\"0\">";
echo "<tr>";
echo " <th><b>Device</b></th>";
echo " <th><b>Label</b></th>";
echo " <th><b>Address</b></th>";
echo " <th><b>Type</b></th>";
echo " <th><b>DisplayNumber</b></th>";
echo " <th><b>Wiki</b></th>";
echo "</tr>";
while ($row = $external->fetch_assoc()){
echo "<tr>";
echo ("<td>" . $row["Device"] ."</td>");
echo ("<td>" . $row["Label"]."</td>");
echo ("<td>" . $row["Address"]."</td>");
echo ("<td>" . $row["Type"]."</td>");
echo ("<td>" . $row["DisplayNumber"]."</td>");
echo ("<td>" . $row["Wiki"]."</td>");
echo "</tr>";
}
} else {
echo "No results founds";
}
$conn->close();
?>
EDIT:
Needed to add:
echo "</table>";
New code:
<?php
$internal = getNovatoInternalNetworkTable();
if ($internal->num_rows > 0){
echo " <b>INTERNAL</b>";
echo "<table cellspacing=\"0\">";
echo "<tr>";
echo " <th><b>Device</b></th>";
echo " <th><b>Label</b></th>";
echo " <th><b>Address</b></th>";
echo " <th><b>Type</b></th>";
echo " <th><b>DisplayNumber</b></th>";
echo " <th><b>Wiki</b></th>";
echo "</tr>";
while ($row = $internal->fetch_assoc()){
echo "<tr>";
echo ("<td>" . $row["Device"] ."</td>");
echo ("<td>" . $row["Label"]."</td>");
echo ("<td>" . $row["Address"]."</td>");
echo ("<td>" . $row["Type"]."</td>");
echo ("<td>" . $row["DisplayNumber"]."</td>");
echo ("<td>" . $row["Wiki"]."</td>");
echo "</tr>";
}
echo "</table>";
} else {
echo "No results founds";
}
?>
<?php
$external = getNovatoExternalNetworkTable();
if ($external->num_rows > 0){
echo " <b>EXTERNAL</b>";
echo "<table cellspacing=\"0\">";
echo "<tr>";
echo " <th><b>Device</b></th>";
echo " <th><b>Label</b></th>";
echo " <th><b>Address</b></th>";
echo " <th><b>Type</b></th>";
echo " <th><b>DisplayNumber</b></th>";
echo " <th><b>Wiki</b></th>";
echo "</tr>";
while ($row = $external->fetch_assoc()){
echo "<tr>";
echo ("<td>" . $row["Device"] ."</td>");
echo ("<td>" . $row["Label"]."</td>");
echo ("<td>" . $row["Address"]."</td>");
echo ("<td>" . $row["Type"]."</td>");
echo ("<td>" . $row["DisplayNumber"]."</td>");
echo ("<td>" . $row["Wiki"]."</td>");
echo "</tr>";
}
echo "</table>";
} else {
echo "No results founds";
}
$conn->close();
?>
You're missing </table> in both display of tables.
Therefore your table will be merge in each other.
echo '</table>';
Before both of your } else { should do the trick

Selecting data from sql to create a thumbnail

I'm creating a thumbnail with a title, image and caption on it. I'm trying to select data from my table to show it into my homepage. Can someone help me to create a normal thumbnail in my php that contains the detail from my sql. I tried to search and can't find how to create a thumbnail using php and not html.
$sql = "SELECT * FROM news";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>first_name</th>";
echo "<th>last_name</th>";
echo "<th>email</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "<td>" . $row['caption'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
In short, is there a way to create a php file from this?
<div class="col-md-4">
<div class="thumbnail">
<img alt="Memory" img src="../../images/a2.jpg">
<div class="caption">
<h3><b>
Title
</b></h3>
<p>
Caption Caption Caption Caption Caption
</p>
<p align="right">
<a class="btn btn-primary" href="news2.html">Read More</a>
</p>
</div>
</div>
</div>
Do you want to replace your table structure with that template structure? You'll need to adjust some of the data to fill the hyperlink (I don't know how you want to build that).
while($row=mysqli_fetch_array($result)){
echo "<div class=\"col-md-4\">";
echo "<div class=\"thumbnail\">";
echo "<img alt=\"Memory\" src=\"../../images/{$row["image"]}\">";
echo "<div class=\"caption\">";
echo "<h3>{$row["title"]}</h3>";
echo "<p>{$row["caption"]}</p>";
echo "<p align=\"right\">";
echo "<a class=\"btn btn-primary\" href=\"news2.html\">Read More</a>";
echo "</p>";
echo "</div>";
echo "</div>";
echo "</div>";
}

Put contents of PHP while loop code within a DIV

So I want to apply this DIV class to these PHP items.
This is what I have, I know it does not work, but what would be correct for what I am trying to achieve.
I cant add the class to each individually, as I want the entire contents to be within the one div.
Hope that makes sense, thanks!
<?php
while($campaigns= mysqli_fetch_assoc($result)){
<div class="feeditem">;
echo "<div class='field'>".$test['part0']."</div>";
echo "<div class='field'>".$test['part1']."</div>";
echo "<div class='field'>".$test['part2']."</div>";
echo "<div class='field'>".$test['part3']."</div>";
echo "<div class='field'>".$test['part4']."</div>";
echo "<div class='field'>".$test['part5']."</div>";
echo "<div class='field'>".$test['part6']."</div>";
</div>;
}
?>
You should use an IDE that gives you errors for syntax problems... You do not have all the html code wrapped in echo ""; statements.
while ($campaigns = mysqli_fetch_assoc($result)) {
echo '<div class="feeditem">';
echo "<div class='field'>" . $test['part0'] . "</div>";
echo "<div class='field'>" . $test['part1'] . "</div>";
echo "<div class='field'>" . $test['part2'] . "</div>";
echo "<div class='field'>" . $test['part3'] . "</div>";
echo "<div class='field'>" . $test['part4'] . "</div>";
echo "<div class='field'>" . $test['part5'] . "</div>";
echo "<div class='field'>" . $test['part6'] . "</div>";
echo '</div>';
}
I believe this is what you're trying to accomplish:
<?php
echo '<div class="feeditem">';
while($campaigns = mysqli_fetch_assoc($result)){
echo"<div class='field'>".$test['part0']."</div>";
echo"<div class='field'>".$test['part1']."</div>";
echo"<div class='field'>".$test['part2']."</div>";
echo"<div class='field'>".$test['part3']."</div>";
echo"<div class='field'>".$test['part4']."</div>";
echo"<div class='field'>".$test['part5']."</div>";
echo"<div class='field'>".$test['part6']."</div>";
}
echo '</div>';

Add link to image when echoing in PHP

In my post form, I have a "show"-page, where I echo out everything in the database. I'm trying to add a link to the image-line. But when I refresh the page, it's just white.
<?php
$sql_connection = mysql_connect("localhost", "user", "root");
mysql_select_db("database_name", $sql_connection);
$result = mysql_query("SELECT * FROM content ORDER BY id DESC");
while ($row = mysql_fetch_array($result)) {
echo "<div>";
echo "<img src=\"http://placehold.it/80x80\"> ";
echo "<br />";
echo "<br />";
echo "<h3>" . $row["who"] . "</h3><br />";
echo "<h4>" . $row["what"] . "</h4><br />";
echo "<h4>" . $row["where"] . "</h4><br />";
echo "<h3>" . $row["contact"] . "</h3><br />";
echo "<img src=\"../img/share_facebook.png\"> ";
echo "</div>";
}
mysql_close($sql_connection);
?>
Someone who can explain why this happens?
echo "<img src=\"http://placehold.it/80x80\"> ";
you have to escape() ALL your " or change to:
echo '<img src="http://placehold.it/80x80">';

Categories