mysql_result() error.

Jerry

not rly active lol
Jul 8, 2013
1,957
522
I recieved this error;
Code:
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in *************.php on line 12
Here's the code;
Code:
<?php
// Create connection
$con=mysql_connect("HOST","USERNAME","PASSWORD","DBNAME");

// Check connection
if (!$con)
  {
  echo "Failed to connect to MySQL: " . mysql_connect_error();
  }
?>

<center><li class="active"><center>User(s) Online : <span class="badge"><?php echo mysql_result(mysql_query("SELECT online_now FROM stats;"),0); ?></span> | Total Cloudflare Resolved : <span class="badge"><?php echo mysql_result(mysql_query("SELECT cloudflare_resolved FROM stats;"),0); ?></span> | Total Skype Resolved : <span class="badge"><?php echo mysql_result(mysql_query("SELECT skype_resolved FROM stats;"),0); ?></span> | Online Today : <span class="badge"><?php echo mysql_result(mysql_query("SELECT online_today FROM stats;"),0); ?></span> </center>
wflSoos.png

Any clues?
 

Jerry

not rly active lol
Jul 8, 2013
1,957
522
Code:
--
-- Table structure for table `stats`
--

CREATE TABLE `stats` (
  `online_now` varchar(11) NOT NULL DEFAULT '',
  `cloudflare_resolved` varchar(11) DEFAULT NULL,
  `skype_resolved` varchar(11) DEFAULT NULL,
  `online_today` varchar(11) DEFAULT NULL,
  PRIMARY KEY (`online_now`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `stats`
--

INSERT INTO `stats` VALUES('1', '17', '11', '29');
phfMYHg.png
 

Unidentified

Living the Developer Life...
Jun 19, 2012
144
20
I would like to show you that MySQL_Result is not really needed, you can use it better by doing the folliwing..

Basic MySQL you can make a function :

This is a Single Function. Output it by putting in your code echo Stats('cloudflare_resolved');
Code:
<?php
    function Stats($Retrieve){
        $Query = MySQL_Query("SELECT * FROM `stats`");
        WHILE($Result = MySQL_Fetch_Assoc($Query){
            Return(Mysql_Real_Escape_String($Result[$Retrieve]));
        }
    }
?>
You Should really have a Sanitize Function...
Code:
<?php
    function Sanitize($Data){
        return(mysql_real_escape_string($Data));
    }
    function Stats($Retrieve){
        $Query = MySQL_Query("SELECT * FROM `stats`");
        WHILE($Result = MySQL_Fetch_Assoc($Query){
            Return(Sanitize($Result[$Retrieve]));
        }
    }
?>
For a MySQLi use..

Code:
<?php
    Class DB{
        Private Static $Connect;
      
        Public Function __construct($Localhost, $Username, $Password, $Database)
        {
            self::$Connect = new Mysqli($Localhost, $Username, $Password, $Database);
            if(self::$Connect->connect_errno){
                echo"
                    Couldn't connect to Database
                ";
            }
        }

        Public Static Function Query($Query)
        {
            return(mysqli_query(self::$Connect, $Query));
        }
      
        Public Static Function Sanitize($Data)
        {
            return(self::$Connect->real_escape_string($Data));
        }
      
        Public Static Function Stats($Retrieve){
        global $Mysqli;
            $Query = $Mysqli::Query("SELECT * FROM `stats`");
            WHILE($Result = mysqli_fetch_assoc($Query)){
                Return($Mysqli::Sanitize($Result[$Retrieve]));
            }
        }
    } $Mysqli = new DB("Localhost", "root", "Password", "Database");
?>

To Output this you write in your code echo $Mysqli::Stats('cloudflare_resolved');
 

Marlboro20

Member
Dec 17, 2013
37
6
Not an expert but shouldn't it be like this?
Code:
<?php
// Create connection
$con=mysql_connect('HOST', 'USERNAME', 'PASSWORD')
mysql_select_db('DBNAME', $con);

// Check connection
if (!$con)
{
    echo "Failed to connect to MySQL: " . mysql_connect_error();
}
?>

<center><li class="active"><center>User(s) Online : <span class="badge"><?php echo mysql_result(mysql_query("SELECT online_now FROM stats;"),0); ?></span> | Total Cloudflare Resolved : <span class="badge"><?php echo mysql_result(mysql_query("SELECT cloudflare_resolved FROM stats;"),0); ?></span> | Total Skype Resolved : <span class="badge"><?php echo mysql_result(mysql_query("SELECT skype_resolved FROM stats;"),0); ?></span> | Online Today : <span class="badge"><?php echo mysql_result(mysql_query("SELECT online_today FROM stats;"),0); ?></span> </center>

You could do something like this
PHP:
<?php
// Create connection
$connection = new mysqli('HOST', 'USERNAME', 'PASSWORD', 'DBNAME') or die(mysqli_connect_error());
$stats = function($row) {
    global $connection;
   
    $query = mysqli_query($connection, 'SELECT ' . $row . ' FROM stats;');
    $array = $query->fetch_array(MYSQLI_NUM);
   
    return $array[0];
};
?>
<center><li class="active"><center>User(s) Online : <span class="badge"><?php echo $stats('online_now'); ?></span> | Total Cloudflare Resolved : <span class="badge"><?php echo $stats('cloadflare_resolved'); ?></span> | Total Skype Resolved : <span class="badge"><?php echo $stats('skype_resolved'); ?></span> | Online Today : <span class="badge"><?php echo $stats('online_today'); ?></span> </center>
 

Unidentified

Living the Developer Life...
Jun 19, 2012
144
20
Not an expert but shouldn't it be like this?
Code:
<?php
// Create connection
$con=mysql_connect('HOST', 'USERNAME', 'PASSWORD')
mysql_select_db('DBNAME', $con);

// Check connection
if (!$con)
{
    echo "Failed to connect to MySQL: " . mysql_connect_error();
}
?>

<center><li class="active"><center>User(s) Online : <span class="badge"><?php echo mysql_result(mysql_query("SELECT online_now FROM stats;"),0); ?></span> | Total Cloudflare Resolved : <span class="badge"><?php echo mysql_result(mysql_query("SELECT cloudflare_resolved FROM stats;"),0); ?></span> | Total Skype Resolved : <span class="badge"><?php echo mysql_result(mysql_query("SELECT skype_resolved FROM stats;"),0); ?></span> | Online Today : <span class="badge"><?php echo mysql_result(mysql_query("SELECT online_today FROM stats;"),0); ?></span> </center>

You could do something like this
PHP:
<?php
// Create connection
$connection = new mysqli('HOST', 'USERNAME', 'PASSWORD', 'DBNAME') or die(mysqli_connect_error());
$stats = function($row) {
    global $connection;
  
    $query = mysqli_query($connection, 'SELECT ' . $row . ' FROM stats;');
    $array = $query->fetch_array(MYSQLI_NUM);
  
    return $array[0];
};
?>
<center><li class="active"><center>User(s) Online : <span class="badge"><?php echo $stats('online_now'); ?></span> | Total Cloudflare Resolved : <span class="badge"><?php echo $stats('cloadflare_resolved'); ?></span> | Total Skype Resolved : <span class="badge"><?php echo $stats('skype_resolved'); ?></span> | Online Today : <span class="badge"><?php echo $stats('online_today'); ?></span> </center>
Nice work mate :)

Add a Sanitize function to stop SQL Injection!
 

Marlboro20

Member
Dec 17, 2013
37
6
Oh Okay No Problem, Just ment it for the Person whom you are trying to help... Maybe we should help the community not get hacked on their Websites in this case I think its a retro or Fansite.
Sanitizing data is good practice but there is no way you could get hacked with what i wrote unless you do something like this
PHP:
<?php
echo $stats($_GET['something']);
// or
echo $stats($_POST['something']);
?>
 

Users who are viewing this thread

Top