how to print mysql result RESOURCE_ID(7) without mysql_fetch_array - php

Is there any way to print the resource_id without mysql_fetch_array.I dont want to loop through result.I want print only the first row at top.I know mysql has been depreciated.This is for old project.

You can make use of arrays in your case
$all_rows = array();
.
. // your query
.
while($dbrow = mysql_fetch_array($query))
{
$all_rows[] = $dbrow;
}
$first_row_array = $all_rows[0]; // first row will be stored here
/*
uncomment the below line if you do not want to use the
first row again while looping through the remaining
rows
*/
/* unset($all_rows[0]); */
foreach($first_row_array as $first_row)
{
// do something with first row data
}
foreach($all_rows as $dbrow)
{
// loop through all the rows returned including the first row
}

A resource in itself is a pretty meaningless type. It only means something to specific functions, like mysql_*. When querying the database, there are certain resources allocated on the MySQL server which hold your requested result; PHP doesn't really have access to those results yet. To give you a handle on those resources on the MySQL server, you get a resource type variable. It's basically just your ticket, saying "if you ever want to access that data waiting for you on the MySQL server, use this number."
So, if you want to output the data from the MySQL server, you will have to fetch it from there, e.g. with mysql_fetch_assoc. That then returns the data to you which you can print.
If you just want the first result, just call that function once.

Related

How to echo separately data from mySQL avoiding any loops?

In mySQL I have a table with 8 rows, id and status.
$make=mysql_query("SELECT id, status FROM data order by id");
My question is how can I avoid using the foreach or any loop to echo the data, but instead to ave something like
<?php echo $row['status with the id 5']; ?>
and in another place of the page to echo the status with id 8 ?
You can utilize the following pattern:
$contents = [];
foreach($results as $result) {
$contents[$result->id] = $result;
}
$results contains the MySQL result set. $contents will be the associative array. It would be more comfortable if you swapped that to a function or class which works for all the tables you want to access applying this pattern. Depending on which database class you use, it might be necessary to cast the key to an integer, otherwise there will be problems accessing the index if it is passed as a string.
Note that you furthermore should migrate your code to MySQLi or PDO first.
If your table is likely to become very big, you should not implement this. Instead, it would be better if you checked in the first place which entries will be needed and load those explicitly with an IN() query.

Showing content from database only once with php and mysql

I'm trying to show images with a specific subject on the screen. I have 5 images with the same subject and it only shows one image. If I change the subject of the image in the database, the next image with the subject I try to call appears.
function selectSubject($subject){
$showSubject = mysql_query("SELECT name FROM images WHERE subject = '$subject'");
while($showSubject = mysql_fetch_array($showSubject)){
$source_subject_trees = $showSubject['name'];
echo "<img class=\"subject_images\" src='img/$source_subject_bomen'></>";
}
};
mysql_query() returns a result handle. You then stomp all over that handle by re-assigning to it within your while() loop:
$showSubject = mysql_query(...);
while($showSubject = msyql_fetch_array($showSubject)) {
^^^^^^^^^^^^---here
mysql_fetch_array() returns an array of one row's data. Since you're assigning to the SAME variable as you stored the actual query result handle, you destroy the result handle... and end up being unable to fetch any more data, because the handle's gone.
In mysql_query you can use GROUP BY subject to get only distinct result
In mysql_query you can use DISTINCT(name) to get distinct result on selection time
Any of aboue mention option to get as per your valid result.

MySQL- Unable to jump to row 0 on MySQL result index

I have got an old site that has recently been displaying an error which is weird as its been untouched for some time. I get the following:
Unable to jump to row 0 on MySQL result index 8
What is the cause of this and how should I fix it?
It is a PHP/MySQL site.
If I remember correctly, this error typically stems from a code segment like the following:
// You probably have some code similar to this
$var = mysql_result( $result, 0, 'column_name');
Where either the query fails or the column doesn't exist. Check that $result is a valid MySQL resource to make sure that the SQL is valid, then make sure you're actually getting results from the database before trying to call mysql_result.
Or, better yet, using mysql_fetch_array instead of manually fetching every column value (if you have multiple columns returned from the query).
Try analysing the result before fetching it.
If result is empty, skip fetching.
$result = mysql_query("SELECT * FROM table1");
if (!$result || !mysql_num_rows($result)) {
die('Empty set.');
}
while ($row = mysql_fetch_array($result)) {
// Your code here
}

How to determine whether a particular resource has data

I am trying to figure out the proper way to get file location data (for display/editing) from MySQL with PHP. So far I've got these three parts. $resfile is a resource getting the actual array. Would I then test with an if statement, or would I have to use a while loop to iterate over the array (which, as far as I know, should only have ONE value)
First part:
$resfile = mysql_query('SELECT file_loc WHERE org_id = '.$org);
Do I use this?
if (!$resfile) {
}
Or this?
while ($filerow = mysql_fetch_array($resfile)) {
}
Or both?
The mySQL library has a function for counting the rows of a result set:
if (mysql_num_rows($resfile) > 0) .......
You need to use both. If the query returns false, then there was an error executing your query. If there is no data returned in the query, (it will still return true) then you need to use fetch_array to get the data.

How to reuse sql query result in PHP?

I'd like to do different operations on the data returned from a single sql query, something like this:
$sql = "SELECT * FROM mandant";
$res = pg_query($_db,$sql);
while ($mandant_list = pg_fetch_object($res))
{
# do stuff
}
while ($mandant = pg_fetch_object($res))
{
# do other stuff
}
But this doesn't work. The first loop gets the data, the second one not.
Is it possible to reuse the result returned from the query without running it again?
If not, why? What happens to the $res variable?
Yes, you can "rewind" the result resource by using pg_result_seek().
It is possible.
The reason the second loop breaks is because every time you are fetching a row from the data set it is being decremented until there are no rows left in the object.
You'll need to make 2 copies of $res and then run the second while() on the second $res.

Categories