homepage Welcome to WebmasterWorld Guest from 54.197.110.151
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

    
Requesting help with inserting and reading TEXT data type in MySQL
R8kit



 
Msg#: 4379360 posted 10:17 pm on Oct 25, 2011 (gmt 0)

Hello everyone,

I am new to MySQL and I am trying to insert the data type TEXT into the database but it isn't working out for me.

I created an SQL file with the following code:


CREATE DATABASE books;

USE books;

CREATE TABLE authors (
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR (255),
info TEXT
);

INSERT INTO authors (name, info) values('Vivek', '
asdfkljasdkl;fjasd
fasdl;kfjaskl;dfjasd
fasdkl;fjasd;lkfjasdf
asdl;fkajsd;lfkjasd;fsd
fkl;asdjfaskl;djfsd
sdakl;jasdl;kfjasdkl;fjasd
asdkl;fjasdl;fkjasl;kfj
');


I created an PHP page with the following code:

<?php
mysql_connect("localhost","root","passwd");

mysql_select_db("books");

$result=mysql_query("SELECT * FROM authors");

$row=mysql_fetch_array($result);

while ($row=mysql_fetch_array($result)) {
echo 'Name: '.$row{'name'}.'<br />';
echo 'Info: '.$row{'info'}.'<br />';
}
?>


Can anyone help me insert and read the TEXT data type the proper way?

Thanks in advance.

 

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4379360 posted 8:35 am on Oct 26, 2011 (gmt 0)

Is it not working? Any errors?

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4379360 posted 4:36 pm on Oct 26, 2011 (gmt 0)

That should work (right?) Error trapping is your friend:

$result=mysql_query("SELECT * FROM authors") or die("Cannot select authors: " . mysql_error());

Should be unrelated, but you can delete the first "$row" set:

// You're doing the same below $row=mysql_fetch_array($result);

while ($row=mysql_fetch_array($result)) {

You could also try $row['info'], the typical array referencing bracket. The curly braces are used for complex curly syntax [php.net] (about halfway down) and more often the application is to establish context, that is, if a variable can be interpreted in various ways, the brackets clarify it to the parser. Can't recall exact samples right now, but it is often helpful in dereferencing complex arrays of arrays.

edit:, see, this is why I post here, reminds me of stuff I'd forgotten. :-) One use of curly syntax is to define the end of a variable where it might otherwise interpret it as undefined. Rather than

echo $item->price . '00';

or this, in which case it **might** expect the variable $price00, which would be null,

$price = $item->price;
echo "$price00";

You use curlies to explicitly define the context and end the variable:

echo "{$item->price}00";

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4379360 posted 5:09 pm on Oct 26, 2011 (gmt 0)

Should be unrelated, but you can delete the first "$row" set:
// You're doing the same below $row=mysql_fetch_array($result);
while ($row=mysql_fetch_array($result)) {


I think that's it (well spotted @rocknbil) - the first call to mysql_fetch_array() returns the first row from the result-set and moves the internal data pointer ahead. The second call, in the while() loop, then attempts to get the second row, but there is none (unless you've run the INSERT a few times) and fails.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved