473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help translating fread to substr

Dear All

Im having some dificulty here:

I found a great PHP code by Catalin Mihaila that reads a SRC (Sinclair
Spectrum $SCREEN Image Format) and tranforms it into PNG format to show
onscreen by the PHP gd2 Library.

The problem is that im trying to translate the 'fread' structure into a
MySQL blob reading, as follows:

Original file access code:

$zxscr = fread($handle, 6144); // first 6144 bytes of file
$attrs = fread($handle, 768); // last 768 bytes of file

My actual MySQL access code:

$zxscr = substr($row[Image], 0, 6144); // first 6144 bytes
of MySQL field "Imagem" inside table "Perguntas"
$attrs = substr($row[Image], -768); // last 768 bytes of
MySQL field "Imagem" inside table "Perguntas"

Here's the original full CODE (for reading the file from servers directory):

$filename=$_GET['scrimg'];
if(!$filename) error("Invalid filename");
$handle = fopen($filename, "r");
if(filesize($filename)<>6912) { fclose($handle); error("Size
of file must be 6912 bytes"); }
$zxscr = fread($handle, 6144); // first 6144 bytes of file
$attrs = fread($handle, 768); // last 768 bytes of file
fclose($handle);

And here's what i changed it into (for reading from a blob field inside
MySQL db):

mysql_connect("localhost", "user", "password") or die("Could
not connect: " . mysql_error());
mysql_select_db("jb-ntg");
$result = mysql_query("SELECT Numero, Nome, Imagem FROM
Perguntas WHERE Numero = jogo");
$zxscr = substr($row[Image], 0, 6144); // first 6144 bytes
of MySQL field "Imagem" inside table "Perguntas"
$attrs = substr($row[Image], -768); // last 768 bytes of
MySQL field "Imagem" inside table "Perguntas"

Here's the structure of the DB "jb-ntg"

CREATE TABLE `perguntas` (
`Numero` int(11) NOT NULL auto_increment,
`Nome` mediumtext NOT NULL,
`Imagem` blob NOT NULL,
`Acessos` int(11) NOT NULL default '0',
`Certas` int(11) NOT NULL default '0',
PRIMARY KEY (`Numero`)
) TYPE=MyISAM AUTO_INCREMENT=3 ;

But i get a blank image (with the right dimensions, so the script must be
ok) the only problem i think is the fact that in the original code i could
get the first 6144 bytes and last 768 bytes of the file, but using the MySQL
blob i apparently cant at least using the substr, and i sincerely have no
idea of the binary equivalent of the fread for blob files.

Anyone can help?

I never coded in PHP and this is my first try at this new language, as you
should remember im trying to make a "Name the Game" PHP game devoted to ZX
Spectrum, and i will, i just need a few guiding lines over here and there.

Thanks for any support in advance,
RootShell


Jul 17 '05 #1
2 2460
RootShell wrote:

(snipped)
And here's what i changed it into (for reading from a blob field inside
MySQL db):

mysql_connect("localhost", "user", "password") or die("Could
not connect: " . mysql_error());
mysql_select_db("jb-ntg");
$result = mysql_query("SELECT Numero, Nome, Imagem FROM
Perguntas WHERE Numero = jogo");

$result is a resource "pointing" to a set of rows.
You're not using this resource in your code.

You need to do

$row = mysql_fetch_assoc($result);

to fill $row with data from the database.

$zxscr = substr($row[Image], 0, 6144); // first 6144 bytes
of MySQL field "Imagem" inside table "Perguntas"

$row[Image] (probably you mean $row["Imagem"], right?) has not been
filled with data from the database;
so now the value of $zxscr is the empty string.

$attrs = substr($row[Image], -768); // last 768 bytes of
MySQL field "Imagem" inside table "Perguntas"

$attrs = '';

Here's the structure of the DB "jb-ntg" (snipped) But i get a blank image (with the right dimensions, so the script must be
ok)

Maybe the right dimensions come from your HTML tags?
<img src="whatever" width="80" height="24" alt="ZX Spectrum screen shot"/>
the only problem i think is the fact that in the original code i could
get the first 6144 bytes and last 768 bytes of the file, but using the MySQL
blob i apparently cant at least using the substr, and i sincerely have no
idea of the binary equivalent of the fread for blob files.

Anyone can help?

I never coded in PHP and this is my first try at this new language, as you
should remember im trying to make a "Name the Game" PHP game devoted to ZX
Spectrum, and i will, i just need a few guiding lines over here and there.


Bem vindo ao PHP e boa-noite :)

[Translation: welcome to PHP and good night :)]

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2
"RootShell" <ro*******@netcabo.pt> wrote in message news:<40**********************@news.telepac.pt>...
Dear All

Im having some dificulty here:

I found a great PHP code by Catalin Mihaila that reads a SRC (Sinclair
Spectrum $SCREEN Image Format) and tranforms it into PNG format to show
onscreen by the PHP gd2 Library.

The problem is that im trying to translate the 'fread' structure into a
MySQL blob reading, as follows:


<snip>

Not a direct answer to your question...

If you want to read the header info from the file use unpack()
<http://in2.php.net/unpack>

A good example is found at
<http://fooassociates.com/phpfer/html/rn45re878.html> and it was
suggested as a nice workaround for "struct" (C's) by Andy and Chung.
--
| Just another PHP saint |
Email: rrjanbiah-at-Y!com
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Joshua Beall | last post by:
Hi All, Any thoughts on the easiest way to translate a MySQL timestamp (which looks like 20040422090941) to the datetime format (which looks like 2004-04-22 09:09:41). This is just to make it...
0
by: Baby Blue | last post by:
I have 2 code like below to grab a news website for my site. However, when I click some links (such as : http://wwww.vnexpress.net/xxx/xxxx ) inside the site which I want to grab, it has some...
3
by: Baby Blue | last post by:
I have 2 codes below to grap data from another site. I use them to get the data from one News site. However, when I click on some link inside (such as :...
1
by: jase_dukerider | last post by:
Hi I have an assignment to hand in shortly for which I am after some guidance. The task is to read a WAV file, request a fade in /out time for the track from the user and the do the fade by...
1
by: noodle_snacks | last post by:
I am trying to use the sample of code posted by thodge at ipswich dot qld dot gov dot au found here: http://au2.php.net/pdf In order to convert a PDF file to a string. I am currently trying...
6
by: Karl Groves | last post by:
I'm trying to work out a mail system which can send an attachment as well as an HTML formatted message (and a default plain text version). I found some pretty good code on PHP.net and modified it...
2
by: gustavoprogrammer | last post by:
Hi you guys! How are? I'm new at the forum and know you could help me!! I Have a C program that works with file...it's so simple, but i need help with deletion of some data in a file(.dat). This...
2
by: vj | last post by:
I have a perl script which connect to network stream using sockets. The scripts first logins in to the server and then parses the data comming from the socket. Statement 1: my $today =...
5
by: angelyn | last post by:
i'm having problem with these matter.. can you please give me exact translated code of this to java... <?php if ($_GET == "atr") { $handle =...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.