blueman378 Posted December 3, 2007 Share Posted December 3, 2007 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: thanks 4 ur help Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 3, 2007 Share Posted December 3, 2007 SELECT * FROM 'games.games' should be SELECT * FROM `games.games` Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 3, 2007 Author Share Posted December 3, 2007 thanks but i still get the exact same error, any more ideas? Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 3, 2007 Share Posted December 3, 2007 $q is not your query.. so $result = $database->query($q) or die("Error: " . mysql_error()); to $result = $database->query($sql) or die("Error: " . mysql_error()); PhREEEk Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 3, 2007 Author Share Posted December 3, 2007 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 Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 3, 2007 Author Share Posted December 3, 2007 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 Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 3, 2007 Share Posted December 3, 2007 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 Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 3, 2007 Share Posted December 3, 2007 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 Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 3, 2007 Author Share Posted December 3, 2007 <?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, Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 3, 2007 Share Posted December 3, 2007 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 Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 3, 2007 Author Share Posted December 3, 2007 <?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 Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 3, 2007 Share Posted December 3, 2007 You can preg_replace anything you need to, but first off, where are you trying to do this? Show me an example... PhREEEk Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 3, 2007 Author Share Posted December 3, 2007 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 Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 3, 2007 Share Posted December 3, 2007 Can we see the form you are using to get processed by showgame.php? PhREEEk Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 3, 2007 Author Share Posted December 3, 2007 hmm i guess this must be where im going wrong, at the moment im simply using a link not a form Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 3, 2007 Share Posted December 3, 2007 A link will work in a pinch, but you will need to retrieve that with $_GET instead of $_POST PhREEEk Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 3, 2007 Author Share Posted December 3, 2007 tried get and still get the same problem Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 3, 2007 Share Posted December 3, 2007 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 Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 3, 2007 Author Share Posted December 3, 2007 sorry (its 3 in the morn) i dnt quite get what u mean, sut save it and run it as test.php?game=3D Space Skimmer ? Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 3, 2007 Author Share Posted December 3, 2007 i get Array ( [game] => 3D Space Skimmer ) Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 3, 2007 Share Posted December 3, 2007 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 Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 3, 2007 Author Share Posted December 3, 2007 SQL: SELECT * FROM Games WHERE gName = '' ORDER BY `gName` ASC so this isnt reading the $game variable? Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 3, 2007 Share Posted December 3, 2007 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 Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 3, 2007 Author Share Posted December 3, 2007 wait so all the problem was was that the $game variable was not being included in that function? Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 3, 2007 Share Posted December 3, 2007 Functions have their own set of variables. If we want a variable outside of the function to be made available to the function, we can add it directly, or by using a global statement within the function. Inside the function, $game didn't exist at all... PhREEEk Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.