#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2014
    Location
    Centurion, South Africa
    Posts
    4
    Rep Power
    0

    mysql_* versus mysqli_*


    I am new to web programming and I have a little problem I do not understand. I have a drop down selection which works fine when using mysql_* but as soon as I replace the mysql_* with mysqli_* it does not work it only populates the selection box with ‘select country’ but nothing comes up. I know my connect and select database part work because I do not go into the ‘or die’ option. Here is my codeing with mysql_* which work but will not work if changed to mysqli_*
    $intIdField = 'code';
    $strNameField = 'name';
    $strTableName = 'country';
    $strNameOrdinal = 'name';
    $strMaskName = 'select country';
    $strOrderField = 'name';
    $strMethod="ASC";
    echo "<select name=\"$strNameOrdinal\">\n";
    echo "<option value=\"NULL\">".$strMaskName."</option>\n";
    $strQuery = "select $intIdField, $strNameField
    from $strTableName
    order by $strOrderField $strMethod";
    if (!$strQuery) {
    die("query failed:" . mysql_error());
    }
    echo
    mysql_query ("set character_set_results='utf8'");
    $rsrcResult = mysql_query($strQuery);
    while($arrayRow = mysql_fetch_assoc($rsrcResult)) {
    echo $row["name"];
    echo $row["code"];
    $strA = $arrayRow["$intIdField"];
    $strB = $arrayRow["$strNameField"];
    echo "<option value=\"$strA\">$strB</option>\n";
    }
    echo "</select>";
  2. #2
  3. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,688
    Rep Power
    1841
    For a start you seem to be missing the actual part where you execute the query held in the string
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2014
    Location
    Centurion, South Africa
    Posts
    4
    Rep Power
    0
    I am not exactly sure what you are referring to, as I say, this is new to me but are you not referring too
    $strQuery = "select $intIdField, $strNameField
    from $strTableName
    order by $strOrderField $strMethod";

    which is there. As I say, this coding work but when I change to 'mysqli_* (with the extra parameter at $result), it is not working
  6. #4
  7. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,688
    Rep Power
    1841
    Either you have not copied everything or I am mis-reading (or mis-understanding - which is possible, I don;t really do php), but ... you have:
    Code:
    $strQuery = "select $intIdField, $strNameField
    from $strTableName
    order by $strOrderField $strMethod";
    if (!$strQuery) {
    That is, you set a variabl to contain the query you wish to execute. Then, the very next thing you do with that variable is test it for being false. You need to pass that string to mySQL to execute it and test the return from the execute, NOT the string with the query in.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1018
    Hi ptanewbe,

    you can't just replace “mysql” with “mysqli”. Those are two entirely different extensions which work differently. Actually, what's the whole point of changing when you don't use the new features of MySQLi and merely stick to the same old techniques you used before?

    Besides that, your old mysql_* code looks pretty bad. This was never correct, not even back then.

    You need to learn how to use MySQLi. Anything else is just a waste of time.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2014
    Location
    Centurion, South Africa
    Posts
    4
    Rep Power
    0
    Thanks, as I say I am new to this and I will work through your links. It means I will have to start over but I would rather do it right

    Comments on this post

    • Jacques1 agrees : Great attitude. :-)
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1018
    Great attitude.

    If you have any trouble with the explanations, just ask.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2014
    Location
    Centurion, South Africa
    Posts
    4
    Rep Power
    0
    Thanks, I appreciate. I am already busy reading up on the details, I really need to make this work. I have programmed for 30+ years on mainframes (Natural/Adabas and Cobol) but this is totally another ball game.
  16. #9
  17. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1018
    By the way, you should consider using PDO instead of MySQLi. It's much more comfortable, and you can use it for many different database systems (not only MySQL).

    Comments on this post

    • NotionCommotion agrees : Yes for PDO!
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo