snteran Posted August 25, 2005 Posted August 25, 2005 I am new to PHP and this is my first site using PHP. I created a website on my test site at home but when I uploaded to TCH, I am getting errors. My first major error is: Fatal error: Call to a member function on a non-object in /home/myvnyuol/public_html/content/news.php on line 23 Code: include_once('conf/main.conf.php'); $story = null; $page = null; $handle = db_connect(); $query = "select * from stories"; line 23 -> $result = $handle->query($query); while ($story = $result->fetch_assoc()) { echo '<table border="0" width="400">'; echo '<tr><td>'; // headline echo "<h2>{$story['headline']}</h2>"; Any Ideas? Thanks, Serge Quote
TCH-Don Posted August 25, 2005 Posted August 25, 2005 Welcome to the forum Serge I assume your config file connects to your database are you using host: localhost database: yourCpanelName_databaseName user: yourCpanelName_databaseUser password: mypassword Its just a guess. Quote
TweezerMan Posted August 25, 2005 Posted August 25, 2005 Welcome to the forums, Serge! Line 23 is giving you an error because you're trying to use object methods (indicated by the '->') instead of calling the regular PHP MySQL functions to run (and fetch) your query. That line should be coded as follows: >$result = mysql_query($query, $handle); You'll also run into the same problem with the next line (line 24). It should be coded as follows: >while ($story = mysql_fetch_assoc($result)) Quote
snteran Posted August 25, 2005 Author Posted August 25, 2005 Welcome to the forum Serge I assume your config file connects to your database are you using host: localhost database: yourCpanelName_databaseName user: yourCpanelName_databaseUser password: mypassword Its just a guess. <{POST_SNAPBACK}> Thank you everyone for the welcomes! I hope not to be such a nuisance as I begin learning how to implement a php site. David, you were right on! I guess there will be a lot of updating of my code. At least I will hopefully get a better understanding of what is actually happening. BTW Don – I used the connect string based off of the information given in the Cpanel DB section. So I was thinking it was more of a rights issue, but now that I know it was a coding issue, and even perhaps a PHP version issue. I’ll make all the needed changes. Thanks again, Serge A True Noobie! Quote
snteran Posted August 25, 2005 Author Posted August 25, 2005 Of course now there is a new error associated with the mysql_fetch_assoc. Error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/myvnyuol/public_html/content/news.php on line 25 I'm not sure what this might mean but I'll visit php.net and see if I can't find any answers. Thanks, Serge Quote
TweezerMan Posted August 25, 2005 Posted August 25, 2005 Just a guess here, but I think the error message is an indication that your query did not execute successfully. Your code as you have it does not check the result of each interaction with the MySQL server to make sure each action completed successfully before continuing on to the next step. For example, you could test the result of your query operation like this: >$result = mysql_query($query, $handle); if (!$result) { die('Query error: ' . mysql_error()); } Ideally, you'd test your $handle variable to make sure you have a valid MySQL link before you run any queries with it: >$handle = db_connect(); if (!$handle) { die('Could not connect: ' . mysql_error()); } It's hard to tell since you're including code for the db_connect() function, but you may need to select a database to work with before running any queries as well: >$db_selected = mysql_select_db('foo', $handle); if (!$db_selected) { die ('Could not select database foo: ' . mysql_error()); } (...where 'foo' is the actual name of your database.) With this extra error detection, the script will give a better error message as to what is wrong, and it will not continue to run past an error where it would be meaningless to do so anyway. Hope this helps... Quote
snteran Posted August 31, 2005 Author Posted August 31, 2005 Just a guess here, but I think the error message is an indication that your query did not execute successfully. Your code as you have it does not check the result of each interaction with the MySQL server to make sure each action completed successfully before continuing on to the next step. For example, you could test the result of your query operation like this: >$result = mysql_query($query, $handle); if (!$result) { die('Query error: ' . mysql_error()); } Ideally, you'd test your $handle variable to make sure you have a valid MySQL link before you run any queries with it: >$handle = db_connect(); if (!$handle) { die('Could not connect: ' . mysql_error()); } It's hard to tell since you're including code for the db_connect() function, but you may need to select a database to work with before running any queries as well: >$db_selected = mysql_select_db('foo', $handle); if (!$db_selected) { die ('Could not select database foo: ' . mysql_error()); } (...where 'foo' is the actual name of your database.) With this extra error detection, the script will give a better error message as to what is wrong, and it will not continue to run past an error where it would be meaningless to do so anyway. Hope this helps... <{POST_SNAPBACK}> thanks for the info David. I guess due to the fact that I am so new to this, I don't understand why everything works fine on my home test box, but after I upload to TCH, I am getting errors. My home enviornment is: Windows 2K pro PHP 5 MySQL 4.1 Thanks for your help, Serge Here is some more of my code in case it helps. Code from db_fns.php function db_connect(){ $handle = new mysqli('localhost', 'loca', 'loca', 'loca'); if (!$handle) { die ('Could not select database LOCA: ' . mysql_error()); } return $handle; } then my code from my news.php file: <?phpinclude_once('conf/main.conf.php'); $story = null; $page = null; $handle = db_connect(); if (!$handle) { die ('Could not select database Loca: ' . mysql_error()); } $query = "select * from stories"; $result = $handle->query($query); Line 29 - > while ($story = $result->fetch_assoc()) { echo '<table border="0" width="400">'; echo '<tr><td>'; // headline echo "<h2>{$story['headline']}</h2>"; echo '</td></tr>'; // byline echo '<tr><td>'; echo substr($story['story_text'],0,60).'.....';; echo '</td></tr>'; echo "<tr> <td> <p align='right' class='morelink'> <a href='content/story.php?pg={$story['id']}'> Read more ... </a> </p> </td> <td width='100'>"; echo '</td></tr>'; } echo '<tr><td colspan="4"><br><br>'; echo '<a href="index.php" class="morelink">Home</a>'; echo '</td></tr>'; echo '</table>'; Quote
TweezerMan Posted August 31, 2005 Posted August 31, 2005 thanks for the info David. I guess due to the fact that I am so new to this, I don't understand why everything works fine on my home test box, but after I upload to TCH, I am getting errors. My home enviornment is: Windows 2K pro PHP 5 MySQL 4.1 <{POST_SNAPBACK}> Essentially, you're programming in a different environment that what exists on the TCH servers: * There are no TCH servers I'm aware of that are running PHP 5 (my server is currently running PHP 4.3.11). You may encounter things that work in PHP 5 (functions and their syntax) but are not backward compatible to PHP 4. * There are no TCH servers I'm aware of that are running MySQL 4.1 either (my server is currently running MySQL 4.0.25). There are known compatibility issues between MySQL 4.0.x and MySQL 4.1. You're also writing PHP code that makes use of functions that are not available on TCH servers. One example: > $handle = new mysqli('localhost', 'loca', 'loca', 'loca'); mysqli is part of the "Improved MySQL Extension" group of PHP functions: The mysqli extension allows you to access the functionality provided by MySQL 4.1 and above. Support for the mysqli extension must be compiled into PHP, and to my knowledge, TCH servers do not have PHP compiled with this option. (Since TCH servers aren't running MySQL 4.1 servers, this extension would not be of much use to anyone.) In order for your code to work on TCH servers, it needs to be written to run in a PHP 4 / MySQL 4.0.x server environment and only call functions that are available in that environment. Your db_connect() function could be rewritten like this to run on a TCH server: >function db_connect() { $handle = mysql_connect('localhost', 'cpanelName_username', 'password'); if (!$handle) { die ('Could not connect to MySQL server: ' . mysql_error()); } return $handle; } And your news.php script would look like this, using my suggestions above: ><?php include_once('conf/main.conf.php'); $story = null; $page = null; $handle = db_connect(); $db_selected = mysql_select_db('cpanelName_foo', $handle); if (!$db_selected) { die ('Could not select database foo: ' . mysql_error()); } $query = "select * from stories"; $result = mysql_query($query, $handle); if (!$result) { die('Query error: ' . mysql_error()); } while ($story = mysql_fetch_assoc($result)) { echo '<table border="0" width="400">'; echo '<tr><td>'; // headline echo "<h2>{$story['headline']}</h2>"; echo '</td></tr>'; // byline echo '<tr><td>'; echo substr($story['story_text'],0,60).'.....';; echo '</td></tr>'; echo "<tr> <td> <p align='right' class='morelink'> <a href='content/story.php?pg={$story['id']}'> Read more ... </a> </p> </td> <td width='100'>"; echo '</td></tr>'; } echo '<tr><td colspan="4"><br><br>'; echo '<a href="index.php" class="morelink">Home</a>'; echo '</td></tr>'; echo '</table>'; Hope this helps... Quote
snteran Posted September 1, 2005 Author Posted September 1, 2005 You are the MAN!!!! Thanks David, no matter what anyone ever says - You are the Man. Gracias, Sergio Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.