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

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2

    Unable to insert row in MySQL


    Hi,

    I have the below code. I am trying to insert a row in mysql table. I am able to connect to data base just fine. Once I run the code, I dont get any error message also no rows get inserted into the table.

    Would you please help?


    <?php
    $con = mysql_connect("","sql","A");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }


    mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
    VALUES ('Peter', 'Griffin',35)");

    mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
    VALUES ('Glenn', 'Quagmire',33)");

    mysql_close($con);
    ?>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2
    Hi,

    I have the below code. I am trying to insert a row in mysql table. I am able to connect to data base just fine. Once I run the code, I dont get any error message also no rows get inserted into the table.

    Would you please help?


    PHP Code:
    <?php
    $con 
    mysql_connect("","sql","A");
    if (!
    $con)
    {
    die(
    'Could not connect: ' mysql_error());
    }


    mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
    VALUES ('Peter', 'Griffin',35)"
    );

    mysql_query("INSERT INTO Persons (FirstName, LastName, Age) 
    VALUES ('Glenn', 'Quagmire',33)"
    );

    mysql_close($con);
    ?>
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Why are you going back to that ancient mysql_ functions when you were just starting with PDO?

    Sorry, but I don't see the point of fumbling with obsolete functionalities that you shouldn't use in production code, anyway.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Oxford, United Kingdom
    Posts
    40
    Rep Power
    2
    You're not seeing any errors because your code isn't handling them.

    I'm not fixing that code. As I've already mentioned in one of your previous posts, you should be using PDO or MySQLi.

    In PDO, you would do:

    PHP Code:
    try 

       
    $db= new PDO"mysql:host=$host;dbname=$db$user $pass ); 

    catch ( 
    PDOException $ex 

       die( 
    "Connection failure - Error: " $ex->getMessage() ); 


    $query "INSERT INTO Persons (FirstName, LastName, Age) 
    VALUES ('Peter', 'Griffin','35')"


    try
    {
        
    $result $db->exec$query ); 
    }
    catch ( 
    PDOExeception $queryEx )
    {
        die ( 
    'Failed to execute query - Error: ' $queryEx->getMessage() );

  8. #5
  9. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    106
    Rep Power
    4
    For one thing, your mysql_query routine requires two parameters.
    You need to specify the link_id like this:
    Code:
    mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
    VALUES ('Peter', 'Griffin',35)",$con);
    Please consider upgrading to the mysquli API for procedural calls.
    The mysql API is depreciated. Google mysqli and select the
    "PHP: Mysqli - Manual" page for information on this new API.
    [I am not yet allowed to enter links, sorry.]
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Oxford, United Kingdom
    Posts
    40
    Rep Power
    2
    Originally Posted by richpri
    For one thing, your mysql_query routine requires two parameters.
    You need to specify the link_id like this:
    Code:
    mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
    VALUES ('Peter', 'Griffin',35)",$con);
    Please consider upgrading to the mysquli API for procedural calls.
    The mysql API is depreciated. Google mysqli and select the
    "PHP: Mysqli - Manual" page for information on this new API.
    [I am not yet allowed to enter links, sorry.]
    mysql_query doesn't actually require two params. If the resource (second param) isn't passed, it will check for the latest active connection and use it.

    I suspect the error is in the query and/or the DB/Table. He/she could be inserting data into columns that don't exist. Or maybe the table doesn't exist?

    Either way, the code I've provided should reveal all.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Oxford, United Kingdom
    Posts
    40
    Rep Power
    2
    Also, please don't help somebody perform bad practices.

    Bad habits are hard to forget. If we get this chap started on depreciated functions, it will be hard for him to stop.
  14. #8
  15. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    106
    Rep Power
    4
    I see your point about bad habits. But, being an old fart, I still like procedural [not object oriented] code. And mysquli supports both. That's why I nudged in that direction.
  16. #9
  17. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Hi,

    Originally Posted by richpri
    And mysquli supports both. That's why I nudged in that direction.
    I guess he only read the first part of your post about mysql_query().

    The MySQLi extension is perfectly fine.

    But for both of you: It's deprecated, not "depreciated". Kind of funny nobody seems to get it right.
  18. #10
  19. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Depreciated:
    Diminish in value over a period of time.
    ..I suppose it is not totally inappropriate
    "The mysql extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used." the docs

IMN logo majestic logo threadwatch logo seochat tools logo