Your HostICan Community  


Go Back   Your HostICan Community > Programming & Design > PHP / MySQL Assistance

PHP / MySQL Assistance We understand. Not all of you are experienced programmers, but some of you are highly skilled at coding. So we've set aside this forum so you can ask questions, and provide answers to the most commonly asked PHP / MySQL issues.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 11-13-2007, 05:47 PM
familyon familyon is offline
Junior Member
 
Join Date: Nov 2007
Posts: 15
Default Problems with PHP/MySQL

Hi,

I'm having a problem using PHP in conjunction with MySQL. This is what I've done:

1. Created a MySQL database using cPanels
2. Created a table and inserted 3 data records into it using phpmyadmin
3. Wrote a PHP script to print out the data I entered in step #2. It worked with no problem
4. Added on to the end of that script some code to insert one more data record. That appeared to work well until I added more code to the script to print out the newly updated database. This showed that it really didn't get updated.

NOTE: I did the same thing on my godaddy account and the exact same thing happened so I suspect its something very simple I am doing wrong. Here's the code and output when I ran it: I also tried to create a table in a PHP script. It appeared to work well until I went to cPanels only to find the table not there.


//Connect To Database
$hostname='localhost';
$username='familyon_john';
$password='pasword';
$dbname='familyon_blog';
$usertable='your_tablename';
$yourfield = 'your_field';

mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to MySQL! Please try again later.');
echo "Connected to MySQL
";
mysql_select_db($dbname)OR DIE ('Unable to connect to database! Please try again later.');;
echo "Connected to Database
";

// Print out the database
$query = "SELECT * FROM comments";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
echo $row['date']. " - ". $row['name']. " - ". $row['email']. " - ". $row['url']. " - ". $row['comment'];
echo "
"; }

// Attempt to insert something into database
$query="INSERT INTO `familyon_blog`.`comments` (
`day` ,
`name` ,
`email` ,
`url` ,
`comment` )
VALUES (
'2007-11-21', 'Katrina', 'kjlkdf@gmila.com', 'this.com', 'this is a katrina')"or die(mysql_error());
echo "Data Entered!";

// Print out the database
$query = "SELECT * FROM comments";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
echo $row['date']. " - ". $row['name']. " - ". $row['email']. " - ". $row['url']. " - ". $row['comment'];
echo "
"; }
?>


OUTPUT:

Connected to MySQL
Connected to Database
- John Vogel - sdfs@jkl .com - asdfsa@com - This is a test from John
- Nancy Vogel - sathren@t.com - jksldj.com - This is from Nancy
- David Vogel - sldkfj@dsk.com - dlkj.com - This is from David
Data Entered! - John Vogel - sdfs@jkl .com - asdfsa@com - This is a test from John
- Nancy Vogel - sathren@t.com - jksldj.com - This is from Nancy
- David Vogel - sldkfj@dsk.com - dlkj.com - This is from David

Last edited by familyon; 11-13-2007 at 05:50 PM. Reason: code was left out
Reply With Quote
  #2  
Old 11-13-2007, 11:28 PM
lnxcode's Avatar
lnxcode lnxcode is offline
The British Kid
 
Join Date: Sep 2007
Location: Richmond, VA
Posts: 2,012
Send a message via AIM to lnxcode Send a message via Skype™ to lnxcode
Default

I'm not programmer. However, you may want to check that you have the right permissions on the database by going to >> cPanel >> MySQL >> changing the permissions to "All Permissions" then you should be set. Otherwise, you need to look at the code (I don't know PHP, or I'd help).
__________________
Thanks,

Denis Motova
Affiliate / Operations Manager

HostICan Answers | Become a HostICan Affiliate | Create a Support Ticket.
Reply With Quote
  #3  
Old 11-14-2007, 11:14 PM
familyon familyon is offline
Junior Member
 
Join Date: Nov 2007
Posts: 15
Default

Thanks Denis,

I checked the permissions and I had all the possible permissions. I'll keep trying.
Reply With Quote
  #4  
Old 11-24-2007, 01:52 PM
itwasntme's Avatar
itwasntme itwasntme is offline
Senior Member
 
Join Date: Sep 2007
Posts: 142
Default

The problem is that you never ran your INSERT query.

The result is that you didn't insert the record and didn't receive an error message.

You just need to modify your code to run the INSERT query and you should be set. Change this part:
Code:
// Attempt to insert something into database
$query="INSERT INTO `familyon_blog`.`comments` (
`day` ,
`name` ,
`email` ,
`url` ,
`comment` )
VALUES (
'2007-11-21', 'Katrina', 'kjlkdf@gmila.com', 'this.com', 'this is a katrina')"or die(mysql_error());
echo "Data Entered!";
So that it looks like this:
Code:
// Attempt to insert something into database
$query="INSERT INTO `familyon_blog`.`comments` (
`day` ,
`name` ,
`email` ,
`url` ,
`comment` )
VALUES (
'2007-11-21', 'Katrina', 'kjlkdf@gmila.com', 'this.com', 'this is a katrina')";
$result = mysql_query($query) or die(mysql_error()); 
echo "Data Entered!";
I don't mean to be a pain in case you are coding this way by preference, but you can reduce your typing by eliminating all the single quotes except for string values in your SQL statements. So this:
Code:
$query="INSERT INTO `familyon_blog`.`comments` (
`day` ,
`name` ,
`email` ,
`url` ,
`comment` )
VALUES (
'2007-11-21', 'Katrina', 'kjlkdf@gmila.com', 'this.com', 'this is a katrina')"or die(mysql_error());
echo "Data Entered!";
ends up looking like this:
Code:
$query="INSERT INTO comments (
day,
name,
email,
url,
comment)
VALUES (
'2007-11-21', 'Katrina', 'kjlkdf@gmila.com', 'this.com', 'this is a katrina')";
It will save you a lot of typing.
Reply With Quote
  #5  
Old 11-25-2007, 10:33 AM
lnxcode's Avatar
lnxcode lnxcode is offline
The British Kid
 
Join Date: Sep 2007
Location: Richmond, VA
Posts: 2,012
Send a message via AIM to lnxcode Send a message via Skype™ to lnxcode
Default

itwasntme - you're a star! - Hopefully this will resolve that users problem Keep up the excellent work!
__________________
Thanks,

Denis Motova
Affiliate / Operations Manager

HostICan Answers | Become a HostICan Affiliate | Create a Support Ticket.
Reply With Quote
  #6  
Old 11-25-2007, 10:53 AM
itwasntme's Avatar
itwasntme itwasntme is offline
Senior Member
 
Join Date: Sep 2007
Posts: 142
Default

Thanks, Denis. I'm just trying to give back a little for all the help I have received here.
Reply With Quote
Reply

Tags
php or mysql, problems

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Again problems receiving outside list server web based e-mails patthronson Shared Hosting 4 09-08-2008 01:45 PM
Problems Installing JDK aarellano Virtual Private Servers (VPS) 4 08-07-2008 02:05 PM
Subdomain problems sweetsweden Shared Hosting 1 05-10-2008 11:17 AM
Problems with uploaded pages smartecosse Shared Hosting 5 03-08-2008 04:15 PM
Mail problems caused by invalid reverse DNS? bkark Virtual Private Servers (VPS) 33 12-15-2007 01:15 PM


All times are GMT -4. The time now is 03:48 PM.


Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.

Copyright © 2003 - 2008 HostICan. All Rights Reserved.