MySQL select on PHP can't get it to work - php

I am having a problem retrieveing information from a MySQL Data Base. Could you help me out? This is the code I wrote:
<?php
$result = mysql_query("SELECT * FROM notas LIMIT 1 ORDER BY id DESC");
while($nt = mysql_fetch_array($result)) {
?>
<div class="nuevos_1">
<div class="over_descripcion">
<div class="over_title">
<h3><?php echo $nt[titulo] ?></h3>
</div>
<div class="over_autor">
<span><b>Por: </b><?php print ucwords(str_replace("-", " ", $nt[autor])); ?> <?php echo $nt[fecha] ?></span>
</div>
<div class="over_texto">
<span><b><?php echo strtoupper(str_replace("-", " ", $nt[categoria])) ?></b> <?php echo substr(strip_tags($nt[texto]), 0, 240)."..." ?></span>
</div>
<div class="over_ver_mas">
<input type="button" value="Leer más" onclick="location.href='/nota/<?php echo $nt[fecha]."/".$nt[titulolower] ?>'" />
</div>
</div>
<a href="/nota/<?php echo $nt[fecha]."/".$nt[titulolower] ?>">
<img src="http://<?php echo $nt[imagen] ?>" border="0" />
</a>
</div>
<?php }; ?>
Nothing inside the SELECT notas database is showing. It is simply gone...
Thanks!

Limit 1 has to go after the ORDER BY

change your query to
SELECT * FROM notas ORDER BY id DESC LIMIT 1

what does the error message say
$result = mysql_query("SELECT * FROM notas LIMIT 1 ORDER BY id DESC");
if (mysql_error()) {
die(mysql_error());
}

The other answers above have it correct - you've got a syntax error in your query. If you'd done the basic step of having:
$result = mysql_query(..) or die(mysql_error());
you'd have seen the error message. Never EVER assume that a query succeeds. Always check for error conditions.
As well, you've got a minor syntax bug in your output:
... <a href="/nota/<?php echo $nt[fecha]."/".$nt[titulolower] ?>"> ...
^^^^^
Your array keys are not quoted, which means they're interpreted as define()'d constants. PHP is polite and will treat those constants as strings if there actually is not constant with that name, but it will issue a warning for every occurence. It should be:
<?php echo $nt['fecha'] ... ?>

I don't too well about PHP, but your select statement limits to just one result. You might want to change this line:
$result = mysql_query("SELECT * FROM notas LIMIT 1 ORDER BY id DESC");
You will not get more than one result as it is.

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 Show Latest Uploded Post first Using SQL and PHP?

I have a website where I show the post update on the database and we use PHP and SQL for that but currently oldest posts are showing first instead I want to show the latest uploaded Post first.
Here is My PHP code with SQL Query
$projectcat_query=mysql_query("select * from projectcat where id=3 ");
while($projectcat_data=mysql_fetch_assoc($projectcat_query))
{ $catid=$projectcat_data['id'];
$limit=3;
$project_query=mysql_query("select * from projects where catid=$catid and status=1 limit $limit ");
while($project_data=mysql_fetch_assoc($project_query)) { ?>
<div class="item <?php echo $projectcat_data['name']; ?>">
<div class="picframe">
<a class="" href="project/<?php echo str_replace(' ','-',$project_data['title']); ?>">
<span class="overlay">
<span class="pf_text">
<span class="project-name"> <?php echo $project_data['title']; ?></span>
</span>
</span>
</a>
<img src="images/services/<?php echo $project_data['image']; ?> ">
</div>
</div>
<?php } ?>
and here my DB table looks like
Change your 2nd query like this
$project_query=mysql_query("select * from projects where catid=$catid and status=1 order by id desc limit $limit ");
Just add ORDER BY statement to your query like this:
$projectcat_query=mysql_query("select * from projectcat where id=3 ORDER BY id DESC");

DayofYear + Curdate not working

I cant get the dayofyear and curdate to work.
If I remove it, the code works but I need the condition.
The error message is "Trying to get property of non-object "
<?php
$rowhtipoo=mysql_query("SELECT * FROM blog_articles WHERE bog_id=1 AND fk_publish_dato_id = DAYOFYEAR(CURDATE());") or die(mysql_error());
$rwhtip=mysql_fetch_object($rowhtipoo);
?>
<div class="tipet">
<p><span class="f vvv"><?php nl2br($rwhtip->english_navn);?></span></p>
<p><?php echo nl2br($rwhtip->english_tekst);?></p>
</div>
It sounds to me like a result isn't found and mysql_fetch_object is returning a false value. Maybe you'd have better luck doing something like the example below.
http://php.net/manual/en/function.mysql-fetch-object.php.
<?php while ( $rwhtip = mysql_fetch_array($rowhtipoo) ) { ?>
<div class="tipet">
<p>
<span class="f vvv">
<?php echo nl2br($rwhtip["english_navn"]);?></span>
</p>
<p>
<?php echo nl2br($rwhtip["english_tekst"]);?>
</p>
</div>
<?php } ?>
Why don't you let PHP take care of the date?
$rowhtipoo=mysql_query("SELECT * FROM blog_articles WHERE bog_id=1 AND fk_publish_dato_id = '" . date('Y-m-d') . "';") or die(mysql_error());
Or, it can be a typo - fk_publish_datO_id ?

Displaying content but not the latest one

I have these codes to display content on my website.
Index.php
$rest = new rest;
$list = $rest->fetch_all();
<?php foreach ($rest->fetch_all() as $rest) { ?>
<br>
<a href="episode.php?id=<?php echo $rest['cast_id']; ?>">
#<?php echo $rest['cast_id']; ?>: <?php echo $rest['cast_title']; ?>
<br>
<font size="2" color="red">
<?php echo $rest['cast_about']; ?></font></a><br>
<br><div class="divider"> </div><br>
<?php } ?>
And include.php
class rest {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM podcast ORDER BY cast_id DESC");
$query->execute();
return $query->fetchAll();
} }
Please can someone tell me how I can get this to show results but not the latest result?
All fields are numbered using an ID tag in mysql.
On my site I have the latest entry listed above this list so I do not require the latest one appearing here.
Please help.
Thank you.
The easiest way to do this is to discard the row in the application. You can do it in SQL by using a calculated limit. Calculate it as (SELECT COUNT(*) FROM podcast) - 1.
Or:
SELECT *
FROM podcast
EXCEPT
SELECT TOP 1 *
FROM podcast
ORDER BY cast_id ASC
Excluding the last row.
You can either use array_shift or array_pop depending on your query sorting as shown bellow:
Assuming the latest result is the first raw on your query result.
$rest = new rest;
$list = $rest->fetch_all();
$latest = array_shift($list);
<?php foreach ($list as $rest) { ?>
<br>
<a href="episode.php?id=<?php echo $rest['cast_id']; ?>">
#<?php echo $rest['cast_id']; ?>: <?php echo $rest['cast_title']; ?>
<br>
<font size="2" color="red">
<?php echo $rest['cast_about']; ?></font></a><br>
<br><div class="divider"> </div><br>
<?php } ?>
If it's the last raw that you need to hide, then use array_pop

update value from '0' to '1' on image click?

I'm new to php and mysql so sorry if i'm doing it wrong. i have a page on my site that lists the reviews that members give to other other users.
Basically i have approved and deleted in my database which means that after a user sends the review it has to be reviewed by the user before it gets displayed.
once the user clicks the approved image which is a tick it goes to approved_review.php and in their i have my sql code to update the value from 0 to 1 in my database.
It should work exactly the same for the delete but obviously instead of updating the approved column it will update deleted.
the code i have tried is not working i have been working on this for quite some time and can;t figure it out.
Can someone please tell me where i'm going wrong?
Heres the code:
<?php
$reviews_set = get_pending_reviews();
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<p> </p>
<div class="pending-review-content">
<?php
$date = $reviews['date_added'];
?>
<div class="prof-content-pend-reviews" id="reviews">
<div class="message_pic"><?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\">
<img width=\"50px\" height=\"50px\" src=\"data/photos/{$reviews['from_user_id']}/_default.jpg\" /></a>";?>
</div>
<div class="reviews-date"><? echo "$date"; ?></div>
<div class="reviews-from">
<?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\">{$reviews['display_name']}"; ?>
</a> Wrote:
</div>
<div class="reviews-content">
<?php echo "{$reviews['content']}"; ?>
</div>
</div>
<div class="reviews-approve">
<img src="assets/img/icons/tick.png" width="30" height="25" /></div>
<div class="reviews-delete">
<img src="assets/img/icons/cross.png" width="30" height="25" />
</div>
<? } ?>
approved_review.php function:
<?
$sql = "UPDATE `playtime`.`ptb_reviews` SET `approved` = '1' WHERE `ptb_reviews`.`id` =".$_SESSION['user_id']."";
echo "<div class=\"infobox1\">review approved.</div>";
?>
Your approach seems logical. After you loop through your reviews, you click on the tick or delete pngs to update or delete.
So, in approved_review.php
<?php
//you are missing the connection to your mysql database...
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$sql = "UPDATE `playtime`.`ptb_reviews` SET `approved` = '1' WHERE `ptb_reviews`.`id` =".$_SESSION['user_id']."";
//execute the mysql query
$r = mysql_query($sql);
if (!mysql_error())
{
echo "<div class=\"infobox1\">Review Approved.</div>";
}
?>
a little edit rrrfusco's post
// or die for details if mysql_query won't work correct
$r = mysql_query($sql) or die (mysql_error());

Categories