Results 1 to 15 of 15

Thread: Php Help!

  1. #1

    Question Php Help!

    Hi guys,
    Can anyone tell me why does my code never print out the correct value? Instead, it always print an empty string?

    mysql_connect($host,$user,$pass) or die(mysql_error());
    mysql_select_db($db) or die(mysql_error());

    $query = "SELECT * FROM eletterlist WHERE md5='$md5'";
    $check = mysql_query($query)or die(mysql_error());
    while ($row = mysql_fetch_array($check) {
    echo "row: ".$row['md5'];
    }

    I am 100% my DB has the record.

    Second question is, when php insert a record to mysql, do they add it to the end of table? or beginning of table?

    THANKS,
    ViscoLink Hosting
    24x7 Support
    30 Days Money Back Guarantee
    www.viscolink.com

  2. #2
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    958
    PHP Code:
    while ($row mysql_fetch_assoc($check) { 
    You could try returning an associative array.

  3. #3
    Join Date
    Feb 2005
    Posts
    105
    Insert is done at the end of the table.

  4. #4
    Quote Originally Posted by Adam-AEC
    PHP Code:
    while ($row mysql_fetch_assoc($check) { 
    You could try returning an associative array.

    What does the function mean?

  5. #5
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    958
    It returns an associative array instead of a numeric array.

    See php docs. http://www.php.net/mysql_fetch_assoc

    Looking at the docs it seems mysql_fetch_array returns both anyways, so my suggestion isn't going to solve your problem.

  6. #6
    even i have changed the to mysql_fetch_assoc($check), it still doesn't print anything????? How can I fix it?

    THANKS,

  7. #7
    Maybe your db returns 0 rows. Try echoing $md5 to see what's its value. Also, use

    PHP Code:

    if(mysql_num_rows())
    {
        while(
    $row mysql_fetch_array.....)
    }
    else
    {
        echo 
    'query did not return any result';


  8. #8
    Join Date
    Dec 2005
    Posts
    326
    instead of:

    echo "row: ".$row['md5'];

    try
    echo "row: $row[md5]";

    but also try using mysql_fetch_array instead of fetch_associate

  9. #9
    It WORKED!!! Thank you all very much. I appreciate your time.


    But then I have another question. How come sometimes when I insert my data to my db, the data sometimes insert from the top and sometimes insert from the bottom of the table? How can I fix this?

  10. #10
    Join Date
    Dec 2005
    Posts
    326
    I don't understand your question..?

  11. #11
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    But then I have another question. How come sometimes when I insert my data to my db, the data sometimes insert from the top and sometimes insert from the bottom of the table? How can I fix this?
    There is no guranteed order of data in a table. It is not that if you enter records, they are appended to the bottom of the table; in fact, you should not program your application such that it depends on the position of records in a table (example: your program relies on a position of a record relative to another -- ie, record a is behind record b) instead, use primary keys and SQL to retrieve results in the order that you want.
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  12. #12
    Quote Originally Posted by fyrestrtr
    There is no guranteed order of data in a table. It is not that if you enter records, they are appended to the bottom of the table; in fact, you should not program your application such that it depends on the position of records in a table (example: your program relies on a position of a record relative to another -- ie, record a is behind record b) instead, use primary keys and SQL to retrieve results in the order that you want.

    OK, now I set my table to have a primary field. what should the $query statment be? The reason why I am asking is because when I do

    mysql_query("INSERT INTO latest_news(id,date, context, author) VALUES ('','$date','$context','$author')") or die(mysql_error());

    I have no value in the id field(which is the primary key field). I thought this value will auto increase by itself?

    Thanks,

  13. #13
    You could set the field Id to auto_acentant? something like that and just do

    mysql_query("INSERT INTO latest_news(date, context, author) VALUES ('','$date','$context','$author')");

    or you could do

    mysql_query("INSERT INTO latest_news(id,date, context, author) VALUES ('null','$date','$context','$author')");

    I think either way, u need to set that primary field to auto_accentent, again i forgot exactly what it's called but you'll know it when u see it.

  14. #14
    sorry, the first example should be like

    mysql_query("INSERT INTO latest_news(date, context, author) VALUES ('$date','$context','$author')");

  15. #15
    Quote Originally Posted by hostingen
    OK, now I set my table to have a primary field. what should the $query statment be? The reason why I am asking is because when I do

    mysql_query("INSERT INTO latest_news(id,date, context, author) VALUES ('','$date','$context','$author')") or die(mysql_error());

    I have no value in the id field(which is the primary key field). I thought this value will auto increase by itself?

    Thanks,
    Add auto_increment to primary key and that's it. You're obviously missing it.

Posting Permissions

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