473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

mysql_query gives Resource id #17

KeredDrahcir
426 256MB
Have a table that needs to have results written to it, that will later be updated. To check if the table has been written to yet I'm using this code:
Expand|Select|Wrap|Line Numbers
  1.     $sql="SELECT DISTINCT field FROM table WHERE field=".$value;
  2.     $new_result=mysql_query($sql,$db);
  3.     if (!$new_result)
  4.     {
  5.       do xyz      
  6.     }
When I do this it doesn't enter the if. I tried echoing $new_results and got Resource id #17. What does this mean and what am I doing wrong that prevents me from entering the if. I tried the sql statement in an sql command prompt and it gave me Empty set (0.00 sec), which I was hoping for.
Sep 15 '10 #1
11 12127
code green
1,726 Expert 1GB
From the manual
Expand|Select|Wrap|Line Numbers
  1. For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error
Does this clear things up?
Sep 15 '10 #2
KeredDrahcir
426 256MB
Not really. It's returning a Rescource on sucess but as I mentioned it was getting Empty Set (which I wanted) in the sql so surely it should be returning false?
Am I missing something?
Sep 15 '10 #3
code green
1,726 Expert 1GB
Yes, an empty result set is not an error.
To check for an empty result set use mysql_num_rows()
Sep 15 '10 #4
KeredDrahcir
426 256MB
Thanks. I did figure it out. I don't understand, that for some reason it did work to begin with and then now it suddenly stopped working.
I've got it woring now. I used mysql_get_row first and then checked it. Is mysql_num_rows a better function to use or are they both much the same?
Sep 16 '10 #5
code green
1,726 Expert 1GB
Entirely different.
mysql_get_row() Returns one row from the resultset.
It is generally used inside a loop to retrieve records.

mysql_num_rows() returns a count of the number of rows in the resultset.
It is generally used to check how many records the query found.

All this stuff is freely available in the PHP manual.
Download it and and use a link via your desktop for easy access
Sep 16 '10 #6
KeredDrahcir
426 256MB
Thanks. I know the difference between to functions. My code shows that I'm trying the check for a result. I was just wondering is using mysql_get_row() and then if (!$result) was better or worse than checking with mysql_num_rows()?
Sep 16 '10 #7
code green
1,726 Expert 1GB
Thanks. I know the difference between to functions
You may understand the functions but you are not using them the way nature intended.
By the way it is mysql_fetch_row not mysql_get_row().
mysql_get_row() and then if (!$result) was better or worse than checking with mysql_num_rows()?
mysql_fetch_row() takes the result resource as a parameter.
If there is no result resource it cannot return a row. So your suggestion is meaningless.
It is much better to use the right function for the right job in the right order.
Expand|Select|Wrap|Line Numbers
  1. if(false===($result = mysql_query($query,$resource))) error
  2. if(mysql_num_rows($result)
  3. {
  4.  while($record = mysql_fetch_row($result))
  5. }    
Or something like that.
I apologise for any syntax errors.
Sep 16 '10 #8
KeredDrahcir
426 256MB
I copied mysql_get_row() from your post.

Expand|Select|Wrap|Line Numbers
  1. $new_result_test=mysql_query($sql,$db);
  2. $new_result = mysql_fetch_row($new_result_test);
  3. if (!$new_result)
  4. {
  5.   new coffee();
  6. }
This is what I have found to work. I was trying to find out if there was a better way of doing it. Am I doing something wrong there?

What I think I'm doing is running the query to try and get a row from the table. I'm doing that to check if a row with a specified value exists yet becuase if it doesn't I need to create it.
Is the method I've used stable?
Sep 16 '10 #9
code green
1,726 Expert 1GB
I think I understand what you are trying to achieve now.
If you simply want to know if a certain record exists, but you don't want to do anything with it,
the most efficient method would be
Expand|Select|Wrap|Line Numbers
  1. $sql="SELECT 1 FROM table WHERE field=".$value; //Will return first field only
  2. $new_result_test = mysql_query($sql,$db); 
  3. if(!mysql_num_rows($new_result_test); 
  4.   new coffee(); 
  5.  
There is no need to return any rows from the resultset if you are not going to use them.
Remember mysql_query() will still return a resource if no rows are found so testing the resource only proves the query executed,
which is where I think you are getting confused.
Sep 16 '10 #10
KeredDrahcir
426 256MB
Thanks. If I do need to use the result, am I still better of testing mysql_num_rows or testing the result?
Sep 21 '10 #11
code green
1,726 Expert 1GB
Again, use the functions for the purpose intended.
There is no result to test, there is only the function return values
Expand|Select|Wrap|Line Numbers
  1. //mysql_query() returns a resource on success, or FALSE on error 
  2. $result = mysql_query($query,$resource);
  3. if($result != false)
  4. {
  5. if(mysql_num_rows($result) > 0) 
  6. //returns a count of the number of rows in the resultset.
  7. {
  8. /*mysql_fetch_row() 
  9.   Returns a numerical array that corresponds to the fetched row, 
  10.   or FALSE if there are no more rows. */ 
  11.  while($record = mysql_fetch_row($result)) 
  12. }  
  13. }   
Sep 21 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Craig Putnam | last post by:
This function: function queryDataConnection($query) { return mysql_query($query) or die("Oops"); } doesn't seem to properly return the result set, since this script: $resultSet =...
6
by: aa | last post by:
I use the following fragment of code to output datf from MySQL: ====================================================== $chan = mysql_connect ($db_host, $username, $password); mysql_select_db...
2
by: Willem Berendsen | last post by:
Hello, I setting up my mysql-server and php on IIS6 and it works almost fine. But I got the next error: Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in...
2
by: Sugapablo | last post by:
I have a small test script connecting to a MySQL database. It seems to work, unless I try to use the resource link identifier returned by mysql_connect(); This works and returns all the rows in...
9
by: Petr Vileta | last post by:
Hi, I'm new here and excuse me if this question was be here earlier. I have a simple code <html><body> <?php <?php $link = mysql_connect("localhost", "user", "password") or die("Grr: " ....
2
by: techjohnny | last post by:
Error: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/jplane/certcent/phpweb/quiz/index.php on line 20 Warning: mysql_num_rows(): supplied argument is...
4
by: atyndall | last post by:
OK, this is the relevant portion script: <?php $username = '__'; // MySQL Database Username. $password = '__'; // MySQL Database Password. $server = '__'; // MySQL Database server (most...
6
by: Kye | last post by:
Can anybody please suggest to me where I can look up what a Resource id #71 is??? No success so far looking for what the resource codes are on the net. -- Yours Sincerely Kye
11
by: Kurda Yon | last post by:
Hi, I got this warning: mysql_query(): 4 is not a valid MySQL-Link resource. The line which cause this warning is: mysql_query("insert into $tablename (id,priority) values('$id', '0.00')",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.