php Error: Resourse id #4 - php

for($i = 0;$i<10;$i++)
{
$query1 ="SELECT `id` FROM radcheck ORDER BY `id` DESC LIMIT 1;";
$lololo= mysql_query($query1) or die(mysql_error());
//echo $lololo;
$query = "INSERT INTO radcheck (username,attribute,op,value) VALUES ('teleuser".$lololo."','Cleartext-Password',':=','$arr1[$i]')";
mysql_query($query) or die(mysql_error());
}
I am trying to retrieve my latest id value to append with my username. And the value retrieve always be 'Resource id #4'.
Is there anyway to solve it ?
Thank you.

Mysql doesn't support TOP that is for SQL Server.
Instead of using TOP you can use mysql LIMIT so your query would be:
SELECT `id` FROM radcheck ORDER BY `id` DESC LIMIT 1;

You do not have to use single quotes around column names. if you need to escape it use backticks. And TOP is not mysql syntax. You have to use limit
SELECT `id` FROM radcheck ORDER BY `id` DESC limit 1
Do not longer use the depricated mysql_* API. Use mysqli_* or PDO

Remove the 's around field names -
"SELECT TOP 1 id FROM radcheck ORDER BY id DESC";
But as Daan told it will not work for mysql, then ORDER & LIMIT will do the trick.
"SELECT id FROM radcheck ORDER BY id DESC LIMIT 1";

i hope you never mind trying this pattern
Select id from radcheck WHERE id=1 ORDER BY 'id' DESC ;

Related

Get record by value or get last record

I am trying to figure out if it is possible to get a record if there is a value otherwise grab the last record. I do currently have a solution using an if statement like the one below:
if (!empty($version)) {
$sql = "SELECT `version` FROM `versioning` WHERE `id`='$itemID' AND `version`='$version' ORDER BY `version` DESC LIMIT 1";
}else{
$sql = "SELECT `version` FROM `versioning` WHERE `id`='$itemID' ORDER BY `version` DESC LIMIT 1";
}
My question is: Is there was a way to do this in a single sql statement and if there is, what would be the pros or cons of doing it in a single statement?
To help clarify
I am trying to figure out if it is possible to use one sql statement instead of an if statement to get either the specified version or if version is empty I would like to get the last version from the versioning table.
Yes you can do it by one sql like the following
$version = (!empty($version))?$version:'%%';
$sql = "SELECT `version` FROM `versioning` WHERE `id`='$itemID' AND `version` LIKE '$version' ORDER BY `version` DESC LIMIT 1";
by using one line, it can be useful if you like to change the conditions in the sql or add more fields or add another join statements
,,, all this you do not have to modify two sql lines
May not be using ANSI SQL statement but you can use a stored procedure though for passing the parameter and call that procedure instead in your app code like
create procedure usp_version (version varchar(10))
as
begin
if version is not null then
SELECT `version` FROM `versioning`
WHERE `id`='$itemID'
AND `version`='$version'
ORDER BY `version` DESC
LIMIT 1;
else
SELECT `version`
FROM `versioning`
WHERE `id`='$itemID'
ORDER BY `version` DESC
LIMIT 1;
end if;
end

Mysql select list after a specific id

I'm trying to find a solution to select a list of rows coming after a certain Id from an ordered list.
For example, first I select 1000 rows. Then, on a subsequent request, i want to fetch another 1000 rows coming from after the last id of the first request. I know i can do it with limit, but suppose there has been 100 rows added between the first and second request, there will be 100 rows that will be from the first request.
Both queries will be ordered by the date of the entries.
Here's an example of the query I thought of:
$query = "SELECT * FROM table WHERE id AFTER $id ORDER BY date DESC";
$query = "SELECT * FROM `table` WHERE `id` > '$id' ORDER BY `date` DESC LIMIT 1000";
Two ways to do this:
WHERE
"SELECT * FROM `table` WHERE `id` > '$id' ORDER BY `date` DESC LIMIT $length"
LIMIT
"SELECT * FROM `table` LIMIT $start, $length"
$query = "SELECT * FROM table WHERE id > $id ORDER BY date LIMIT 1000";
You're asking about logic, not code so here it is.
The first request selects the first 1000.
$query = "SELECT * FROM the_table ORDER BY `date` DESC LIMIT 0,1000";
NB date is a reserved word so needs escaping if you've called a column "date" which you shouldn't.
$rs=$db->selectMany($query); // replace this with however you select the rows. $rs is results set
Do stuff with PHP and save the maximum id. They may not be in order.
$maxid=0;
foreach ($rs as $r){
// whatever you need to do with your results
$maxid=max($maxid, $r->id);
}
Your subsequent select uses the last id
$query = "SELECT * FROM the_table WHERE id > $maxid ORDER BY date DESC LIMIT 0,1000";
BUT you need to take note that you're ordering by date and using id to find a breakpoint which sounds like it would cause data to be missed.
Perhaps you mean to use WHERE`date`> $maxdate? If so you can figure that out from the code given.

Where do I put DESC in a mysql query? (PHP)

This is my query:
$query = "SELECT * FROM table ORDER BY 'timestamp' LIMIT $startAt, $perPage";
I'm trying to sort the results by the latest (latest on top), so I tried to add DESC.
But where ever I put it in the query it gives me an error, the only way it doesn't give an error is like this:
$query = "SELECT * FROM movies ORDER BY 'timestamp' DESC LIMIT $startAt, $perPage";
But even though it doesn't give an error, it still doesn't work.
This probably seems like a silly thing to ask but it's really driving me crazy
Timestamp is the datatype in MySQL. So, always try to avoid such column name in your table. Even, if you have a column named as timestamp, you must have to use backtick. So, use below query:
$query = "SELECT * FROM table ORDER BY `timestamp` LIMIT $startAt, $perPage";
Try backtick on timestamp :
$query = "SELECT * FROM movies ORDER BY `timestamp` DESC LIMIT $startAt, $perPage";
Using backticks permits you to use alternative characters
I think however, instead of mandating whether or not you can use backticks, they should have a standard for names. It solves more 'real' problems.
Do not use quote in field name. Try this:
$query = "SELECT * FROM movies ORDER BY timestamp DESC LIMIT $startAt, $perPage";

How to do a MYSQL Select dependant on a previous MYSQL Select

My problem is that I need to SELECT fields from a MYSQL table depending on a previous MYSQL SELECT. Before I did this through an if and else if statement when it could only be one of two things but now I've added a third and so this no longer works. This is the effect I want:
$call = "SELECT * FROM blog_posts WHERE id = $_GET[id] LIMIT 1";
$latest = mysql_query($call);
$result = mysql_fetch_array($latest);
$post_cat = $result['category'];
$sql="SELECT title, post_thumb, id FROM blog_posts WHERE category = $post_cat AND id <> $_GET[id] ORDER BY id DESC LIMIT 6";
Thanks in advance.
$sql="SELECT `title`, `post_thumb`, `id` FROM `blog_posts` WHERE category = '".$post_cat."' AND `id` = '".$_GET[id]."' ORDER BY `id` DESC LIMIT 6";
Put tables/columns in backticks and surround PHP variables in concatenated quotes. At the moment, your query asks MySQL to find a value in the category column which is $post_cat. Not the PHP variable $post_cat, the explicit string $_post_cat.

there is something wrong with sql query

following sql query was working fine, i don't whats wrong i did its stopped fetching records.
$data = mysql_query("SELECT * FROM product_table where pid=$saa1 OR gpid=$saa1 OR
category_id=$saa1 ORDER BY autoid desc limit $no2,20")
or die(mysql_error());
when i remove or clause its works fine for example
$data = mysql_query("SELECT * FROM product_table ORDER BY autoid desc limit
$no2,20")
or die(mysql_error());
please have a look and let me know where i am doing mistake....
regards,
It seems,your query is OK, but when you use WHERE clause, you limit the results , so perhaps there is no recored for display, especially when you use LIMIT for starting offset and number of results.
Your query is ok but there is no record to satisfy your where part. go to your database and create some new rows with your criteria.
Try:
$data = mysql_query("SELECT * FROM product_table where (pid=$saa1 OR gpid=$saa1 OR
category_id=$saa1) ORDER BY autoid desc limit $no2,20")
or die(mysql_error());

Categories