Jump to content

Echo swf file from mysql DB


GFOnyX
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi.

 

I am having trouble embedding a swf file from a database on a php page. I know how to embed a swf file using just php with the help of swfobject.js but I am having trouble being able to actually show the file from a db. I am not storing the actual file on the database but the path to the flash file (eg games/flashgame1.swf)

 

I am properly connected to the db so there is no problem there. The table is called games and has two columns for now:     id | path

 

 

Here is my code:

 
<?php
require_once('config.php');
if (!session_id()) session_start();
?>

<!DOCTYPE html>
<html>
<head> 
<meta content="text/html; charset=UTF-8" http-equiv="content-type"> <meta name="author" content="Onyx"> <link rel="stylesheet" type="text/css" href="<?php echo HTTP_STYLE;?>layout.css" /> <link href="<?php echo HTTP_IMAGES;?>favicon.ico" 
rel="icon" type="image/x-icon" />

</head>
<body>

<div  id="wrapper2">   
<div id="container">

<?php $result = mysql_query("SELECT * FROM games");
while($row = mysql_fetch_array($result))         
	{            
	$path = $row['1'];
	$id = $row['0'];
	$width = "546";
	$height = "431";
	$version = "9.0.0";          }

echo "<script type=text/javascript src=swfobject.js></script>";
echo "<script type=text/javascript>";
echo "swfobject.embedSWF('$path','$id', '$width', '$height', '$version')";echo "</script>"; 
?>
 
</div>
</div>
</body>
</html>

Any ideas what i am doing wrong?

Link to comment
Share on other sites

your code for getting the path out of the database looks fine to me. What is the following line outputting?

echo "swfobject.embedSWF('$path','$id', '$width', '$height', '$version')";echo "</script>"; 

Is this outputting the correct JavaScript syntax? if it is you need to make sure $path is pointing to correct location of where your .swf files are located.

Link to comment
Share on other sites

The page does not output anything currently besides some css styling (footer,header etc)

 

 I created another column called image and i can properly echo out the image so i am connected to the DB and the path to the swf file is correct. I have checked many times in case it was that. 

Link to comment
Share on other sites

I changed mysql_fetch_array to mysql_fetch_assoc in case it makes a difference but nothing changed.The  error is as following

 

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in  the line that has while($row = mysql_fetch_assoc($result))  

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in  the line that has while($row = mysql_fetch_array($result))

Link to comment
Share on other sites

That error usually indicates your query is failing due to an error or is returning an empty result.

 

To see if there is an error from your query. Chang the mysql_query line to

$result = mysql_query("SELECT * FROM games") or die('Query Error: ', mysql_error());

This should print out an error if the query is failing.

Edited by Ch0cu3r
Link to comment
Share on other sites

I am getting

 

Parse error: syntax error, unexpected ',' in C:\xampp\htdocs\kinderlearn\game2.php on line 27  

 

something wrong with the syntax of the above line.

 

The query runs fine if i execute it on MySQL via PHPmyadmin. It returns the correct results. I don't see how it can be wrong.It is like the simplest query :)

Edited by GFOnyX
Link to comment
Share on other sites

Sorry Ch0cu3r i am stupid. When i was executing your requests i was hiting the link as http://localhost/project/game2.php which of course would return an error since i am calling every page from index.php which contains the dbconnect file. When i saw the image i had the link as  http://localhost/project/index.php?page=game2.php which is correct. Sorry for the trouble.

 

The problem however remains. No error messages but still can not see the swf file...

 

 

Here is my latest code

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
require_once('config.php');

if (!session_id()) session_start();
?>

<!DOCTYPE html>
<html>
<head> 
<meta content="text/html; charset=UTF-8" http-equiv="content-type"> 
<meta name="author" content="Onyx"> 
<link rel="stylesheet" type="text/css" href="<?php echo HTTP_STYLE;?>layout.css" /> 
<link href="<?php echo HTTP_IMAGES;?>favicon.ico" rel="icon" type="image/x-icon" />

</head>

<body>
<div id="wrapper2">   
<div id="container">

<?php
$path="";
$id="";
$width="";
$height="";
$version="";
$image="";

$result = mysql_query("SELECT * FROM games") or die('Query Error: '. mysql_error());

while ($row = mysql_fetch_assoc($result))         
{            
$path = $row['path'];   
$id = $row['id'];   
$width = "546";   
$height = "431";   
$version = "9.0.0";   
$image = $row['image'];        
}
echo "<script src='swfobject.js'></script>";
echo "swfobject.embedSWF(" . $path . ", " . $id . ", " . $width . ", " . $height . ", " . $version . ")";
echo "<img src=$image>";
?>
 </div>
</div>
</body>
</html>

The above outputs:

swfobject.embedSWF(Start_Quiz_App.swf, 1, 546, 431, 9.0.0)  and the image i have.

Edited by GFOnyX
Link to comment
Share on other sites

Ok so your code PHP is working correctly as it a is outputting the Javascript code within your while loop. The next problem to tackle then is making sure your Javasript does not have any errors

 

If you are using the latest version of IE, Firefox, Chrome etc you should to access the Developer Tools pain by pressing F12. Go to the console tab and refresh your page (using F5). Does the console log any errors?

Link to comment
Share on other sites


echo "<script src='swfobject.js'></script>";
echo "swfobject.embedSWF(" . $path . ", " . $id . ", " . $width . ", " . $height . ", " . $version . ")";
echo "<img src=$image>";
This isn't going to output correct javascript syntax. Firstly you didn't wrap the js in script tags. 2nd, you didn't wrap your values in quotes. The number arguments might work without them but the path certainly won't.
Link to comment
Share on other sites

These are the errors i am getting.I should say that one the same site on a different page i can display swf files just fine but they are not stored in the DB.I call them manually. It is when i try to call them from the DB that they do not display.

 

HTML1502: Unexpected DOCTYPE. Only one DOCTYPE is allowed and it must occur before any elements.
index.php, line 64 character 1

HTML1503: Unexpected start tag.
index.php, line 65 character 1

HTML1512: Unmatched end tag.
index.php, line 70 character 1

HTML1514: Extra "<body>" tag found. Only one "<body>" tag should exist per document.
index.php, line 72 character 1

HTML1521: Unexpected "</body>" or end of file. All open elements should be closed before the end of the document.
index.php, line 81 character 1

FB.getLoginStatus() called before calling FB.init().

Link to comment
Share on other sites

okay so Ch0cu3r just had you do that so you could verify the output. You need to change it back to correct javascript syntax and then see if you get any javascript errors. Change it back to how you had it originally:

 

echo "<script type='text/javascript' src='swfobject.js'></script>";
echo "<script type='text/javascript'>";
echo "swfobject.embedSWF('$path','$id', '$width', '$height', '$version')";echo "</script>"; 
Then look to see if the js console is giving any errors.

 

Also, it looks like your $path is just outputting "Start_Quiz_App.swf" is your .swf file really located the same directory as the script?

Link to comment
Share on other sites

First of all,thank you all for helping me.  :)

 

I replaced the code as Josh said but now i get no output at all at the page.

I went to firefox and checked the JS under console and shows these two things. I don't know if these are what you mean...

[18:57:32.342] Use of getUserData() or setUserData() is deprecated.  Use 
WeakMap or element.dataset instead. @ resource://gre/modules/XPIProvider.jsm 
-> 
jar:file:///C:/Users/Alexander/AppData/Roaming/Mozilla/Firefox/Profiles/m5buocf4.default/extensions/%7Bd10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d%7D.xpi!/bootstrap.js 
-> 
jar:file:///C:/Users/Alexander/AppData/Roaming/Mozilla/Firefox/Profiles/m5buocf4.default/extensions/%7Bd10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d%7D.xpi!/lib/requestNotifier.js:64[18:57:32.497] 
Use of getPreventDefault() is deprecated.  Use defaultPrevented instead. @ 
chrome://flashblock/content/flashblock.xml:119

Yes the swf file is located in the same dir as the script which is the root of the site.

Edited by GFOnyX
Link to comment
Share on other sites

  • Solution

You are going to http://localhost/ to run your php scripts. You can't directly access php scripts from file:// protocol.

Those errors are coming from a browser extension you have installed called flashblock. This could be interfering, try disabling it.

 

 

Any way ou may also want to read this part of the documentation

https://code.google.com/p/swfobject/wiki/documentation#STEP_3:_Embed_your_SWF_with (I assume that is the js library you're using)

 

The id (secound) parameter for swfobject needs to be the name of the html element where you want the swf file to be displayed. Having just that line of javascript isn't going to display the swf file. You may need to add this line to your while loop

echo '<div id="'.$id.'"><p>Alternative content</p></div>';
Edited by Ch0cu3r
Link to comment
Share on other sites

 

You are going to http://localhost/ to run your php scripts. You can't directly access php scripts from file:// protocol.

Those errors are coming from a browser extension you have installed called flashblock. This could be interfering, try disabling it.

 

 

Any way ou may also want to read this part of the documentation

https://code.google.com/p/swfobject/wiki/documentation#STEP_3:_Embed_your_SWF_with (I assume that is the js library you're using)

 

The id (secound) parameter for swfobject needs to be the name of the html element where you want the swf file to be displayed. Having just that line of javascript isn't going to display the swf file. You may need to add this line to your while loop

echo '<div id="'.$id.'"><p>Alternative content</p></div>';

 

 

And that was the miracle line that was missing! :happy-04:

I for sure will study the link though.

 

I can not thank you enough for taking the time and helping me out. All of you guys, thank you very much!!!

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.