Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

What is wrong with code?

Featured Replies

Hello, I am currently delving into the world of OO and I am hitting problems I am currently trying to create a cms and I have got the CMS inserting data into the database and pulling it out until I try view the full article, when I get this error...now from what I know this means that it fetched no data so the table must be empty but this is not so does any one have any ideas?

<?php
// Require the database class
require_once('includes/DbConnector.php');

// Create a new DbConnector
$connector = new DbConnector();

// IMPORTANT!!! Validate the ID number.

// Execute the query to retrieve the selected article
$result = $connector->query('SELECT title,thearticle FROM cmsarticles WHERE ID = '.$HTTP_GET_VARS['id']);

// Get an array containing the resulting record
$row = $connector->fetchArray($result);

?>

Your selected article: <?php echo $row['title'];?>
<br><br>
<?php echo $row['thearticle'];?>

 

<?php
////////////////////////////////////////////////////////////////////////////////////////
// Class: DbConnector
// Purpose: Connect to a database, MySQL version
///////////////////////////////////////////////////////////////////////////////////////
require_once 'SystemComponent.php';

class DbConnector extends SystemComponent {

var $theQuery;
var $link;

//*** Function: DbConnector, Purpose: Connect to the database ***
function DbConnector(){

	// Load settings from parent class
	$settings = SystemComponent::getSettings();

	// Get the main settings from the array we just loaded
	$host = $settings['dbhost'];
	$db = $settings['dbname'];
	$user = $settings['dbusername'];
	$pass = $settings['dbpassword'];

	// Connect to the database
	$this->link = mysql_connect($host, $user, $pass);
	mysql_select_db($db);
	register_shutdown_function(array(&$this, 'close'));

}

//*** Function: query, Purpose: Execute a database query ***
function query($query) {

	$this->theQuery = $query;
	return mysql_query($query, $this->link);

}

//*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result) {

return mysql_fetch_array($result); //this is line 46

}

//*** Function: close, Purpose: Close the connection ***
function close() {

	mysql_close($this->link);

}


}
?>

Could you tell us the error message?

  • Author

sorry meant to copy and paste and forgot ha ha the error message is,

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\Outlane\includes\DbConnector.php on line 43

 

the line that says //this is line 46 should actually read this is line 43 by the way

First thing i usually do is try echoing the sql, then copy it over and run it on your database directly, make sure theres no typos etc.

 

From first glance, is your id column id or ID?

 

You will want to validate that $HTTP_GET_VARS['id'] before you run the statement, your leaving yourself open for injection attacks.

  • Author

Ok thanks for that tip, how would I go about validating it, I am horrendously new to PHP and I am currently learning straight from a book.

Add this method to your DbConnector class:

public function escape($input) {	  // You might also call this validate or sanitize
return mysql_real_escape_string($input);
}

It escapes the input so it is safe for interpolating with SQL without the risk of SQL injection attacks (see documentation for this function). However a more modern approach is to use parameterized SQL. This would also mean you would need to move away from the mysql_* functions to use mysqli or PDO. Its best to take one step at a time when you are learning so this solution would be fine :).

 

You would then use it when executing your query like this:

// Execute the query to retrieve the selected article
$result = $connector->query('SELECT title,thearticle FROM cmsarticles WHERE ID = ' . $connector->escape($HTTP_GET_VARS['id']));

 

Hope this helps with that. As for your original problem, are you running on PHP 4 or PHP 5? I would hope 5, 4 is VERY out of date (and you will need to remove the word "public" from my sample to make it work on 4). PHP 5 deprecates the old long array names which you are using.

 

Instead of using $HTTP_GET_VARS['whatever'], use $_GET['whatever']. So my previous code snippet would now look like:

// Execute the query to retrieve the selected article
$result = $connector->query('SELECT title,thearticle FROM cmsarticles WHERE ID = ' . $connector->escape($_GET['id']));

See how that goes for you, and come back if you have problems. :D

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.