Page 1 of 1

script working locally but not online

Posted: Sun Apr 11, 2010 4:05 pm
by geoffmuskett
Hi there,

I'm new to PHP but managed to get this script working lovely locally, very pleased with myslef...then I uploaded it...and nothing is displayed in that area! the site is http://www.tyrefinders.co.uk/testing/index.php.

The scripts uses the name of the current page ($thisPage), uses regex to take out caps and spaces, adds "_paragraphs" to the now lowercase and one word page name. This relates to the name of the table in the database, then the results are looped.

I'm too inexperienced to know why it isn't working live, can anyone spot a possible reason? Maybe something to do with the regex?? The first chunk- connect to server and select database works because its used elsewhere in the site.

Code: Select all

<?php
// connect to db
require_once 'a/inc/login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
// unable to connect
if (!$db_server) die("unable to connect to MYSQL: " . mysql_error());
// select db
mysql_select_db($db_database)
	or die("unable to select database: " . mysql_error());



// regulars expressions to find spaces, tabs, newlines etc 
$sPattern = '/\s*/m';
// replace spaces, tabs or whatever with no space
$sReplace = '';
// apply expressions and replace to a string and assign to a variable
$thisPage_no_space = preg_replace( $sPattern, $sReplace, $thisPage );

// assign table value with page name
$table = $thisPage_no_space . "_paragraphs";

// get data
$query="SELECT * FROM $table";
$result=mysql_query($query);

// find the ammount of rows
$num=mysql_numrows($result);

mysql_close();
// loop results until number or rows is reached
$i=0;
while ($i < $num) {
// assign selected results to variables
$heading=mysql_result($result,$i,"heading");
$paragraph=mysql_result($result,$i,"paragraph");

echo "<img src=\"a/images/content_left_top.gif\" width=\"475\" height=\"10\" alt=\"top\" />
	<div class=\"content_text\"><img src=\"a/images/content_tittle_arrows.gif\" width=\"20\" height=\"9\" alt=\"arrows\" /><h2>$heading</h2>
	<p>$paragraph</p>
	</div><!-- close content_text -->
	<img class=\"bottom15\" src=\"a/images/content_left_bottom.gif\" width=\"475\" height=\"10\" alt=\"top\" />";
if ($i==0) {
	echo "<img src=\"a/images/tyres_exhausts_tracking_batteries.gif\" width=\"475\" height=\"70\" alt=\"Tyres Exhausts Tracking Batteries\" />";	
}

$i++;
}
?>
Thanks for your time!

Geoff

Re: script working locally but not online

Posted: Sun Apr 11, 2010 4:22 pm
by requinix
1. Check that you actually have stuff in the database table.
2. How do you determine $thisPage?

Re: script working locally but not online

Posted: Wed Apr 21, 2010 3:17 pm
by geoffmuskett
Hi, thanks for the reply tasairis. Sorry I haven't got back to you in a while... tonsillitis is horrendous!

I define it like this: <?php $thisPage="Home"; ?>

So in this case after the regex it becomes home_paragraphs. There's definitely stuff in the database because it works if I manually insert the value:

Code: Select all

$query="SELECT * FROM home_paragraphs";
Any ideas?

Thanks,

Geoff

Re: script working locally but not online

Posted: Wed Apr 21, 2010 3:31 pm
by requinix
Put an

Code: Select all

or die(mysql_error())
after the mysql_query and see what MySQL is complaining about.

(And change the $sPattern to "/\s+/m")

Re: script working locally but not online

Posted: Wed Apr 21, 2010 4:12 pm
by mikosiko
Geoff... in your main page you are getting this error:
"Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /homepages/18/d209788419/htdocs/testing/a/inc/paragraphs.php on line 28"

likely it means that your query is failing

Re: script working locally but not online

Posted: Thu Apr 22, 2010 7:06 am
by dmsundar
hi,,

try changing

$query="SELECT * FROM $table";
to
$query="Select * from " . $table ;

Re: script working locally but not online

Posted: Thu Apr 22, 2010 3:58 pm
by geoffmuskett
Thanks for your replies, I appreciate your help! After all that it turns out my regex wasn't doing what I thought! It's taking out the spaces but not making the string lowercase. For some reason this didn't matter locally, it all worked, but my hosts php seems to be fussier. Anyway I used strolower, and life is good again. I now have:

Code: Select all

<?php
// connect to db
require_once 'a/inc/login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
// unable to connect
if (!$db_server) die("unable to connect to MYSQL: " . mysql_error());
// select db
mysql_select_db($db_database)
	or die("unable to select database: " . mysql_error());

// regulars expressions to find spaces, tabs, newlines etc 
$sPattern = '/\s+/m';
// replace spaces, tabs or whatever with no space
$sReplace = '';
// apply expressions and replace to a string and assign to a variable
$thisPage_no_space = preg_replace( $sPattern, $sReplace, $thisPage );

// assign table value with page name
$table = $thisPage_no_space . "_paragraphs";
// make lowercase
$table_lowercase = strtolower($table);
// get data
$query="Select * from $table_lowercase" ;
$result=mysql_query($query);

// find the ammount of rows
$num=mysql_numrows($result);

mysql_close();
// loop results until number or rows is reached
$i=0;
while ($i < $num) {
// assign selected results to variables
$heading=mysql_result($result,$i,"heading");
$paragraph=mysql_result($result,$i,"paragraph");

echo "<img src=\"a/images/content_left_top.gif\" width=\"475\" height=\"10\" alt=\"top\" />
	<div class=\"content_text\"><img src=\"a/images/content_tittle_arrows.gif\" width=\"20\" height=\"9\" alt=\"arrows\" /><h2>$heading</h2>
	<p>$paragraph</p>
	</div><!-- close content_text -->
	<img class=\"bottom15\" src=\"a/images/content_left_bottom.gif\" width=\"475\" height=\"10\" alt=\"top\" />";
if ($i==0) {
	echo "<img src=\"a/images/tyres_exhausts_tracking_batteries.gif\" width=\"475\" height=\"70\" alt=\"Tyres Exhausts Tracking Batteries\" />";	
}

$i++;
}
?>
This PHP malarkey is a long and rocky road!
Thanks again for your time!