Displaying one result on a query with LIMIT 10 - php

Today i'm makeing this new question because this is not posible to display one result on a query with limit 10.
Here is the query:
$query = "SELECT * FROM articol WHERE status = 1 ORDER BY data DESC LIMIT 10";
$result = mysql_query($query) or die ("Could not execute query");
while($row = mysql_fetch_array($result))
{
$id = $row["id"];
$titlu = $row["titlu"];
$data = $row["data"];
$desc = $row["continut"];
$part = strip_tags($desc);
}
And this is the echo to display
<link>http://dirlink.ro/articol.php?art_id=<?php echo $id; ?></link>
<title><?php echo $titlu; ?></title>
<description><?php echo substr($part,0,180); ?> ...{Citeste tot} </description>
<pubDate><?php echo $data; ?></pubDate>
The same code is putted on other page, for other category of my website and it's work just fine. I don't understand why at this section is echoing only one result.

All you are doing is reassigning the variables, you are not outputting them, so you will only ever end up outputting the last result.
What you need to do is call them inside the while loop:
$query = "SELECT * FROM articol WHERE status = 1 ORDER BY data DESC LIMIT 10";
$result = mysql_query($query) or die ("Could not execute query");
while($row = mysql_fetch_assoc($result)) {
$id = $row["id"];
$titlu = $row["titlu"];
$data = $row["data"];
$desc = $row["continut"];
$part = strip_tags($desc);
print "<link>http://dirlink.ro/articol.php?art_id=$id</link>\n"
."<title>$titlu</title>\n"
."<description>".substr($part,0,180)." ...{Citeste tot} </description>\n"
."<pubDate>$data</pubDate>\n";
}
Edited a little after I read the question properly... [blush]

I think you want to use mysql_fetch_assoc($result) for named elements, but I could be mistaken.
your echos need to be in the loop if you are trying to list them all; otherwise your values are going to be overwritten and only the last row will be displayed.

I have fixed it: I checked the RSS field with rssvalidator.org and there it showed me the errors and how to fix it.
So I fixed the error, which seem to originate from some &circi;, which is a character of a Romanian language.

Related

Why I can not echo mysql query result when using WHERE statement

I have the next code which shows no results:
<?php include ("access.php");
$corpustitle = "Korpus Bilingüe Alemany-Català (de)";
$result = mysqli_query($dbiac, "SELECT corpus FROM corpus_info WHERE title = '$corpustitle'") or die(mysqli_error($dbiac));
while($row = mysqli_fetch_array($result)){
echo $row['corpus']."<br>";
} ?>
But if I take the WHERE statement out of the query I get next results:
$result = mysqli_query($dbiac, "SELECT corpus FROM corpus_info") or die(mysqli_error($dbiac));
banctraddeucat_ca
banctraddeucat_de
banctraddeuspa_de
...etc.
And also if I do the original query at the phpmyadmin I get the result I'm looking for:
Yes, after #delboy1978uk comments I added this line before the query, and that made the difference:
mysqli_set_charset($dbiac, 'utf8');
So the entire code now is:
<?php include ("access.php");
$corpustitle = "Korpus Bilingüe Alemany-Català (de)";
mysqli_set_charset($dbiac, 'utf8');
$result = mysqli_query($dbiac, "SELECT corpus FROM corpus_info WHERE title = '$corpustitle'") or die(mysqli_error($dbiac));
while($row = mysqli_fetch_array($result)){
echo $row['corpus']."<br>";
} ?>
Thanks...

Printing a MySQL query in PHP

function MattsScript()
{
$query = 'SELECT * FROM `ACCOUNTING` WHERE `ACCTSTATUSTYPE` = "start" AND `Process_status` IS NULL LIMIT 0,100';
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
while ($row = mysql_fetch_assoc($result))
{
echo $row['USERNAME'] . "<br />";
echo $row['ACCTSTATUSTYPE'];
}
}
I am trying to echo the results of a query. What I think is happening here is I am saving a query to a variable, the first 100 results (LIMIT 0,100) then using a loop to echo each row to the page.
However nothing is happening, no error and nothing written to the page.
Is there something I am missing?
if you are expecting only one result remove the while loop if not leave the while loop and remove the line $row = mysql_fetch_assoc($result); before the while loop. Also make sure you are querying your database correctly.
Example: $result = mysql_query($query) or die(mysql_error());

Figuring out why I am getting a Resource ID #5 error

This is a part of my code, and the echo is to test the value and it gives me Resource ID #5
$id = mysql_query("SELECT id FROM users WHERE firstname='$submittedfirstname' AND lastname='$submittedlastname' AND email='$submittedemail'") or die(mysql_error());
$counter = mysql_num_rows($id);
echo $id;
I am just getting into programming, and lately seeing lot of Resource ID outputs/errors while working with Databases.
Can someone correct the error in my code? And explain me why it isnt giving me the required output?
This is not an error. This is similar to when you try to print an array without specifying an index, and only the string "Array" is printed. You can access the actual data contained within that resources (which you can think of as a collection of data) using functions like mysql_fetch_array().
In fact, if there were an error here, the value of $id would not be a resource. I usually use the is_resource() function to verify that everything is alright before using variables which are supposed to contain a resource.
I guess what you intend to do is this:
$result = mysql_query("SELECT id FROM users WHERE firstname='$submittedfirstname' AND lastname='$submittedlastname' AND email='$submittedemail'") or die(mysql_error());
if(is_resource($result) and mysql_num_rows($result)>0){
$row = mysql_fetch_array($result);
echo $row["id"];
}
Did you mean to echo $counter? $id is a resource because mysql_query() returns a resource.
If you are trying to get the value of the id column from the query, you want to use e.g., mysql_fetch_array().
Here is an excerpt from http://php.net/mysql.examples-basic:
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
Adapted to the code you provided, it might look something like this:
$result =
mysql_query("SELECT id FROM users WHERE firstname='$submittedfirstname' AND lastname='$submittedlastname' AND email='$submittedemail' LIMIT 1")
or die(mysql_error());
if( $row = mysql_fetch_array($result, MYSQL_ASSOC) )
{
$id = $row['id'];
}
else
{
// No records matched query.
}
Note in my code that I also added LIMIT 1 to the query, as it seems like you are only interested in fetching a single row.
are you looking for
while ($row = mysql_fetch_array($id)) {
echo $row['id'];
}
?
$kode_gel = substr($_GET['gel'],0,3);
$no_gel = substr($_GET['gel'],3,5);
$cek = mysql_query("SELECT id_arisan
FROM arisan WHERE kode_gel = '".$kode_gel."'
AND no_gel = '".$no_gel."'");
$result = mysql_fetch_array($cek);
$id = $result['id_arisan'];
header("location: ../angsuran1_admin.php?id=".$id);

mysql query SELECT doesn't work with returning single values

I looked for an answer prior to writing this but found nothing, but feel free to post a link if I missed it.
I am trying to get the values from a single row of my mysql table. The query I'm using below returns nothing; the echo of mysql_num_rows is always zero. I know the title variable used in WHERE is valid and the database is connected, etc.. Thanks in advanced.
$title = $_REQUEST["title"];
$query = mysql_query("SELECT * FROM links WHERE title = '$title'");
if(!$query) {
die ("Error: " . mysql_error());
}
echo mysql_num_rows($query);
$row = mysql_fetch_row($query);//also tried mysql_fetch_array
$link = $row['link'];
$type = $row['type'];
$user = $row['user'];
$date = $row['date'];
$rating = $row['rating'];
$info = $row['info'];
Try with:
$title = mysql_real_escape_string($_REQUEST["title"]);
$query = mysql_query("SELECT * FROM links WHERE title LIKE '$title'");
echo $query // run it in mysql prompt to check if there are any results first.
if(!$query) {
die ("Error: " . mysql_error());
}
echo mysql_num_rows($query);
$row = mysql_fetch_row($query);//also tried mysql_fetch_array
$link = $row['link'];
$type = $row['type'];
$user = $row['user'];
$date = $row['date'];
$rating = $row['rating'];
$info = $row['info'];
$title = $_REQUEST["title"];
$query = mysql_query("SELECT * FROM links WHERE title = '$title'");
if(!$query) {
die ("Error: " . mysql_error());
}
echo mysql_num_rows($query);
while($row = mysql_fetch_row($query))
{
$link = $row['link'];
$type = $row['type'];
$user = $row['user'];
$date = $row['date'];
$rating = $row['rating'];
$info = $row['info'];
}
If mysql_num_rows returns zero it means that your query is probably not selecting anything from the database. You have to check if your sql query is actualy selecting anything. Check the value of the $title variable and try excecuting the query directly to your database.
You are also vulnerable to sql injections. Always filter user input. You can use mysql_real_escape_string and htmlspecialchars for that.
Try getting values without a where just like this one:
$query = mysql_query("SELECT * FROM links") or die(mysql_error());
If there is wrong it should prompt you the error.
Many people are already saying it but try to cleanse the request parameters ^_^
One common thing to do is print out your sql query to make sure its what you expect.
$sqlquery = "SELECT * FROM links WHERE title='".$title."'";
$query = mysql_query($sqlquery);
echo $sqlquery;
also, as has already been mentioned... you're vulnerable to SQL Injection attacks.
you should use
while($row = mysql_fetch_row($query){...} and also use mysql_real_escape_string to avoid sql injection. And also dont use echo command infront of mysql_num_rows. And even i think its not necessary to get number of rows to execute the remaining part of the script.
P.s i'm on mobile so i'm not able to post script.

SQL query is only retrieving first record

I have a query which is designed to retireve the "name" field for all records in my "tiles" table but when I use print_r on the result all I get is the first record in the database. Below is the code that I have used.
$query = mysql_query("SELECT name FROM tiles");
$tiles = mysql_fetch_array($query);
I really cant see what I have done wrong, I have also tried multiple searches within google but I cant find anything useful on the matter at hand.
<?php
// Make a MySQL Connection
$query = "SELECT * FROM example";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['name']. " - ". $row['age'];
echo "<br />";
}
?>
'mysql_fetch_array'
Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.
This means that it returns array (contains values of each field) of A ROW (a record).
If you want other row, you call it again.
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
// Do something with $row
}
Hope this helps. :D
Use "mysql_fetch_assoc" instead of "mysql_fetch_array".
$query = mysql_query('SELECT * FROM example');
while($row = mysql_fetch_assoc($query)) :
echo $row['whatever'] . "<br />";
endwhile;
I believe you need to do a loop to invoke fetch array until it has retrieved all the rows.
while ($row = mysql_fetch_array($query) ) {
print_r( $row );
}

Categories