Web Design and Programming Pt 16

[ad_1]
I show you how to connect to and insert information into a MySQL database. I show you how to enter the information: Directly through the MySQL shell : With PHP code : From a CSV file

Code is Here:


Posted

in

by

Tags:

Comments

30 responses to “Web Design and Programming Pt 16”

  1. Nemanja Avatar

    there's no csv file :/

  2. davis aronovich Avatar

    Hi,
    I getting and err " call to undefined function mysgl_connect() ?
    i have Xampp installed. also if i change to mysqli_connect it doesnt give error in next line for mysql_select_db that its undefined.?

  3. marcus whelan Avatar

    could you make a video just like this one but instead of importing from a csv file could you import from a .xlsx file?

  4. Sexual Potatoes Avatar

    Wow things really escalated in difficulty as you started to casually and constantly introduce new concepts without going over them individually >_<

  5. alexander shekhtman Avatar

    I finally figured out what was wrong: I must have been blind, because all those {} confused me. here is my code, which maybe should have been looked at in the beginning:
    if($dbc = mysql_connect (DBHOST, DBUSER, DBPW) ){
    if(!mysql_select_db (DBNAME) ){
    trigger_error("Couldn't connect to the database<br>MySQL Error: " . mysql_error () );
    exit();
    }
    else{
    trigger_error("Couldn't connect to MySQL<br>MySQL Error: " . mysql_error() );
    exit();
    }
    }

    I had the else inside the upper if. but the else belongs below the upper if. now it works. i was going crazy installing different versions of mysql from the mysql site and various auto install scripting sites like brew. next time i will look at the code more carefully. now i can install from scratch the latest version of mysql from the mysql site.

    and here is the terminal print out:
    mysql> select * from manufacturers
        -> ;
    +—————+—————–+
    | manufacturer_id | manufacturer_name |
    +—————+—————–+
    |               2 | Acer              |
    |               3 | Acroprint         |
    |               4 | Actu              |
    +—————+—————–+
    3 rows in set (0.00 sec)

    and here is the html print out:
    insert into manufacturers (manufacturer_id, manufacturer_name) values (2,'Acer')
    insert into manufacturers (manufacturer_id, manufacturer_name) values (3,'Acroprint')
    insert into manufacturers (manufacturer_id, manufacturer_name) values (4,'Actu')

    it is so embarrassing that a little {} caused me so much grief and anger i was ready to just give up.

  6. alexander shekhtman Avatar

    see everything works when i log in at terminal 
    mysql> show databases;
    +——————+
    | Database           |
    +——————+
    | information_schema |
    | customers          |
    | mysql              |
    | performance_schema |
    +——————+
    4 rows in set (0.00 sec)

    mysql> use customers;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A

    Database changed
    mysql> desc customers;
    +—————–+———–+—-++——-+————–+
    | Field             | Type        | Null | Key | Default | Extra          |
    +—————–+———–+—-++——-+————–+
    | manufacturer_id   | smallint(5) | NO   | PRI | NULL    | auto_increment |
    | manufacturer_name | varchar(45) | NO   |     | NULL    |                |
    +—————–+———–+—-++——-+————–+
    2 rows in set (0.01 sec)

  7. alexander shekhtman Avatar

    once again here is proof that i am able to connect to the mysql server with root and password:
    alexander$ mysql -h localhost -u root -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 8
    Server version: 5.6.22 Homebrew

    Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.

    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

    mysql> 

  8. alexander shekhtman Avatar

    I did a clean install of mysql from something called Brew:
    https://coderwall.com/p/os6woq/uninstall-all-those-broken-versions-of-mysql-and-re-install-it-with-brew-on-mac-mavericks

    But I still get these errors and therefore can't connect to sql via php:
    WARNING: Found existing config file /usr/local/opt/mysql/my.cnf on the system.
    Because this file might be in use, it was not replaced,
    but was used in bootstrap (unless you used –defaults-file)
    and when you later start the server.
    The new default config file was created as /usr/local/opt/mysql/my-new.cnf,
    please compare it with your file and take the changes you need.

    WARNING: Default config file /etc/my.cnf exists on the system
    This file will be read by default by the MySQL server
    If you do not want to use this, either remove it, or use the
    –defaults-file argument to mysqld_safe when starting the server

    I am going absolutely crazy, there is no reason why it shouldn't work. I am thinking somehow the sockets are not properly configured so i need help in what to use/write/modify, maybe some .cnf file. i don't know much unix or servers so this is all i have for you or anyone else that knows what to do. i can't continue learning php unless this is fixed. 

  9. alexander shekhtman Avatar

    i found a file that controls the mysql server but i have no clue what to modify. please if anyone knows what to do send me modifications. thank you.

    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

    [mysqld]

    # Remove leading # and set to the amount of RAM for the most important data
    # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    # innodb_buffer_pool_size = 128M

    # Remove leading # to turn on a very important data integrity option: logging
    # changes to the binary log between backups.
    # log_bin

    # These are commonly set, remove the # and set as required.
    #basedir = localhost
    #datadir = php
    port = 3306
    #server_id = …..
    socket = /var/mysql/mysql.sock

    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
    # join_buffer_size = 128M
    # sort_buffer_size = 2M
    # read_rnd_buffer_size = 2M

    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

    # CUSTOMIZED BY MMVMySQL SCRIPT – JUST GENERIC SETTINGS
    # DO NOT TREAT AS GOSPEL

    innodb_buffer_pool_size=2G
    skip-name_resolve
    max-connect-errors=100000
    max-connections=500

  10. alexander shekhtman Avatar

    btw there is no mysql5 command on my mac. 
    and here is a print out having a successful login in at terminal:
    Alexanders-MacBook-Air:~ alexander$ mysql -h localhost -u root -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 8
    Server version: 5.6.22 Homebrew

    Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.

    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

    mysql> show databases;
    +——————+
    | Database           |
    +——————+
    | information_schema |
    | customers          |
    | mysql              |
    | performance_schema |
    +——————+
    4 rows in set (0.00 sec)

  11. alexander shekhtman Avatar

    please someone help me i am out of options nothings works for me to use a php to connect to sql server please tell me what to do.

  12. alexander shekhtman Avatar

    i am able to add rows  via command line but still nothing works from php
    mysql> insert into manufacturer values(null, 'johnson');
    Query OK, 1 row affected (0.01 sec)

  13. alexander shekhtman Avatar

    i think i fixed the sql connect error now the sql thinks there is no such file name:
    Warning: mysql_connect(): No such file or directory in /Library/WebServer/Documents/php/phptutorial16.php on line 12
    insert into manufacturer (manufacturer_id, manufacturer_name) values ('2','Acer America')

    Warning: mysql_query(): No such file or directory in /Library/WebServer/Documents/php/phptutorial16.php on line 29

    Warning: mysql_query(): A link to the server could not be established in /Library/WebServer/Documents/php/phptutorial16.php on line 29

    Notice: Query failed
    MySQL Error: No such file or directory in /Library/WebServer/Documents/php/phptutorial16.php on line 29
    insert into manufacturer (manufacturer_id, manufacturer_name) values ('3','Acroprint')

    Warning: mysql_query(): No such file or directory in /Library/WebServer/Documents/php/phptutorial16.php on line 29

    Warning: mysql_query(): A link to the server could not be established in /Library/WebServer/Documents/php/phptutorial16.php on line 29

    Notice: Query failed
    MySQL Error: No such file or directory in /Library/WebServer/Documents/php/phptutorial16.php on line 29
    insert into manufacturer (manufacturer_id, manufacturer_name) values ('4', 'Actu')

    Warning: mysql_query(): No such file or directory in /Library/WebServer/Documents/php/phptutorial16.php on line 29

    Warning: mysql_query(): A link to the server could not be established in /Library/WebServer/Documents/php/phptutorial16.php on line 29

    Notice: Query failed
    MySQL Error: No such file or directory in /Library/WebServer/Documents/php/phptutorial16.php on line 29

    Warning: mysql_close(): no MySQL-Link resource supplied in /Library/WebServer/Documents/php/phptutorial16.php on line 32

  14. alexander shekhtman Avatar

    if anyone has answers how to connect to mysql i will try it as i ran out of options…

  15. alexander shekhtman Avatar

    this too doesn't work:
    Modify the mysql.script using this command (enter it on a single line):

    shell> sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit
      /usr/local/mysql/support-files/mysql.server
    Locate the option file that defines the basedir value and modify it to contain these lines:

    basedir=/usr/local/mysql
    datadir=/usr/local/mysql/data
    In the /Library/StartupItems/MySQLCOM/ directory, make the following group ID changes from staff to wheel:

    shell> sudo chgrp wheel MySQLCOM StartupParameters.plist

  16. alexander shekhtman Avatar

    this too doesn't work:
    sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
    Password:
    ln: /var/mysql/mysql.sock: File exists

  17. alexander shekhtman Avatar

    nothing works to connect to sql, i even did this:
    GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

  18. alexander shekhtman Avatar

    i get this error:
    Notice: Couldn't connect to MySQL
    MySQL Error: in /Library/WebServer/Documents/mysql/phptutorial16.php on line 18

  19. alexander shekhtman Avatar

    where is the cvs file for manuf

  20. alexander shekhtman Avatar

    zip should also be unsigned

  21. Chanrithea Bun Avatar

    I have trouble inserting into any databases I created except the 'test' database that comes with MySQL. I tried creating two databases under 'root' with a password. Both databases won't except insert via PHP.

    I keep getting this error message:

    Notice: Couldn't select database
    MySQL Error: Access denied for user ''@'localhost' to database 'MyTestDB'

  22. John von Horn Avatar

    That's one tinyint for DB but one massive double for me (trying to paraphrase Neil Armstrong's first step on the moon, there). A superb milestone; being able to use a cgi script to add info to a MySQL database. I got such a buzz out of seeing that actually work (I went slightly off road and added state codes and names to my "states" table). This is awesome! If we keep going we can actually create fully functioning websites. That is massive! I might not be Mr. Minimal Wage for much longer – woo hoo :))

    OK, let's ask the great man a question, please. You told us to use debugging code during development but to take that code out when ready for production. Would you actually take the code out or just switch it off with a boolean constant?

    define('ERROR_MESSAGES',0);     # I set this to 'FALSE' and it still displayed, needed to be 0 [or define TRUE and FALSE, maybe?] :))

    if (ERROR_MESSAGES) … echo "Can't open database" …

    Finally, a tribute snippet to you:

    # We don't define the master, he defines us – your new tagline

    While(Derek_is_brilliant)
    {
         # Will run forever
         echo "Thank You";
    }

  23. LaZonaInformatica Avatar

    What is the difference between trigger_error() and die()? Are they used in different situations or is it just a matter of preference? Thanks

  24. Derek Banas Avatar

    You're very welcome 🙂

  25. Sanket Rao Avatar

    super stuff derek, really useful thanks

  26. Derek Banas Avatar

    Thank you 🙂 My channel is definitely different from most. I don't even know what I'm going to cover from one week to the next. I'm glad you like them

  27. Sglow Avatar

    I have subscribed and will probably end up watching a few hundred of your videos by the time I'm done haha. You teach very well and seem to actually KNOW what you are talking about, which honestly is kind of rare for some of the tutorials on YT 😉

    I even forwarded your homemade pizza video to my brother!

  28. Marius B Avatar

    you are a Tutorial GOD!

  29. Derek Banas Avatar

    @soterescon Thanks a bunch. Thanks for being the only commenter 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *