...

View Full Version : Resolved Need help inserting rows/checking if they are there



xFinaLx
07-01-2011, 08:17 PM
I'm relatively new to MySQL, and I just can't figure something out.

I created a database called newsletter. Inside of newsletter I made a table called 'emails' with columns 'id' (int), 'email' (varchar 40), 'ip' (varchar 40), and 'date' (DATETIME).

I wrote a script to input a test row to this new table:


mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("newsletter") or die(mysql_error());
mysql_query("INSERT INTO emails (email, ip) VALUES('test@test.com','123456')") or die(mysql_error());


This didn't return any errors so I assumed all went well and the row was created.

So I tried to select the row and print out the contents. This didn't work at all. It just returned "Email: IP: "


mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("newsletter") or die(mysql_error());
$result = mysql_query("SELECT * FROM emails") or die(mysql_error());
$row = mysql_fetch_array($result);
echo ("Email: ".$row['email']."<br />");
echo (" IP: ".$row['ip']."<br />");


So after a bit of searching I found some code that should echo 'empty' if the table didn't have any rows. After I put it in, sure enough, it told me the table was empty:


$rows = mysql_result(mysql_query('SELECT COUNT(*) FROM emails'), 0);
if (!$rows) {
echo "empty";
}


So now I'm a bit lost. What am I doing wrong?

Also, I don't have access to PHPmyAdmin or anything like that.

Fugix
07-01-2011, 08:46 PM
since you do not have access to phpmyadmin, im assuming that you cannot manually check your table rows, so let's add code to see if there are any rows at all..


mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("newsletter") or die(mysql_error());
$result = mysql_query("SELECT * FROM emails") or die(mysql_error());
$num_rows = mysql_num_rows($result);
print "There are " . $num_rows . " rows in this table";

xFinaLx
07-01-2011, 08:55 PM
"There are 0 rows in this table"

So I'm assuming there is a problem with how I'm inputting the data?

Fugix
07-01-2011, 09:03 PM
yep, lets try writing it a different way.


mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("newsletter") or die(mysql_error());
$query = mysql_query("INSERT INTO emails VALUES('','test@test.com','123456','')") or die(mysql_error());
print $query;

please post the results

xFinaLx
07-01-2011, 09:09 PM
thanks bro that did the trick!!! :)


"Email: test@test.com
IP: 123456
ArrayThere are 1 rows in this table"

Fugix
07-01-2011, 09:21 PM
glad I could be of assistance...:)
please mark as resolved



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum