try this out: $mysql_access = mysql_connect("localhost", "user", "pass") or die("Cannot connect to DB!"); mysql_select_db( "DBname") or die("Cannot select DB!"); $sql = "SELECT asin, name FROM freebs_items ORDER BY name DESC"; $result = mysql_query($sql); $num = mysql_affected_rows(); print "number of records found = " . $num; $i=0; while ($i < $num) { $book_name = mysql_result($result,$i,"name"); $book_asin = mysql_result($result,$i,"asin"); echo ("<a href=\"$book_asin\">$book_name</a><br />"); $i++; } Code (markup):
OK cool - that worked, but now the other elements on the page are trying to pull content from that DB too... I tried adding this to the end of the query, but it didn't seem to do the job... mysql_close($link); I *think* I'm on the right lines here...
what do you mean by this? in any event, in front of database code, you need to specify this again: mysql_select_db("DBname") or die("Cannot select DB!"); replacing "DBname" with whatever database you want the code to use...
Tried that - does the same as mentioned above... Including the ones for the side bar include statements? Yep - done that... It's just the lnclued in other parts of the template are now not working... this will be so much easier... http://www.oweb.co.uk/site-map/
hmmm... The error says: [Table 'stretch_owebbs.wp_posts' doesn't exist] SELECT ID, post_title FROM wp_posts WHERE post_status = 'publish' AND post_password ='' ORDER BY post_date DESC LIMIT 0, 5 is there a table named 'wp_posts' in database 'stretch_owebbs'?
Nope - but the table it *should* be using isn't in that DB - it's in the usual site one - stretch_wrdp1 So I tried closing the connestion to owebbs from our previous query by using mysql_close($mysql_access); But that only casused an error to be repeated over and over between each item... I added the "close connection" line underneath $i++; Is that right?
ah, i get it... your wordpress script is trying to use the database you specified in: $mysql_access = mysql_connect("localhost", "user", "pass") or die("Cannot connect to DB!"); mysql_select_db( "DBname") or die("Cannot select DB!"); what you can do is: $mysql_access = mysql_connect("localhost", "user", "pass") or die("Cannot connect to DB!"); mysql_select_db( "DBname", $mysql_access) or die("Cannot select DB!"); make sure the $mysql_access is a unique variable name not used in wordpress...
yes, closing the connection would work too you need to add it after the while loop: while ($i < $num) { $book_name = mysql_result($result,$i,"name"); $book_asin = mysql_result($result,$i,"asin"); echo ("<a href=\"$book_asin\">$book_name</a><br />"); $i++; } mysql_close($mysql_access); not after the $1++;
Oh ok cool - so what does that do? Simply adding "$mysql_access" doesn't work... You said I need to make it a unique variable - changing it to $mysql_bsaccess gives me Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/stretch/public_html/oweb/wp-content/plugins/phpinposts.php(40) : eval()'d code on line 3 Cannot select DB! I've got this all wrong - clearly...
Ignore that last post... I now have this... <phpcode> $mysql_bsaccess = mysql_connect("localhost", "USER", "PASS") or die("Cannot connect to DB!"); mysql_select_db( "stretch_owebbs", $mysql_bsaccess) or die("Cannot select DB!"); $sql = "SELECT asin, name FROM freebs_items ORDER BY name DESC"; $result = mysql_query($sql); $num = mysql_affected_rows(); print "number of records found = " . $num; $i=0; while ($i < $num) { $book_name = mysql_result($result,$i,"name"); $book_asin = mysql_result($result,$i,"asin"); echo ("<li><a href=\"$book_asin\">$book_name</a>"); $i++; }</phpcode> But it's not doing the trick... Closing it didn't work either... - Scratch that, just saw your last post... Now I get this where the sidebar content should be appearing - Warning: Invalid argument supplied for foreach() in /home/stretch/public_html/oweb/my-hacks.php on line 9 I got this far using this... $mysql_access = mysql_connect("localhost", "USER", "PASS") or die("Cannot connect to DB!"); mysql_select_db( "stretch_owebbs") or die("Cannot select DB!"); $sql = "SELECT asin, name FROM freebs_items ORDER BY name DESC"; $result = mysql_query($sql); $num = mysql_affected_rows(); print "number of records found = " . $num; $i=0; while ($i < $num) { $book_name = mysql_result($result,$i,"name"); $book_asin = mysql_result($result,$i,"asin"); echo ("<li><a href=\"$book_asin\">$book_name</a></li>"); $i++; } mysql_close($mysql_access); PHP:
did you try closing the connection as suggested in post #48? otherwise: if you want to use $mysql_bsaccess, make sure you've got it in both the mysql_connect and the mysql_select_db. see the following: $mysql_bsaccess = mysql_connect("localhost", "user", "pass") or die("Cannot connect to DB!"); mysql_select_db( "DBname", $mysql_bsaccess) or die("Cannot select DB!");
Yep - tried that, still getting the "Warning: Invalid argument supplied for foreach() in /home/stretch/public_html/oweb/my-hacks.php on line 9" Displayed now... Sorry about this hassle - I get confused with all this pretty easily...
well, at least the menus are appearing now... making some headway... you mind posting line 9 of /home/stretch/public_html/oweb/my-hacks.php?
Of course... Only I'm not sure that's the issue - there are 2 things that need to be displayed on the left, but the first is the only one actually outputting an error... myhacks is... <?php function get_recent_posts($no_posts = 5, $before = '<li>', $after = '</li>', $show_pass_post = false, $skip_posts = 0) { global $wpdb, $tableposts; $request = "SELECT ID, post_title FROM $tableposts WHERE post_status = 'publish' "; if(!$show_pass_post) { $request .= "AND post_password ='' "; } $request .= "ORDER BY post_date DESC LIMIT $skip_posts, $no_posts"; $posts = $wpdb->get_results($request); $output = ''; foreach ($posts as $post) { $post_title = stripslashes($post->post_title); $permalink = get_permalink($post->ID); $output .= $before . '<a href="' . $permalink . '" rel="bookmark" title="Permanent Link: ' . $post_title . '">' . $post_title . '</a>' . $after; } echo $output; } ?> PHP:
sebasic, i'm fairly certain that you're hitting this problem because you have multiple databases on the same server and although you call mysql_connect with a new handle, mysql_connect actually returns an existing handle if the parameters you specify for mysql_connect are the same. in your case, you're calling: $mysql_access = mysql_connect("localhost", "USER", "PASS") or die("Cannot connect to DB!"); mysql_select_db( "stretch_owebbs", $mysql_access) or die("Cannot select DB!"); are the parameters in blue the same as those used by your other database? if so, you can try: $mysql_access = mysql_connect("127.0.0.1", "USER", "PASS") or die("Cannot connect to DB!"); mysql_select_db( "stretch_owebbs", $mysql_access) or die("Cannot select DB!");
Yeah they prolly are the same... They were... And now - I get this again where the stretch_owebbs is supposed to output... Parse error: parse error, unexpected T_STRING in /home/stretch/public_html/oweb/wp-content/plugins/phpinposts.php(40) : eval()'d code on line 2 That's after I changed 'localhost' to '127.0.0.1'
ah... good... at least it's not a database problem anymore... care to post: /home/stretch/public_html/oweb/wp-content/plugins/phpinposts.php(40) : eval()'d code on line 2
This is the whole of that particular plugin... <?php /* Plugin Name: Run PHP Version: 0.2.2 Plugin URI: http://mark.scottishclimbs.com/archives/2004/07/02/running-php-in-wordpress-posts/ Description: Allows PHP to run in posts by using custom HTML style tags Author: Mark Somerville Author URI: http://mark.scottishclimbs.com/ */ /* run_php This function runs and outputs PHP code that exists within <phpcode></phpcode> tags. */ function run_php($data) { $tag = "phpcode"; $taglength = strlen($tag); //html_entity_decode doesn't work! These lines change ' and " instead. Maybe other entities needed? $data = str_replace(array("‘", "’"), "'",$data); $data = str_replace(array("â€", "“"), '"', $data); $data = str_replace("″", '"', $data); $data = str_replace("′", "'", $data); while($phpstart = strpos($data, "<".$tag.">")) { $phpend = strpos($data, "</".$tag.">"); $phptrueend = $phpend + $taglenth + 3 + $taglength; $before = substr($data, 0, $phpstart); $after = substr($data, $phptrueend); $phpcodelength = $phpend - ($phpstart+2+$taglength); $phpcode = substr($data, $phpstart+2+$taglength, $phpcodelength); //remove the <br /> and <p></p> tags that WP adds to the code $phpcode = str_replace('<br />', ' ', $phpcode); $phpcode = str_replace('<p>', '', $phpcode); $phpcode = str_replace('</p>', '', $phpcode); ob_start(); eval($phpcode); $data = $before.ob_get_clean().$after; } return $data; } add_filter('the_content', 'run_php'); ?> PHP: It lets you run php queries within a wordpress post by using the tag <phpcode></phpcode> instead of <?php ?>
could you put echo "<pre>$phpcode</pre>"; PHP: before the eval line? That will tell you what code is being executed and should help you determine the problem