I am just trying to get the auto incremented value of a table that is currently the highest. I do not need to know what the next auto increment is, just the highest value of what is in the table right now. I am using the code below, but regardless of what the actual auto increment is, what table I last inserted into, what table was last updated / modified, or any other factors that I can see, the value always returns Resource id #4. This is perplexing to me for two reasons. First I don't understand why the number is always 4, second I do not understand why I am getting back a string value (with letters and a symbol) instead of just an integer. What is the deal here?
<?php $highest_id = mysql_query("SELECT MAX(c_id) FROM customers"); ?>
mysql_query doesn't return the value from the query, it returns a result resource. To get the actual value, you need to use one of the mysql_fetch_* functions, passing it the result resource you got from mysql_query.
<?php
$result = mysql_query("SELECT MAX(c_id) FROM customers");
$row = mysql_fetch_row($result);
$highest_id = $row[0];
?>
or the shorter...
<?php
$highest_id = mysql_result(mysql_query("SELECT MAX(c_id) FROM customers"), 0);
?>
This is my answer:
require_once 'db_cconnection.php';
$query = "SELECT MAX(stud_id) FROM student_tbl";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_row($result);
echo $row[0];
When you use mysqli_fetch_row it will fetch only one row, as we only want one row. $row will be an array. So we need to get its value through array index.
mysql_query returns a result handle, not the actual result. In other words, your query's result is saved as resource id #4 (and that's not guaranteed to be the same always, it's coincidence if you see it that way all the time).
To access the result you need to use something like mysql_fetch_array or one of the other functions in the same family.
Something like this:
<?php
$query = mysql_query("SELECT MAX(c_id) as max FROM customers");
$row = mysql_fetch_array($query);
$highest_id = $row['max'];
?>
the 2015 way of doing this
suppose you have database ($this->db)
$maxid = $this->db->query('SELECT MAX(c_id) FROM `customers`')->fetchColumn();
$highest_id = mysql_result(mysql_query("SELECT MAX(c_id) FROM customers"));
Related
The query SELECT * FROM TABLE WHERE id LIKE '%1% is not working properly, it's not select the id 1.
mysql_connect('localhost', 'root' , '');
mysql_select_db('database');
$sql = ("select * from search WHERE id LIKE '%3%'");
mysql_query($sql);
$my_variable = mysql_query($sql);
$display_data = mysql_fetch_row($my_variable);
while ($list = mysql_fetch_assoc($my_variable)) {
$id = $list['id'];
$title = $list['title'];
$keywords = $list['keywords'];
$img = $list['img'];
$link = $list['link'];
}
If you are looking to SELECT id 1 then use = not LIKE. The way LIKE is being used it will match every id that has a 1 in it and you are not guaranteed to get the first one in order, so instead use:
SELECT * FROM search WHERE id = 1
According to the PHP documentation of mysql_fetch_row it
Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.
Which means that the first result won't show up in the next (mysql_fetch_assoc) procedure. You could try removing the $display_data = mysql_fetch_row($my_variable); line and only use the while($list = mysql_fetch_assoc($my_variable)) { ... } procedure. See if that solves your problem.
$sql = ("select * from search WHERE id ='3'");
The id is an integer use = instead of like . Equal is more accurate.
And first echo your query in your program->
echo $sql;die;
copy that query and run it on your phpmyadmin
and then check is your column id is int type if it is then like will not give you the result. You have to use the where clause here .But if you have the column id is of type varchar then definitely give you the result .
Try to use search tab under your database->table in your phpmyadmin and put the condition there.
You will definitely get your answer there.
I have this:
$query = "SELECT time FROM table_schedule GROUP BY time ORDER BY count(*) desc LIMIT 1 ";
then I stored it in:
$result = mysql_query($query);
I will output the value by <?php echo $result ?> but I am always getting this value Resource id #20.
I am new in sql so I hope someone can help me with my problem.
You are trying to show the result in a unproper way. In PHP mysql_query() will execute a query. In order to get the result, you should use
$result = mysql_query($query);
$row=mysql_fetch_array($result); // fetches the result of the query and stores in an associative array
$time=$row['time']; // get the result from the associate array with field name as the index
You can also use ,
$row=mysql_fetch_row($result);
$time=$row[1];
which will fetch the result as an enumerated array where you want to display each fields with the column number starting from 0.
For reference
Thanks a lot for your answers, they really helped me a lot!
I'm curious of what I'm doing wrong: I can't get any values from a SQL statement.
My database table structure:
My phpcode:
include('config.php');
$id = $_GET['id'];
$query = "SELECT * FROM upload WHERE id = '$id'";
echo $query."<br/>";
$result = mysql_query($query) or die('error');
print_r($result);
echo $result['id'];
I get the following when testing:
"SELECT * FROM upload WHERE id = '1'
Resource id #2"
But there IS an id with value '2', but why doesn't it show in my html?
Only with a 'while' statement I get the desired results:
while($results = mysql_fetch_array($result))
{
echo $results['filetitle'];
}
Is this while statement necessary with a single result? I mean, there can only be one ID.
A resource is a resource. It contains a number of rows. It isn't special-cased for when there is exactly one row returned.
You don't have to use while if you know there is exactly one result, but you still need to use mysql_fetch_array or some other method to extract the first row from it.
$result is a matrix, having each row an output. So to access to id you firstly have to indicate the row.
For example, with $result[ 0 ][ 'id' ].
However, it is quite better to do it through the while($results = mysql_fetch_array($result)) expression.
SELECT statement return Resource ID #, you have to iterate your result using mysql_fetch_array, mysql_fetch_assoc functions.
try with this
$result[0]['id'];
I have this
$sql = "SELECT id FROM table";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['id'];
}
This echo's all id's found in the table.
How can I choose to echo only a selected id.
Say the second id found on the table?
EDIT
I think I have confused people and myself aswell.
Let me try to explain again.
Using the above query I can echo all results found in the table with echo $row['id'];
However I do not want echo all results, just selected ones.
You guys have suggested I use limit or a Where clause.
If I do this I will be limited to just one record. This is not what I want.
I want to echo a selection of records.
Something likes this
echo $row['id'][5], $row['id'][6], $row['id'][6]
But obviously this is incorrect syntax and will not work but hopefully you get what I am trying to do.
Thanks
If you only want the second row then you could change your query to use offset and limit e.g.
SELECT id FROM table LIMIT 1, 1
You could also use a for loop instead of the while loop and then put in a conditional.
UPDATE
Just noticed comments above - you also need to sort the PHP bug by changing mysql_fetch_array to mysql_fetch_assoc.
UPDATE 2
Ok based on your update above you are looking to get all of the rows into an array which you can then iterate over.
You can just use mysql_fetch_array and then use $array[0]. For example:
$sql = "SELECT id FROM table";
$result = mysql_query($sql) or die(mysql_error());
$ids = array();
while($row = mysql_fetch_array($result)) {
$ids[] = $row[0];
}
From what I can gather from your questions you should not be selecting all records in the table if you wish to just use the Nth value, use:
SELECT id FROM table LIMIT N, 1
That will select the Nth value that was returned. Note: The first result is 0 so if you wish to get the second value the Nth value should be 1.
mysql_data_seek() let's you jump to a specific data-set(e.g. the 2.nd)
Example:
$sql = "SELECT id FROM table";
$result = mysql_query($sql) or die(mysql_error());
//get the 2nd id(counting starts at 0)
if(mysql_data_seek($result,1))
{
$row=mysql_fetch_assoc($result);
echo $row['id'];
}
OR:
use mysqli_result::fetch_all
It returns an array instead of a resultset, so you can handle it like an array and select single items directly (requires PHP5.3)
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How do i “echo” a “Resource id #6” from a MySql response in PHP?
I am new at php and SQL and I'm trying to make the php page list the numbers of enries in the table.
I'm using this code but it returns Resource id #2:
$rt=mysql_query("SELECT COUNT(*) FROM persons");
echo mysql_error();
echo "<h1>Number:</h1>".$rt;
Because you get a mysql ressource when you do a mysql_query().
Use something like mysql_fetch_assoc() to get the next row. It returns an array with the column names as indices. In your case it's probably COUNT(*).
Here's a fix and some minor improvements of your snippet:
$rt = mysql_query("SELECT COUNT(*) FROM persons") or die(mysql_error());
$row = mysql_fetch_row($rt);
if($row)
echo "<h1>Number:</h1>" . $row[0];
If you need to get all rows of the resultset use this snippet:
while($row = mysql_fetch_assoc($rt)) {
var_dump($row);
}
Try this:
$rt=mysql_query("SELECT COUNT(*) FROM persons");
echo mysql_error();
$count = mysql_result($rt, 0, 0);
echo $count;
In PHP, resources are returned from certain functions so that they can be passed to other related functions. Examples include database connections, database query results, file-handles, etc.
According to the documentation on mysql_query(), a SELECT query returns a resource. You can take that resource and pass it to a number of different functions. To retrieve a count of the rows, you can use mysql_num_rows(), to retrieve the results of the query, you can use either mysql_fetch_array(), mysql_fetch_assoc() or mysql_fetch_object().
A normal pattern for dealing with database results will look something like this:
$result = mysql_query("SELECT * FROM persons"); // run query against database
$count = mysql_num_rows($result); // retrieve a count of the rows in the previous query
while ($row = mysql_fetch_assoc($result)) { // loop through all the rows in the resultset
// use $row['column_name'] to access columns in your resultset
}
From your example above:
$result = mysql_query("SELECT COUNT(*) AS num FROM persons"); // run query against db
$row = mysql_fetch_assoc($result); // retrieve the 1 (and only) row
$count = $row['num']; // we needed to alias the COUNT(*) column as `num`
mysql_query() doesn't return a value, it returns a resource (see here in the manual).
The returned result resource should be passed to another function for dealing with result tables (like mysql_fetch_array() or mysql_fetch_assoc()), to access the returned data.
Example based on your initial code:
$rt=mysql_query("SELECT COUNT(*) FROM persons");
while($row = mysql_fetch_assoc($rt)) {
var_dump($row);
}
mysql_query returns a resource object. You need to fetch rows from it first (mysql_fetch_row).
Straight from PHP.net.......
"For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error."
From the documentation on mysql_query:
For SELECT, SHOW, DESCRIBE, EXPLAIN
and other statements returning
resultset, mysql_query() returns a
resource on success, or FALSE on
error.