Jump to content

[SOLVED] mysql select error,


blueman378

Recommended Posts

hi guys i have this code:

<?php
include("include/session.php");

function GetInfo(){
   global $database;
   $sql = "SELECT * FROM 'games.games' WHERE gName='23D Space Skimmer' ORDER BY `gName` ASC'";
   $result = $database->query($q) or die("Error: " . mysql_error());
    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    } 
   /* Display table contents */
   echo "<table align=\"left\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n";
   echo "<tr><td><b>Game Name</b></td><td><b>Description</b></td><td><b>Swf File</b></td><td><b>Thumb File</b></td></tr>\n";
   for($i=0; $i<$num_rows; $i++){
      $name   = $row["gName"]; 
      $desc  = $row["gDescription"]; 
      $swf   = $row["gSwfFile"]; 
      $thumb    = $row["gThumb"]; 

      echo "<tr><td>mysql_result($result,$i,'gName')</td><td>$udesc</td><td>$swf</td><td>$thumb</td></tr>\n";
   }
   echo "</table><br>\n";
}

?>

<?
GetInfo();
?>

 

which is meant to get the data from the database and display it in a form (db connection done in session.php

 

but i get this error: "Error: Query was empty"

 

my table looks like this:

img

 

thanks 4 ur help

Link to comment
Share on other sites

hi i fixed the errors,

so heres my new code:

<?php
include("include/session.php");

function GetInfo(){
   global $database;
$q = "SELECT * "
       ."FROM ".Games." WHERE gName='3D Space Skimmer' ORDER BY `gName` ASC";   
   $result = $database->query($q) or die("Error: " . mysql_error());
    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    } 
   /* Display table contents */
   echo "<table align=\"left\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n";
   echo "<tr><td><b>Game Name</b></td><td><b>Description</b></td><td><b>Swf File</b></td><td><b>Thumb File</b></td></tr>\n";
   for($i=0; $i<$num_rows; $i++){
      $name   = mysql_result($result,$i,"gName"); 
      $desc   = mysql_result($result,$i,"gDescription");
      $swf    = mysql_result($result,$i,"gSwfFile");
      $thumb  = mysql_result($result,$i,"thumb");

      echo "<tr><td>$name</td><td>$desc</td><td>$swf</td><td>$thumb</td></tr>\n";
   }
   echo "</table><br>\n";
}

?>

<?
GetInfo();
?>

 

but now when i run it i get

 

______________________________________

|Game name|Description|Swf File|Thumb File|

 

and thats it no data under it, i know it is searching it because if i change the where statment to one thats not true it says nothing to display,

 

cheers

Link to comment
Share on other sites

ah got it,

 

<?php
include("include/session.php");

function GetInfo(){
   global $database;
$q = "SELECT * "
       ."FROM ".Games." WHERE gName='3D Space Skimmer' ORDER BY `gName` ASC";   
   $result = $database->query($q) or die("Error: " . mysql_error());
   /* Error occurred, return given name by default */
   $num_rows = mysql_numrows($result);
   if(!$result || ($num_rows < 0)){
      echo "Error displaying info";
      return;
   }
   if($num_rows == 0){
      echo "Database table empty";
      return;
   }
   /* Display table contents */
   echo "<table align=\"left\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n";
   echo "<tr><td><b>Game Name</b></td><td><b>Description</b></td><td><b>Swf File</b></td><td><b>Thumb File</b></td></tr>\n";
   for($i=0; $i<$num_rows; $i++){
      $name   = mysql_result($result,$i,"gName"); 
      $desc   = mysql_result($result,$i,"gDescription");
      $swf    = mysql_result($result,$i,"gSwfFile");
      $thumb  = mysql_result($result,$i,"gthumb");

      echo "<tr><td>$name</td><td>$desc</td><td>$swf</td><td>$thumb</td></tr>\n";
   }
   echo "</table><br>\n";
}

?>

<?
GetInfo();
?>

 

so now i need to modify this code because the url for the page will be

showgame.php?game=Game name

so i need the where tag to be a variable to get the name from the url tag

 

cheers

Link to comment
Share on other sites

Learn to insert quick 'n dirty code snippets to trouble shoot with. Especially dumping variables and such. You should echo out your SQL Query, and the resultant array:

 

<?php
$q = "SELECT * FROM " . Games . "
     WHERE gName = '3D Space Skimmer'
     ORDER BY `gName` ASC
    ";   
$result = $database->query($q) or die("Error: " . mysql_error());
//debug
$row = mysql_fetch_array($result);
echo "Query:<br>$q<br><pre>";
print_r($row);
die("</pre>");
//end debug
if(mysql_num_rows($result) == 0){
   echo("Nothing to Display!");
}

 

PhREEEk

 

Link to comment
Share on other sites

You should start loading your data results into arrays instead of using them 1 at a time like you are... it will make things much easier for you down the road...

 

<?php
include("include/session.php");

function GetInfo(){
    global $database;
    $q = "SELECT * FROM " . Games . "
          WHERE gName = '3D Space Skimmer'
          ORDER BY `gName` ASC
         ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if( $num_rows == 0 ){
      return 'Database table empty';
    }
    /* Display table contents */
    $content = "<table align=\"left\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">
  <tr>
    <td><b>Game Name</b></td>
    <td><b>Description</b></td>
    <td><b>Swf File</b></td>
    <td><b>Thumb File</b></td>
  </tr>
";
    while( $row = mysql_fetch_assoc($result) ) {
        $content .= "  <tr>
    <td>$row['gName']</td>
    <td>$row['gDescription']</td>
    <td>$row['gSwfFile']</td>
    <td>$row['gthumb']</td>
  </tr>
";
    }

    $content .= "</table><br>\n";
    return $content;
}

// You can
echo GetInfo();

// or
$showGames = GetInfo();
echo $showGames;

?>

 

PhREEEk

Link to comment
Share on other sites

<?php
include("include/session.php");

function GetInfo(){
    global $database;
    $q = "SELECT * FROM " . Games . "
          WHERE gName = '3D Space Skimmer'
          ORDER BY `gName` ASC
         ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if( $num_rows == 0 ){
      return 'Database table empty';
    }
    /* Display table contents */
    $content = "<table align=\"left\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">
  <tr>
    <td><b>Game Name</b></td>
    <td><b>Description</b></td>
    <td><b>Swf File</b></td>
    <td><b>Thumb File</b></td>
  </tr>
";
    while( $row = mysql_fetch_assoc($result) ) {
        $content .= "<tr>
    <td>$row['gName']</td>
    <td>$row['gDescription']</td>
    <td>$row['gSwfFile']</td>
    <td>$row['gthumb']</td>
  </tr>
";
    }

    $content .= "</table><br>\n";
    return $content;
}

// You can
echo GetInfo();
?>

 

i tried using that but get:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\showgame.php on line 27

 

any ideas, n thanks so much 4 all ur help,

Link to comment
Share on other sites

Sorry.. forgot the curly braces around arrays...

 

try this:

 

<?php
include("include/session.php");

function GetInfo(){
    global $database;
    $q = "SELECT * FROM " . Games . "
          WHERE gName = '3D Space Skimmer'
          ORDER BY `gName` ASC
         ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if( $num_rows == 0 ){
      return 'Database table empty';
    }
    /* Display table contents */
    $content = "<table align=\"left\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">
  <tr>
    <td><b>Game Name</b></td>
    <td><b>Description</b></td>
    <td><b>Swf File</b></td>
    <td><b>Thumb File</b></td>
  </tr>
";
    while( $row = mysql_fetch_assoc($result) ) {
        $content .= "  <tr>
    <td>{$row['gName']}</td>
    <td>{$row['gDescription']}</td>
    <td>{$row['gSwfFile']}</td>
    <td>{$row['gthumb']}</td>
  </tr>
";
    }

    $content .= "</table><br>\n";
    return $content;
}

// You can
echo GetInfo();

// or
$showGames = GetInfo();
echo $showGames;

?>

 

PhREEEk

Link to comment
Share on other sites

<?php
include("include/session.php");

function GetInfo(){
    global $database;
    $q = "SELECT * FROM " . Games . "
          WHERE gName = '3D Space Skimmer'
          ORDER BY `gName` ASC
         ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if( $num_rows == 0 ){
      return 'Database table empty';
    }
    /* Display table contents */
    $content = "<table align=\"left\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">
  <tr>
    <td><b>Game Name</b></td>
    <td><b>Description</b></td>
    <td><b>Swf File</b></td>
    <td><b>Thumb File</b></td>
  </tr>
";
    while( $row = mysql_fetch_assoc($result) ) {
        $content .= "<tr>
    <td>$row[gName]</td>
    <td>$row[gDescription]</td>
    <td>$row[gSwfFile]</td>
    <td>$row[gThumb]</td>
  </tr>
";
    }

    $content .= "</table><br>\n";
    return $content;
}

// You can
echo GetInfo();
?>

worked aswell thanks, but any ideas on the variable question about where?

 

i tried

$_GET['game'] and $_POST['game']

but get table empty, also i need a script to remove the %20 and replace it with a space, any ideas?

 

thanks 4 all ur help

Link to comment
Share on other sites

ok well lets say (simply because the only game i have in my database is 3D Space Skimmer) the url is

showgame.php?game=3D Space Skimmer

 

the code i need is something like

 

<?php
include("include/session.php");

$game = $_POST['game'];
$game = urldecode($game);

function GetInfo(){
    global $database;
    $q = "SELECT * FROM " . Games . "
          WHERE gName = '$game'
          ORDER BY `gName` ASC
         ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if( $num_rows == 0 ){
      return 'Database table empty';
    }
    /* Display table contents */
    $content = "<table align=\"left\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">
  <tr>
    <td><b>Game Name</b></td>
    <td><b>Description</b></td>
    <td><b>Swf File</b></td>
    <td><b>Thumb File</b></td>
  </tr>
";
    while( $row = mysql_fetch_assoc($result) ) {
        $content .= "<tr>
    <td>$row[gName]</td>
    <td>$row[gDescription]</td>
    <td>$row[gSwfFile]</td>
    <td>$row[gThumb]</td>
  </tr>
";
    }

    $content .= "</table><br>\n";
    return $content;
}

// You can
echo GetInfo();
?>

 

but i always get Database table empty

 

thanks

 

Link to comment
Share on other sites

Again, let's dump our variable and see what's going on...

 

Save this script and run your URL. Post the result.

 

<?php
include("include/session.php");

echo "<pre>";
print_r($_GET);
die("</pre>");

$game = $_POST['game'];
$game = urldecode($game);

function GetInfo(){
    global $database;
    $q = "SELECT * FROM " . Games . "
          WHERE gName = '$game'
          ORDER BY `gName` ASC
         ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if( $num_rows == 0 ){
      return 'Database table empty';
    }
    /* Display table contents */
    $content = "<table align=\"left\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">
  <tr>
    <td><b>Game Name</b></td>
    <td><b>Description</b></td>
    <td><b>Swf File</b></td>
    <td><b>Thumb File</b></td>
  </tr>
";
    while( $row = mysql_fetch_assoc($result) ) {
        $content .= "<tr>
    <td>$row[gName]</td>
    <td>$row[gDescription]</td>
    <td>$row[gSwfFile]</td>
    <td>$row[gThumb]</td>
  </tr>
";
    }

    $content .= "</table><br>\n";
    return $content;
}

// You can
echo GetInfo();
?>

 

PhREEEk

Link to comment
Share on other sites

Do the same thing again... (save this new script, run the same URL, post result)

 

<?php
include("include/session.php");

$game = $_GET['game'];

function GetInfo(){
    global $database;
    $q = "SELECT * FROM " . Games . "
          WHERE gName = '$game'
          ORDER BY `gName` ASC
         ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */

die("SQL:<br>$q");

    $num_rows = mysql_numrows($result);
    if( $num_rows == 0 ){
      return 'Database table empty';
    }
    /* Display table contents */
    $content = "<table align=\"left\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">
  <tr>
    <td><b>Game Name</b></td>
    <td><b>Description</b></td>
    <td><b>Swf File</b></td>
    <td><b>Thumb File</b></td>
  </tr>
";
    while( $row = mysql_fetch_assoc($result) ) {
        $content .= "<tr>
    <td>$row[gName]</td>
    <td>$row[gDescription]</td>
    <td>$row[gSwfFile]</td>
    <td>$row[gThumb]</td>
  </tr>
";
    }

    $content .= "</table><br>\n";
    return $content;
}

// You can
echo GetInfo();
?>

 

PhREEEk

Link to comment
Share on other sites

Ah, we have a scope issue... lol...

 

<?php
include("include/session.php");

$game = $_GET['game'];

function GetInfo($game){
    global $database;
    $q = "SELECT * FROM " . Games . "
          WHERE gName = '$game'
          ORDER BY `gName` ASC
         ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if( $num_rows == 0 ){
      return 'Database table empty';
    }
    /* Display table contents */
    $content = "<table align=\"left\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">
  <tr>
    <td><b>Game Name</b></td>
    <td><b>Description</b></td>
    <td><b>Swf File</b></td>
    <td><b>Thumb File</b></td>
  </tr>
";
    while( $row = mysql_fetch_assoc($result) ) {
        $content .= "<tr>
    <td>$row[gName]</td>
    <td>$row[gDescription]</td>
    <td>$row[gSwfFile]</td>
    <td>$row[gThumb]</td>
  </tr>
";
    }

    $content .= "</table><br>\n";
    return $content;
}

// You can
echo GetInfo($game);
?>

 

PhREEEk

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.