Recordset in WordPress: mysql_error(): 9 is not a valid MySQL-Link resource

lshane
lshane used Ask the Experts™
on
Hello.

I am using WordPress 2.7 with a PHP plugin caled "exec_PHP", which is supposed to allow PHP code to be processed within the body of a post.

The PHP code is processed in the body if it is simple, such as displaying current date and time, etc.

However, I am trying to perform a simple SELECT statement recordset from the [wp_users] table, displaying the {user_login} and {user_email}.

I ran the script on a blank PHP page, and it works perfectly.

I copied to the post of WordPress.  it displays the results, but it also displays the following error:
Warning: mysql_error(): 9 is not a valid MySQL-Link resource in /home/content/s/t/a/startertemp/html/wp-includes/wp-db.php on line 615

I attached the code I am using, and will also attach a screenshot containing the error and the recordset results (which are accurate).

Please help.

Thanks so much,
Shane
<?php
$con = mysql_connect("starterwpdata.db.4972497.hostedresource.com","starterwpdata","Starterdata1");
if (!$con)
  {
  die ('Unable to connect to database! Please try again later.');
  }
 
mysql_select_db("starterwpdata", $con);
 
$result = mysql_query("SELECT * FROM wp_users");
 
 
 
while($row = mysql_fetch_array($result))
  {
  echo $row['user_login'] . " " . $row['user_email'];
  echo "<br />";
  }
 
 
 
 
mysql_close($con);
?>

Open in new window

ee-error.jpg
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Can you try changing your code to the below and see if you still have errors? It's pretty much the same as what you have, but uses the wordpress built-in database calls.

<?php
	global $wpdb;
	$result = $wpdb->get_results("SELECT * FROM $wpdb->users");
	foreach ($result as $row) {
	  echo $row['user_login'] . " " . $row['user_email'];
	  echo "<br />";
	}
?>

Open in new window

Commented:
Scratch the last attached code, I pasted the wrong chunk in that wasn't completely edited, please use the below.

<?php
	global $wpdb;
	$result = $wpdb->get_results("SELECT * FROM $wpdb->users");
	foreach ($result as $row) {
	  echo $row->user_login . " " . $row->user_email;
	  echo "<br />";
	}
?>

Open in new window

Author

Commented:
Wow, Dzynit.  That worked great!  Thank you.

It was mainly an experiment, because ultimatey I want to be able to list all thumbnail images from another table, but wanted to make sure I could run recordsets.

I will hopefully be able to apply what you provided to my thumbnail list.  if not... I will be back.  LOL

Thanks so much!
Shane
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Author

Commented:
Excellent.

Author

Commented:
One question, though.  I noticed in PHP Admin that the tables are named [wp_users], etc.--- prefixed with "wp_".  I noticed your example only has [$wpdb->users].  I guess $wpdb is a built-in variable of some sort for WP, but I am assuming if I use this method, that I would still use that [$wpdb->users] and just swap out "users" for whatever other table I am using.

Example:
If it is [wp_products], then I would use [$wpdb->products], or whatever.

It seems there are built-in table structures for WP also.

Would you know where I could find all that information?


Thank you!
Shane

Commented:
Yes, the $wpdb at the beginning let's wordpress add the chosen prefix for the wordpress install.

http://codex.wordpress.org/Function_Reference/wpdb_Class#Tables gives you examples of the queries and at the bottom in the table list, if you click on the link in that list to the table you're working with, it will give you a list of the fields and what's in them for reference.

(Since the prefix was different in your install, that's most likely why the first attempt wasn't working for you, but I'd still suggest getting familiar with the slight variation using the wordpress's built-in query system just to help avoid problems - especially as wordpress releases new versions. I actually found that using the wordpress calls were neater and simpler.)

Author

Commented:
Great - thanks again!


Shane

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial