Jump to content

Calling data from MySQL


cheechm

Recommended Posts

Can't seem to get this to work:

index.php

<?php
////////////////////////////////////////////////////////////////////////////////////////
// Class: Index
// Purpose: The main page of the site
///////////////////////////////////////////////////////////////////////////////////////
require_once 'includes/functions.php'; 


    //*** Function: Define variables, Purpose: Make life easier ***
    
$con = new DbConnect();
session_start();

//Checks if there is a login cookie
if(isset($_COOKIE[".SITE_NAME."]))


if(!isset($username) | !isset($password)) { //check if they are logged in

//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE[".SITE_NAME."];
$pass = $_COOKIE[".SITE_NAME."];
$check = mysql_query("SELECT * FROM ".TBL_USERS." WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
}
else
{
$this->session_status==True;
header("Location: index.php");
} } } }


//start constructing the page



echo SITE_NAME;
echo "<br>";


// Use the query function of DbConnect to run a database query
$result = $con->query('SELECT * FROM ".TBL_ARTICLES."');

// Get the result
$row = $con->fetchArray($result);

// Show it to the user
echo $row['articletext'];

$con->close();

?>

 

functions.php

<?php
////////////////////////////////////////////////////////////////////////////////////////
// Class: Functions
// Purpose: Contains all the functions the user may need
///////////////////////////////////////////////////////////////////////////////////////
require 'config.php';


class DBConnect extends SystemComponent {

    var $theQuery;
    var $link;

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

    // 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, $this);
    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);

}

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

    @mysql_close($this->link);

}
}
?>

 

I have checked. It can connect to the DB. However I can't seem to pull the article text from the DB.

Anyone see why?

Thanks

 

Please use code tags for large amounts of code. Use PHP tags for small snippets of code

Link to comment
https://forums.phpfreaks.com/topic/73025-calling-data-from-mysql/
Share on other sites

index.php:

<?php
////////////////////////////////////////////////////////////////////////////////////////
// Class: Index
// Purpose: The main page of the site
///////////////////////////////////////////////////////////////////////////////////////
require_once 'includes/functions.php'; 


    //*** Function: Define variables, Purpose: Make life easier ***
    
$con = new DbConnect();
session_start();

//Checks if there is a login cookie
if(isset($_COOKIE[".SITE_NAME."]))


if(!isset($username) || !isset($password)) { //check if they are logged in

//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE[".SITE_NAME."];
$pass = $_COOKIE[".SITE_NAME."];
$check = mysql_query("SELECT * FROM ".TBL_USERS." WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
}
else
{
$this->session_status==True;
header("Location: index.php");
} } } } 


//start constructing the page



echo SITE_NAME;
echo "<br>";


// Use the query function of DbConnect to run a database query
$result = $con->query("SELECT * FROM articles");

// Get the result
$row = $con->fetchArray($result);

// Show it to the user
echo $row['articletext'];

$con->close();

?>

 

functions.php:

<?php
////////////////////////////////////////////////////////////////////////////////////////
// Class: Functions
// Purpose: Contains all the functions the user may need
///////////////////////////////////////////////////////////////////////////////////////
require 'config.php';


class DBConnect extends SystemComponent {

    var $theQuery;
    var $link;

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

    // 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", $this->link)or die(mysql_error());
    register_shutdown_function(array(&$this, 'close'));

}

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

    $this->theQuery = $query;
    return mysql_query($query)or die(mysql_error());

}

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

    return mysql_fetch_array($result)or die(mysql_error());

}

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

    mysql_close($this->link);

}
}

 

Error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /includes/functions.php on line 44

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.