473,405 Members | 2,262 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,405 software developers and data experts.

MySQL Connection and loop

Hi all I am new to php and i need a little help, I have a webpage which is going to draw all the content from a database were the user can login and amend the values of the field.

Within the database I have two fields id and content. For each row I want an variable such as pageContent1 for the second row pageContent2 and so on.

I have wrote the below code but this does not work can anyone help?

Thank You

Tom

Expand|Select|Wrap|Line Numbers
  1. // Make a MySQL Connection
  2. mysql_connect("###", "###", "###") or die(mysql_error());
  3. mysql_select_db("cbags") or die(mysql_error());
  4.  
  5. // Get all the data from the "example" table
  6. $result = mysql_query("SELECT * FROM home_content") 
  7. or die(mysql_error());  
  8. $h = 1;
  9. while($row = mysql_fetch_array( $result )) {
  10.         $pageContent[$h] = $row['content'];
  11.        $h = $h + 1;
  12.        }
  13.  
Aug 23 '07 #1
13 2539
jx2
228 100+
i dont get it - is there anything wrong with your code?

[php]
// Make a MySQL Connection
mysql_connect("###", "###", "###") or die(mysql_error());
mysql_select_db("cbags") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM home_content")
or die(mysql_error());

//well you can try this way (but it wont change anythinhg)
$pageContent[0]=0;
while($pageContent[] = mysql_fetch_array( $result )) ;
}
[/php]

...
Aug 23 '07 #2
Hi thank you for getting back so quickly, in firefox my screen goes blank when uploading to my site. If I take out the code then everything loads up correctly.

I will give what you have mentioned ago, with what you have wrote how does the pageContent1 get the content of the field in the database with an id of 1?

Thank you

Tom
Aug 23 '07 #3
jx2
228 100+
I will give what you have mentioned ago, with what you have wrote how does the pageContent1 get the content of the field in the database with an id of 1?
eighter of them cos you used fetch_array()
if you want to display results you can use
[php]
$row = implode(' , ',$pageContent[1]) );
echo $row;
[/php]
that will display row 1
Aug 23 '07 #4
No luck still nothing gets displayed none of the design of the page displays just a blank screen.

my original code works fine without trying to get the value pageContent to increment each time it goes through the loop.(see below)

[php]
// Make a MySQL Connection
mysql_connect("mysql", "webuser", "pa55w0rd") or die(mysql_error());
mysql_select_db("cbags") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT content FROM home_content WHERE id = 1")
or die(mysql_error());

while($row = mysql_fetch_array( $result )) {
$pageContent = $row['content'];
}
[/php]
Aug 23 '07 #5
jx2
228 100+
No luck still nothing gets displayed none of the design of the page displays just a blank screen.

my original code works fine without trying to get the value pageContent to increment each time it goes through the loop.(see below)

Expand|Select|Wrap|Line Numbers
  1. // Make a MySQL Connection
  2. mysql_connect("mysql", "webuser", "pa55w0rd") or die(mysql_error());
  3. mysql_select_db("cbags") or die(mysql_error());
  4.  
  5. // Get all the data from the "example" table
  6. $result = mysql_query("SELECT content FROM home_content WHERE id = 1") 
  7. or die(mysql_error());  
  8.  
  9. while($row = mysql_fetch_array( $result )) {
  10.     $pageContent = $row['content'];
  11.  
as i wrote above you need to use echo
Aug 23 '07 #6
as i wrote above you need to use echo
I have used echo on the example you provided but it did not work nothing was displayed.
Aug 23 '07 #7
jx2
228 100+
I have used echo on the example you provided but it did not work nothing was displayed.
try to use
[php]
echo mysql_error();
[/php]
you might have error in your sql query
or you display wrong variable
Aug 23 '07 #8
jx2
228 100+
[php]
mysql_connect("mysql", "webuser", "pa55w0rd") or die(mysql_error());
mysql_select_db("cbags") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT content FROM home_content WHERE id = 1")
or die(mysql_error());

while($row = mysql_fetch_array( $result )) {
foreach($row as $v){
echo "$v , ";
}
echo "\n<br />";

}
echo mysql_error();
[/php]

try this one
Aug 23 '07 #9
I have added in the echo for the mysql error, but nothing is displayed still.

BTW thank you for your help

I now have the script as:

[php]// Make a MySQL Connection
mysql_connect("mysql", "webuser", "pa55w0rd") or die(mysql_error());
mysql_select_db("cbags") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM home_content")
or die(mysql_error());

$pageContent[0]=0;
while($pageContent[] = mysql_fetch_array( $result )) ;
$row = implode(' , ',$pageContent[1]) );
echo $row;
echo mysql_error();
}[/php]
Aug 23 '07 #10
[php]
mysql_connect("mysql", "webuser", "pa55w0rd") or die(mysql_error());
mysql_select_db("cbags") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT content FROM home_content WHERE id = 1")
or die(mysql_error());

while($row = mysql_fetch_array( $result )) {
foreach($row as $v){
echo "$v , ";
}
echo "\n<br />";

}
echo mysql_error();
[/php]

try this one
Progress I know get the page loading up with the following line:

1 , 1 , Next Event , Next Event ,

so it is writing out two id's and two of the content field for the one row.
Aug 23 '07 #11
Thank you for your help I have solved my problem by changing to the following:
[PHP]

while($row = mysql_fetch_array( $result )) {
foreach($row as $v){
$pageContent[$v] = $row['content'];
}
}

[/PHP]

Then Further down in my page i have

[PHP]<?php echo $pageContent[1]; ?>[/PHP]

replacing the 1 to a 2 for the second row of content.

Regards

Tom
Aug 23 '07 #12
jx2
228 100+
1 , 1 , Next Event , Next Event ,
well you neet to use mysql_fetch_assoc() or mysql_fetch_row() if you want to avoid this problem

what you did is actualy bad and i woudnt encurage you do do that

regards
jx2
Aug 23 '07 #13
Which Part is not encouraged an what way to do it would be encouraged?

Tom
Aug 24 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dariusz | last post by:
I am a beginner in PHP and MySQL, and am working through a book and various online tutorials on PHP and MySQL and now stuck - installed everything on "localhost" and it all works fine. My question...
5
by: Phil Powell | last post by:
I've read some online resources that utilize various MySQL command-line actions to migrate data from Access to MySQL. The situation is this: a group of co-workers of mine will be using an Access...
5
by: Stuart Mueller | last post by:
I have a mySQL database, called clients it has a single table called client with 2325 records in it. It was originally an Access database but I have exported it to mySQL. I have created a system...
11
by: DJJ | last post by:
I am using the MySQL ODBC 3.51 driver to link three relatively small MySQL tables to a Microsoft Access 2003 database. I am finding that the data from the MySQL tables takes a hell of a long time...
3
by: tulika dutta | last post by:
after 8 hrs my MySQL connection with JDBC gets time out. It gives the error java.sql.SQLException: No operations allowed after connection closed.Connection was implicitly closed due to underlying...
4
by: mramsay | last post by:
Hi, I'm having a real problem creating a dynamic hyperlink for my website. I want to pull the field name from mysql table. Field name is description. I would like this to be a hyperlink on my...
5
by: mramsay | last post by:
HI, I'm having alot of problems trying to figure out how to create drop down links from a hyperlink on my homepage. I have a hyperlink called Programs (this is for a community centre) When...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
4
by: theschaef | last post by:
Hello, Sorry in advance if this is a trivial problem. I have a script which is executed about 2000 times in a row, and requires a connection to the mysql database each time it is executed. I am...
1
ssnaik84
by: ssnaik84 | last post by:
Hi Guys, Last year I got a chance to work with R&D team, which was working on DB scripts conversion.. Though there is migration tool available, it converts only tables and constraints.. Rest of...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.