473,396 Members | 1,826 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,396 software developers and data experts.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result source

Expand|Select|Wrap|Line Numbers
  1. I'm having a problem. I'm sure I'm going to kick myself over the answer...
  2.  
  3. I have a table that stores vendors and their languages. This table starts out blank. I am querying the table to see if a vendor has been added to the table yet. The problem is, if they haven't been added, I can't seem to get the script to realize that. here is what I am trying to do.
  4.  
  5. $testvend = SELECT language FROM vendor_details WHERE id = $vendorid
  6.  
  7. if(!$testvend){
  8. //script to add vendor to list, with supplied language
  9. }else{
  10. while($row = mysql_fetch_array($testvend)){
  11. //script to get vendor language and display vendor langauge
  12. }
  13. }
The result I get is:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result source in (script name) on line (line with mysql_fetch_array)

*NOTE: I did try searching bytes.com and the web. The search on bytes.com returns a blank page. The web showed my method above, as well as the option to add "or die" after the query. adding or die didn't do anything either. I've also tried manually adding information to the table, so that the script is not working off of a blank table, but I still get the same result if the vendor does not exist. Also, I apologize that the title isn't very clear. I realized after I posted it I should have put a problem description in the title...
Mar 5 '09 #1
11 4708
Markus
6,050 Expert 4TB
You need to suply the mysql_* functions with a result resource. Simply passing it the query string will not do.

Expand|Select|Wrap|Line Numbers
  1. // SQL.
  2. $sql = "SELECT * FROM `tbl1`"
  3.  
  4. // Create result resource
  5. $query = mysql_query( $sql );
  6.  
  7. // foreach traverse here.
  8.  
Also, please wrap code with [code] [/code] tags and give your threads a meaningful title.

Moderator.
Mar 5 '09 #2
TheServant
1,168 Expert 1GB
I know this has been already said, but took me a second to work out the problem. Your code should be:
Expand|Select|Wrap|Line Numbers
  1. $testvend = mysql_query(" SELECT language FROM vendor_details WHERE id = '$vendorid' " );
Or:
Expand|Select|Wrap|Line Numbers
  1. $testquery = SELECT language FROM vendor_details WHERE id = '$vendorid';
  2. $testvend = mysql_query( $testquery );
Also, I think that php variables need to be surrounded by single quotes ( ' ), as well as the whole statement being a string - surrounded by double quotes ( " ). Also I see no semicolons " ; "? Was that a mistype or are you decalring variables without that? If so, I don't know why it's not throwing an error up for that but it should.
Mar 5 '09 #3
Markus
6,050 Expert 4TB
@TheServant
Deja vu :)

@TheServant
If you surround a variable that your database expects to be an int type, you will get an error. So only variables that are strings need quotes.

@TheServant
I think he just threw it together quickly to show us.
Mar 5 '09 #4
TheServant
1,168 Expert 1GB
@Markus
Interesting. I will have to test because I '$quote' all my variables and have not had any trouble. Thanks for the info.
I think he just threw it together quickly to show us.
That's what I thought, but just better make sure ;)
Mar 5 '09 #5
Yeah, I did just throw it together quickly, thats why they weren't in there.

My actual mistake was that I was leaving out the mysql_query part, so I was assigning the words in the query to the variable, instead of the actual results.

I'm having another issue though.

Here is my code

Expand|Select|Wrap|Line Numbers
  1. $testvend = mysql_query("SELECT language FROM vendor_details WHERE id = '" . $vendor . " ' ");
  2. if(!$testvend){
  3. //script if vendor does not exist
  4. }else{
  5. while($row = mysql_fetch_array($testvend)){
  6. //script to do if vendor exists
  7. }
  8. }
  9.  
The problem is, even if the vendor doesn't exist, it executes the code as if it did. I've tried using
Expand|Select|Wrap|Line Numbers
  1. if(count(mysql_fetch_array($testvend)) > 0){
  2. //script to do if vendor exists
  3. }
  4.  
But it seems to return a value greater than 0 anyway. I'm working on a test page that will do a better job of showing my issues, and I'll post the code here, and the results, when I get it done.

Thanks again for the responses!
Mar 5 '09 #6
Here is the code I am testing with:
(copied and pasted)
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     require("scripts/db_connect.php");
  4.  
  5.     $vendor = 4486;
  6.  
  7.     $result = mysql_query("SELECT * FROM vendor_details WHERE id = '" . $vendor . "'");
  8.  
  9.     if(!$result){
  10.         echo "no vendor " . $vendor . "<br />";
  11.     }else{
  12.         echo "vendor " . $vendor . " exists<br />";
  13.     }
  14.  
  15.     print_r(mysql_fetch_array($result));
  16.     echo "<br />";
  17.  
  18.     $vendor = 6510;
  19.  
  20.     $result = mysql_query("SELECT * FROM vendor_details WHERE id = '" . $vendor . "'");
  21.  
  22.     if(!$result){
  23.         echo "no vendor " . $vendor . "<br />";
  24.     }else{
  25.         echo "vendor " . $vendor . " exists<br />";
  26.     }
  27.  
  28.     print_r(mysql_fetch_array($result));
  29. ?>
  30.  
Here are the results:

vendor 4486 exists
Array ( [0] => 4486 [id] => 4486 [1] => [phone] => [2] => [email] => [3] => [contact] => [4] => German [language] => German [5] => 0 [rating] => 0 )
vendor 6510 exists

And this is my table (copied from phpmyadmin, formatted with | for easier reading - there is only one item it right now)

id | phone | email | contact | language | rating
4486 | | | | German | 0

So, I need to find a way to tell the script that there is no vendor, if there isn't one...
Mar 5 '09 #7
I also decided to test it by providing a variable to the query string istead. I got the same results

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     require("scripts/db_connect.php");
  3.  
  4.     $vendor = 4486;    
  5.     $result = mysql_query("SELECT * FROM vendor_details WHERE id = " . $vendor . "");
  6.     if(!$result){
  7.         echo "no vendor " . $vendor . "<br />";
  8.     }else{
  9.         echo "vendor " . $vendor . " exists<br />";
  10.     }
  11.     while($row = mysql_fetch_array($result)){
  12.         echo "vendor language " . $row['language'];
  13.     }
  14.     print_r(mysql_fetch_array($result));
  15.     echo "<br />";
  16.  
  17.     $query = "SELECT * FROM vendor_details WHERE id = " . $vendor;
  18.     $result = mysql_query($query);
  19.     if(!$result){
  20.         echo "no vendor " . $vendor . "<br />";
  21.     }else{
  22.         echo "vendor " . $vendor . " exists<br />";
  23.     }
  24.     while($row = mysql_fetch_array($result)){
  25.         echo "vendor language " . $row['language'];
  26.     }
  27.     print_r(mysql_fetch_array($result));
  28.     echo "<br />";
  29.  
  30.     $vendor = 6510;
  31.     $result = mysql_query("SELECT * FROM vendor_details WHERE id = " . $vendor . "");
  32.     if(!$result){
  33.         echo "no vendor " . $vendor . "<br />";
  34.     }else{
  35.         echo "vendor " . $vendor . " exists<br />";
  36.     }
  37.     while($row = mysql_fetch_array($result)){
  38.         echo "vendor language" . $row['language'];
  39.     }
  40.     print_r(mysql_fetch_array($result));
  41.     echo "<br />";
  42.  
  43.     $query = "SELECT * FROM vendor_details WHERE id = " . $vendor;
  44.     $result = mysql_query($query);
  45.     if(!$result){
  46.         echo "no vendor " . $vendor . "<br />";
  47.     }else{
  48.         echo "vendor " . $vendor . " exists<br />";
  49.     }
  50.     while($row = mysql_fetch_array($result)){
  51.         echo "vendor language " . $row['language'];
  52.     }
  53.     print_r(mysql_fetch_array($result));
  54.     echo "<br />";
  55. ?>
  56.  
results:
vendor 4486 exists
vendor language German
vendor 4486 exists
vendor language German
vendor 6510 exists

vendor 6510 exists

I took out the print_r because I figured it would be easier to read. I still get the same issue - it keeps telling me the vendor exists when it doesn't.
Mar 5 '09 #8
Markus
6,050 Expert 4TB
mysql_num_rows($result_resource) will tell you the amount of rows affected by the query. So, just run an if conditional, checking that mysql_num_rows($result_resource) turns more than 0 rows.
Mar 5 '09 #9
That worked. I was also just about to post that testing if the resource is null also works.

One of the things I had tried was:
Expand|Select|Wrap|Line Numbers
  1. if(count(mysql_fetch_array($result)) > 0)
  2.  
But that returns 1 in both cases. The mysql_num_rows returns the proper count.

Thanks again for your help and patience!
Mar 5 '09 #10
TheServant
1,168 Expert 1GB
lol, didn't realize it worked like this but it makes sense. mysql_fetch_array() will fetch an array as the name suggests. If the array is empty and you count it, it counts 1 empty array, returning 1. So mysql_num_rows() is the way to go.
Mar 6 '09 #11
Markus
6,050 Expert 4TB
Glad we could be of help. It's always smarter to stick to methods that are there for a specific purpose (using mysql_* functions for mysql related stuff, etc.)

- Mark.
Mar 6 '09 #12

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

Similar topics

4
by: Ryanlawrence1 | last post by:
Heya, I get these 2 errors: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/themepar/public_html/changepass.php on line 20 You have not entered all the...
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...
1
by: lsmamadele | last post by:
I am getting the following error messages in my search: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mamadele/public_html/BESTPLAYS/search.php on...
11
by: Breana | last post by:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/breana/public_html/category.php on line 88 ------------------------------------------- It does this...
4
by: fisherd | last post by:
When i run this code, i keep getting this message; Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\checklogin.php on line 26 i use this code to...
4
by: kalyanip14 | last post by:
Hi I am new to Php.I am trying to read My sql database data from table to php. I am getting this warning Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource ...
2
by: perhapscwk | last post by:
When I run my site from localhost, no error, but when I move it to webhosting, it show below error, why? Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in...
0
Atli
by: Atli | last post by:
What to discuss: What is a "MySQL resource". What causes the error. How to fix it. Common Newbie Pitfalls This article is the second installment in a series of (hopefully) many, following...
1
by: kmacc | last post by:
Hi, I'm getting this error on a page after changing server host, the error did not happen on my old host. I'm thinking it is to do with a new MySQL version and more strict coding. Can anyone help?...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.