Welcome! Log In Create A New Profile

Advanced

PHP Twelve - PHP and MySQL

Posted by 77959132 NeoGek 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
PHP Twelve - PHP and MySQL
March 01, 2013 06:30AM
Was very cool once I figured that my phpMyAdmin was slightly different that the one used in the example. Done!
avatar Re: PHP Twelve - PHP and MySQL
March 01, 2013 08:16AM
What is the best way to type a long SQL statement? I am forever missing something somewhere and getting errors.
avatar
Mac
Re: PHP Twelve - PHP and MySQL
March 01, 2013 08:25AM
I understand your question 100%

So this is how I do it

Language: PHP
$query = mysql_query(" UPDATE playerpoints SET tries=';$tries';, pens=';$pen';, convs=';$conv';, drops=';$drops'; WHERE fid=';$fid'; AND id=';$playerid'; ");

This allows me to ensure that there is not typos, not extra or missing ticks, commas etc.

When I am sure it is correct and it is ready for publishing, I put it back on one line.
avatar Re: PHP Twelve - PHP and MySQL
March 01, 2013 10:04AM
Thanks Mac!!

The examples in the book make use of

Language: PHP
$insert = "INSERT INTO movie (movie_id, movie_name, movie_type, " . "movie_year, movie_leadactor, movie_director)" . "VALUES (1, ';Bruce Almighty';, 5, 2003, 1, 2), " . "(2, ';Office Space';, 5, 1999, 5, 6), " . "(3, ';Grand Canyon';, 2, 1991, 4, 3), ";

This is a pain. Miss one comma or fullstop and you get a SQL error. Your example is much better!! Thanks!!
avatar
Mac
Re: PHP Twelve - PHP and MySQL
March 01, 2013 12:00PM
Yip, much easier like this

Language: SQL
$insert = " INSERT INTO movie (movie_id, movie_name, movie_type, movie_year, movie_leadactor, movie_director) VALUES (1, ';Bruce Almighty';, 5, 2003, 1, 2), (2, ';Office Space';, 5, 1999, 5, 6), (3, ';Grand Canyon';, 2, 1991, 4, 3) ";     //OR WHEN submitted BY a form, FOR example   $insert = " INSERT INTO movie VALUES ';';, ';$name';, $type';, ';$year';, ';$actor';, ';$director'; "; //IF you USE ALL the FIELDS, you do NOT have TO specify the COLUMN names. I USE '; '; FOR the movie_id, which IS an auto INCREMENT

See how the missing ' before $type stands out!
Re: PHP Twelve - PHP and MySQL
March 13, 2013 12:48PM
Does formatting of your code make a difference ?

Example is there a difference between these two in php??
Language: PHP
} }

I have noticed that I sometimes get errors because of stuff like this
avatar
Mac
Re: PHP Twelve - PHP and MySQL
March 13, 2013 03:49PM
That is not really formatting... it is just spacing of the brackets. Has no effect, other than that is adds white space which slows down parsing of the code by millions of a second..... Errors can only surface here because of of a mismatch of opening and closing brackets. This becomes a problem when you nest brackets i.e. you have sets of brackets inside other brackets. You probably fixed the error by moving i left or right, which allowed an missing bracket to surface

Language: PHP
...{ //code ...{ //code } {
Re: PHP Twelve - PHP and MySQL
March 15, 2013 08:41PM
Great chapter, My phpMyAdmin was also slightly different, but you learn more with the little challenges.
Re: PHP Twelve - PHP and MySQL
March 16, 2013 11:07AM
Yes thanks, very helpful.
avatar Re: PHP Twelve - PHP and MySQL
March 17, 2013 06:05PM
This section was not as complicated as I expected. It seems I have been exposed to all that it mentioned in my day to day work without realising it but I have to admit that I sometimes still struggle with the type field.

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
avatar Re: PHP Twelve - PHP and MySQL
March 18, 2013 03:08AM
Yes, also had some issues with examples and my version of MySql. Did someone else also not find the Extra field setting mentioned in the example where you should indicate auto_increment or do I just need new glasses? I looked on line and it seems in my version of phpMyadmin I have to tick the box under A_I to select auto increment for that field. Otherwise very good explanations in this section.
avatar Re: PHP Twelve - PHP and MySQL
March 18, 2013 12:43PM
Nope, I did not get it but then as you said it was a setting in your PMA and my hosting company handles that so that might be why.

77929284 Wrote:
-------------------------------------------------------
> Yes, also had some issues with examples and my
> version of MySql. Did someone else also not find
> the Extra field setting mentioned in the example
> where you should indicate auto_increment or do I
> just need new glasses? I looked on line and it
> seems in my version of phpMyadmin I have to tick
> the box under A_I to select auto increment for
> that field. Otherwise very good explanations in
> this section.

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
Re: PHP Twelve - PHP and MySQL
March 20, 2013 10:45AM
Done, if u sing xamplite notice its slightly different and i think some of these screeenshots where done in win xp, otherwise completed chapter succesfuilly

..&ru..
Re: PHP Twelve - PHP and MySQL
March 20, 2013 11:10AM
done.
avatar Re: PHP Twelve - PHP and MySQL
March 20, 2013 01:09PM
I just ran across this on a website linking to an article on the PHP website. It is about mysql_query not being available as from PHP 5.5.0

mySQL
avatar Re: PHP Twelve - PHP and MySQL
March 20, 2013 11:34PM
phpMyAdmin makes it very easy to create new databases and tables because you are working with a graphical interface.
This is a must for anyone starting out with MySQL.
I remember trying to create a database and some tables.
I was totally lost until I discovered phpMyAdmin.
You can even execute a SQL statement and see what result is returned.
Re: PHP Twelve - PHP and MySQL
March 21, 2013 07:54PM
I really like SQL, it is just so useful! smiling smiley
Is it necessary to use phpMyAdmin?
Command line SQL worked easy enough for me.
avatar
Mac
Re: PHP Twelve - PHP and MySQL
March 22, 2013 08:33AM
phpMyAdmin... it is easiest to use. By far.
Re: PHP Twelve - PHP and MySQL
April 02, 2013 12:25AM
Mysql does not work it just display blank page. Wht seems to be the problem here? I am almost done with the last chapterz
avatar Re: PHP Twelve - PHP and MySQL
April 02, 2013 09:48AM
Dude, are you doing your chapters out of sequence?

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
Re: PHP Twelve - PHP and MySQL
April 02, 2013 01:33PM
is your table populated?

can you connect to a database successfully?
Re: PHP Twelve - PHP and MySQL
April 04, 2013 08:41PM
Language: PHP
$SQL = "INSERT INTO tb_address_book (id,First_Name, Surname, Address) VALUES (';$_POST[';id';]';,';$_POST';[First_name';]';,';$_POST[';Surname';]'; ,';$_POST[';Address';]';);
i was trying to insert data by this line but it gives me errors. whats wrong here
avatar Re: PHP Twelve - PHP and MySQL
April 04, 2013 09:50PM
INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)


78042968 Wrote:
-------------------------------------------------------
>
Language: PHP
> $SQL = "INSERT INTO tb_address_book > (id,First_Name, Surname, Address) VALUES > (';$_POST[';id';]';,';$_POST';[First_name';]';,';$_POST[';Su > rname';]'; ,';$_POST[';Address';]';); >
> i was trying to insert data by this line but it
> gives me errors. whats wrong here

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
Re: PHP Twelve - PHP and MySQL
April 05, 2013 08:56AM
error 1: first_name
Language: PHP
';$_POST';[First_name';]';,


error 2: missing closing quote for INSERT - just before a semicolon

is there a reason for inserting an id? since you are inserting, I will assume you currently don't have ID field on your form.

this is my simplest way of doing insert

Language: PHP
<?php //assuming your form fields are named as follows: First_name, Surname, Address   $first_name = $_POST[';First_name';]; $surname= $_POST[';Surname';]; $address = $_POST[';Address';];   // escape special charecters in a string $first_name = mysql_real_escape_string($_POST[';First_name';]); $surname= mysql_real_escape_string($_POST[';Surname';]); $address = mysql_real_escape_string($_POST[';Address';]);   //testing to see what values will be sent to a database echo "First Name: ".$first_name."<br>"; echo"Surname: ".$surname."<br>"; echo"Address: ".$address;     $SQL = "INSERT INTO tb_address_book (First_Name, Surname, Address) VALUES (';$first_name';,';$surname';,';$address';)";   // please note i did not insert ID - assume your id column is on auto_increament. ?>
Re: PHP Twelve - PHP and MySQL
April 05, 2013 09:09AM
... also there is a proble with the way you use your quotes/ single quotes.

Your statement should atleast look like this:
Language: PHP
$SQL = "INSERT INTO tb_address_book (id, First_Name, Surname, Address) VALUES (';$_POST[id]';, ';$_POST[First_name]';, ';$_POST[Surname]';, ';$_POST[Address]';)";
Re: PHP Twelve - PHP and MySQL
April 06, 2013 12:34AM
I need help.. when i run the script below i get this error: Warning: mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Access denied for user 'bp5am'@'localhost' (using password: YES) in C:\wamp\www\test\select.php on line 3

Language: PHP
<?php //connect to MySQL; note we';ve used our own parameters- you should use //your own for hostname, user, and password $connect = mysql_connect("localhost", "bp5am", "bp5ampass") or die ("Hey loser, check your server connection.");   //create the main database if it doesn';t already exist $create = mysql_query("CREATE DATABASE IF NOT EXISTS moviesite") or die (mysql_error());   //make sure our recently created database is the active one mysql_select_db("moviesite");   //create "movie" table $movie = "CREATE TABLE movie ( movie_id int(11) NOT NULL auto_increment, movie_name varchar(255) NOT NULL, movie_type tinyint(2) NOT NULL default 0, movie_year int(4) NOT NULL default 0, movie_leadactor int(11) NOT NULL default 0, movie_director int(11) NOT NULL default 0, PRIMARY KEY (movie_id), KEY movie_type (movie_type,movie_year) )";   $results = mysql_query($movie) or die (mysql_error());   //create "movietype" table $movietype = "CREATE TABLE movietype ( movietype_id int(11) NOT NULL auto_increment, movietype_label varchar(100) NOT NULL, PRIMARY KEY (movietype_id) )";   $results = mysql_query($movietype) or die (mysql_error());   //create "people" table $people = "CREATE TABLE people ( people_id int(11) NOT NULL auto_increment, people_fullname varchar(255) NOT NULL, people_isactor tinyint(1) NOT NULL default 0, people_isdirector tinyint(1) NOT NULL default 0, PRIMARY KEY (people_id) )";   $results = mysql_query($people) or die (mysql_error());   echo "Movie Database successfully created!";   ?>
avatar Re: PHP Twelve - PHP and MySQL
April 06, 2013 01:19PM
Lean, have you checked your connection details? Comment out everything but your connection and see if you can connect. If so uncomment out each section one at a time till you have isolated the offending code. Makes sense?

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
Re: PHP Twelve - PHP and MySQL
April 06, 2013 02:48PM
Thanks Shado. let me try that.
Re: PHP Twelve - PHP and MySQL
April 07, 2013 07:23PM
Hi 77960009 Lean,
I got the same error when my password was incorrect, maybe just double check your password?
Re: PHP Twelve - PHP and MySQL
April 08, 2013 08:34AM
OMG are we creating Databases and table in PHP? but why?

You wont have this problem if you created it straight in MYSQL Database or what ever DBMS you are using.

Do you know how to create a Database and tables in a DBMS?
Re: PHP Twelve - PHP and MySQL
April 10, 2013 01:44PM
hi everyone,

Is anyone using XAMPP for server or am i the only one?

I cannot find the directory for my first data base exercise i.e the 'tbl_address_book' created from phpMyAdmin. Can't seem to get where the file is saved.

Thanks.

78026962 afojonny
avatar
Mac
Re: PHP Twelve - PHP and MySQL
April 10, 2013 01:59PM
installation_directory/mysql/data

By default is under Program Files, which is no good for Win7 above becauue of administrator rights issues. One should install to C:, which makes it xampp/mysql/data
Re: PHP Twelve - PHP and MySQL
April 10, 2013 02:14PM
did you try - http://localhost/xampp/ and then clicking on PhpMyAdmin

look for a database that you created - under a database name, you should be able to see all your tables.
Re: PHP Twelve - PHP and MySQL
April 10, 2013 02:15PM
sorry [localhost ] will be
hhtp://localhost/XAMPP 
Re: PHP Twelve - PHP and MySQL
May 01, 2013 12:57PM
Good day to every one, i have the following questions:

1- When we add new database name, there is a drop dawn menu and its selected at collation? Its should be always at collation?

2- If I select LONGTEXT for all the text fields? It will slow the database?


3- I'm not sure whats the different between SMALLINT and INT type.
What i normally use for the ID is INT and length/valuse 11?

4- What’s the best to use a field type and secure for the passwords field?


Thank you
Mohammed Sabah
avatar
Mac
Re: PHP Twelve - PHP and MySQL
May 02, 2013 07:54AM
Default collation is fine.
Slow the db is difficult to define...in general use what you need

Do a Google search on "mysql field types tutorial" e.g. http://www.w3resource.com/mysql/mysql-data-types.php

e.g. INT can take a max value of 4294967295, whereas MEDIUM INT can take up to 16777215. So how many do you require, more than 167 million?
TINYTEXT can take 255 character, LONGTEXT 4294967295.
Re: PHP Twelve - PHP and MySQL
May 20, 2013 12:14PM
PHP Myadmin Was a bit different but could figure it out easily with some googling. Google is definitely a programmers friend grinning smiley Just goes to show the developments they make every day. Loving PHP winking smiley
Re: PHP Twelve - PHP and MySQL
May 20, 2013 12:30PM
I found the prescribed textbook useful too. The text fields were well explained.

78026962 afojonny
Sorry, only registered users may post in this forum.

Click here to login