Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2011
    Posts
    79
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Need help inserting rows/checking if they are there

    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:
    PHP Code:
    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: "
    PHP Code:
    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:
    PHP Code:
    $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.
    Last edited by xFinaLx; 07-01-2011 at 10:28 PM.
    Quote Originally Posted by bullant View Post
    Basically - Java is to Javascript as Ham is to Hampster.

  • #2
    Regular Coder
    Join Date
    Jun 2011
    Posts
    103
    Thanks
    0
    Thanked 13 Times in 13 Posts
    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..

    PHP Code:
    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"

  • #3
    New Coder
    Join Date
    May 2011
    Posts
    79
    Thanks
    3
    Thanked 4 Times in 4 Posts
    "There are 0 rows in this table"

    So I'm assuming there is a problem with how I'm inputting the data?
    Quote Originally Posted by bullant View Post
    Basically - Java is to Javascript as Ham is to Hampster.

  • #4
    Regular Coder
    Join Date
    Jun 2011
    Posts
    103
    Thanks
    0
    Thanked 13 Times in 13 Posts
    yep, lets try writing it a different way.

    PHP Code:
    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

  • Users who have thanked Fugix for this post:

    xFinaLx (07-01-2011)

  • #5
    New Coder
    Join Date
    May 2011
    Posts
    79
    Thanks
    3
    Thanked 4 Times in 4 Posts
    thanks bro that did the trick!!!


    "Email: test@test.com
    IP: 123456
    ArrayThere are 1 rows in this table"
    Last edited by xFinaLx; 07-01-2011 at 10:12 PM.
    Quote Originally Posted by bullant View Post
    Basically - Java is to Javascript as Ham is to Hampster.

  • #6
    Regular Coder
    Join Date
    Jun 2011
    Posts
    103
    Thanks
    0
    Thanked 13 Times in 13 Posts
    glad I could be of assistance...
    please mark as resolved


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •