Jump to content


Msql query keeps returning empty


1 reply to this topic

#1 ken govaert

    Young Padawan

  • Members
  • Pip
  • 1 posts

Posted 10 September 2005 - 05:33 AM

I'm trying to make a simple flash site which gets its main content from a database. I try to use the database class to connect flash mx and Mysql (found in the exellent beginner/advanced book php5 for flash)
http://www.friendsof...4665/index.html

Anyway, this is the database.php class

<?php
class Database {
  var $dbHost;
  var $dbUser;
  var $dbPass;
  var $dbName;
  var $flash;
  var $dbLink;
  var $result;
  var $resultObj;
  
  function Database($dbHost, $dbUser, $dbPass, $dbName, $flash=1){
    $this->host = $dbHost;
    $this->user = $dbUser;
    $this->pwd = $dbPass;
    $this->dbName = $dbName;
$this->flash = $flash;
$this->connect();
    }

  // Connect to the mySQL Server and Select the database
  function connect() {
    $this->dbLink = @mysql_pconnect($this->host, $this->user, $this->pwd);
    if (!$this->dbLink) {
       $error = 'Couldn\'t connect to mySQL Server';
    echo $this->flash ? 'error='.urlencode($error) : $error;
    exit();
       }
    if (!@mysql_select_db($this->dbName, $this->dbLink)) {
      $error = 'Couldn\'t open Database: '. $this->dbName;
   echo $this->flash ? 'error='.urlencode($error) : $error;
   exit();
   }
return $this->dbLink;  
    }
  // Execute an SQL query
  function query($query) {
    $this->result = @mysql_query($query, $this->dbLink);
if (!$this->result) {
   $error = 'MySQL Error: ' . mysql_error();
   echo $this->flash ? 'error='.urlencode($error) : $error;
   exit();
   }  
// store result in new object to emulate mysqli OO interface
$this->resultObj = new MyResult($this->result);
return $this->resultObj;
    }
  function close(){
    // Close MySQL Connection
    @mysql_close($this->dbLink);
    }
}
class MyResult {
  var $theResult;
  var $num_rows;
  
  function MyResult(&$r) {
    if (is_bool($r)) {
   $this->num_rows = 0;
   }
else {
   $this->theResult = $r;
      // get number of records found
   $this->num_rows = @mysql_num_rows($r);
   }
}
  
  // fetch associative array of result (works on one row at a time)
  function fetch_assoc() {
    $newRow = @mysql_fetch_assoc($this->theResult);
return $newRow;
}
  
  }
?>

and this is a returnData.php script causing the trouble

<?php
require_once('database.php');

$db = new Database('localhost', 'username', 'password', 'database');
$theRow=$_POST['row'];
$sql="SELECT * FROM siteData WHERE section_id='$theRow'";
$result= $db->query($sql);
$row=@mysql_fetch_array($result);
$theText=urlencode($row['section_text']);
$theImage=urlencode($row['section_image']);
echo "&theText=$theText&theImage=$theImage";
$db->close();
?>

The idea is that a table is searched by section_id and this returns text and a link to a .jpg file that should load up. (both of them dont!)
Somehow the script 'works' but the sql query returns 0 and so I dont see data appearing in the swf. So I Tried to alter the query but same $result..empty

<?php

require_once('database.php');


$db = new Database('localhost', 'username', 'passwd', 'dbname', 0);


$sql='SELECT * FROM siteData WHERE section_id="'.$_POST['row'].'"';
$result= $db->query($sql);
$row=mysql_fetch_array($result);
$theText=urlencode($row['section_text']);
$theImage=urlencode($row['section_image']);
echo "&theText=$theText&theImage=$theImage";
$db->close();

?>


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 10 September 2005 - 12:35 PM

Well, i see that in $db->query, if a result isn't returned, it gives off an error. If anything, your flash script should be formatted to accomate the error, so is the an error being returned, and are you handling it correctly?

By correctly, i mean at any point, do you have your flash script displaying the error? If so, please post the error.
If the query is just returning 0 rows, then check your database and make sure something's in it.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users