Connecting Tech Pros Worldwide Forums | Help | Site Map

MySQL Connection and loop

Member
 
Join Date: May 2007
Posts: 40
#1: Aug 23 '07
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.  

jx2 jx2 is offline
Familiar Sight
 
Join Date: Feb 2007
Location: Bristol UK
Posts: 227
#2: Aug 23 '07

re: MySQL Connection and loop


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]

...
Member
 
Join Date: May 2007
Posts: 40
#3: Aug 23 '07

re: MySQL Connection and loop


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
jx2 jx2 is offline
Familiar Sight
 
Join Date: Feb 2007
Location: Bristol UK
Posts: 227
#4: Aug 23 '07

re: MySQL Connection and loop


Quote:

Originally Posted by tommurray

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
Member
 
Join Date: May 2007
Posts: 40
#5: Aug 23 '07

re: MySQL Connection and loop


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]
jx2 jx2 is offline
Familiar Sight
 
Join Date: Feb 2007
Location: Bristol UK
Posts: 227
#6: Aug 23 '07

re: MySQL Connection and loop


Quote:

Originally Posted by tommurray

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
Member
 
Join Date: May 2007
Posts: 40
#7: Aug 23 '07

re: MySQL Connection and loop


Quote:

Originally Posted by jx2

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.
jx2 jx2 is offline
Familiar Sight
 
Join Date: Feb 2007
Location: Bristol UK
Posts: 227
#8: Aug 23 '07

re: MySQL Connection and loop


Quote:

Originally Posted by tommurray

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
jx2 jx2 is offline
Familiar Sight
 
Join Date: Feb 2007
Location: Bristol UK
Posts: 227
#9: Aug 23 '07

re: MySQL Connection and loop


[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
Member
 
Join Date: May 2007
Posts: 40
#10: Aug 23 '07

re: MySQL Connection and loop


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]
Member
 
Join Date: May 2007
Posts: 40
#11: Aug 23 '07

re: MySQL Connection and loop


Quote:

Originally Posted by jx2

[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.
Member
 
Join Date: May 2007
Posts: 40
#12: Aug 23 '07

re: MySQL Connection and loop


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
jx2 jx2 is offline
Familiar Sight
 
Join Date: Feb 2007
Location: Bristol UK
Posts: 227
#13: Aug 23 '07

re: MySQL Connection and loop


Quote:
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
Member
 
Join Date: May 2007
Posts: 40
#14: Aug 24 '07

re: MySQL Connection and loop


Which Part is not encouraged an what way to do it would be encouraged?

Tom
Reply