Need PHP Basics Information

Discussion in 'PHP' started by webjohn52, Mar 22, 2013.

  1. #1
    I am new in PHP, I have just started my career with this. So I want some basics of database like PHP Would you please provide me the relevant information that will be useful for me

    Your reply will be appreciated.
    Thanks
    shankar
     
    webjohn52, Mar 22, 2013 IP
  2. gandalf117

    gandalf117 Active Member

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    The relational database management system (RDBMS) that is used with PHP is called MySQL. The language used to work with it is SQL (Structured Query Language). But SQL is only used to invoke queries and is usually used in conjunction with other languages like: PHP, Python, Ruby, Java, C#, etc.

    The good news is there are only a few lines of code that you need to have a working database with PHP:


    $host = "localhost";
    $dbusername = "root";
    $dbpassword = "SomePassword";
    $database = "DatabaseName";
    $dblink = mysql_connect($host,$dbusername,$dbpassword);
    $db = mysql_select_db($database);


    Then you can just do something like:
    $query = mysql_query("select * from SomeTable;");
    while($row = mysql_fetch_array($query))
    {
    echo "$row[SomeFieldName], $row[SomeOtherFieldName], etc.";
    }
    and you can get any information assuming that you have an existing database and you have connection with it.

    The bad news is that this is just the very basics. There are other ways to establish a connection with a database but the real challenges come with forming the particular queries. For example "select * from SomeTable;" is literally the simplest query there is. This little query by itself can be very complex or very very long or both. It could be several rows or even pages depending on what you are doing. So for that you need to know SQL.

    There are plenty of links and tutorials on how to create databases and queries with SQL on the internet. Just search for it. You already have the very basics.

    Cheers
     
    gandalf117, Mar 22, 2013 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    I have worse news: Everything you posted in old and deprecated code. Don't even bother starting with the mysql_* functions. If you want to access databases, use PDO instead. It's your best bet.

    More on the subject: www.php.net/pdo
     
    nico_swd, Mar 22, 2013 IP
  4. Strider64

    Strider64 Member

    Messages:
    40
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    25
    #4
    I'll just add another option, if you know you are only going to work with MySQL then you can also use mysqli <-- Notice the i an the end of the string. Just something to think about before you get started with a project. However, like gandalf said most peope prefer PDO for it gives them the better option when it comes to types of databases that are out there. Just DON'T start off using mysql_* functions.
     
    Strider64, Mar 22, 2013 IP
  5. gandalf117

    gandalf117 Active Member

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5

    lol, don't scare him like that. They are depreciated but only in PHP 5.5.0 which got released literally yesterday. I wouldn't say that he shouldn't start with this "old code".

    On the contrary the procedural style is much less confusing especially for starters. The object oriented style is daunting even to look at, especially that link you sent him.
     
    gandalf117, Mar 22, 2013 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    I kinda disagree there. I think he should start with PDO or at least MySQLi. Once he gets used to the mysql_* functions, he might won't bother learning anything else until it's completely removed from the PHP core.

    It may be a little more confusing, yes, but on the long run it's the best solution. Especially when you're still learning, it's very likely that you forget to escape some variables and there are SQL injection vectors all over your code.

    You're right on the link, though. It might not be the most helpful. :p

    This one is probably better: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
     
    nico_swd, Mar 22, 2013 IP
    Rukbat likes this.
  7. gandalf117

    gandalf117 Active Member

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #7
    I agree that on the long run it is best to stick to the object oriented solution, yes.
     
    gandalf117, Mar 22, 2013 IP
  8. annaharris

    annaharris Active Member

    Messages:
    119
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    51
    #8
    If you want to get some basic information about PHP, then first you need to understand variables, arrays, mysql, etc. Refer these links, you can get some ideas.
    http://www.php.net/manual/en/index.php
    http://www.tizag.com/phpT/
     
    annaharris, Mar 30, 2013 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #9
    Thing is, much like we've been told to STOP using HTML 3.2 for some fifteen years (and people keep sleazing it out as 4 tranny and now 5 lip-service) we've been told to STOP using mysql_ functions for almost eight years now... they FINALLY added the big red warning boxes telling people to STOP using it -- but of course you have apologists who seem to just want nubes to intentionally write insecure code. God forbid you teach people the RIGHT and BETTER way of doing things before introducing them to half-assed broken and buggy methodology. It's also a great tool to teach OOP in PHP or at least drive home how to use it so they're not stuck when it comes time to put on the big boy pants.

    ... and if for some noodle doodle reason you REALLY think functional is 'easier' (It isn't) mysqli_ offers those too... though I find it's implementation harder to teach or learn than PDO's, particularly when it comes to building queries (bindParam SUCKS compared to array passing when you start having large amounts of data)

    Maybe I've been at this **** for too long, but how PDO is any harder than the old mysql_ functions is beyond me. If anything it's simpler -- I think the only people who would find it harder are the people who still have their heads wedged up 1997's arse...

    Which is to say the majority of people sleazing out websites it seems.
     
    deathshadow, Apr 1, 2013 IP
    nico_swd likes this.
  10. gandalf117

    gandalf117 Active Member

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #10

    You sound like a proud and knowledgeable guy who likes to keep to standards and conventions. And once again you have a good point but it seems you are totally missing mine. Here are my points:

    1) I started programming literally almost 10 years ago in collage. I still find classes harder and more confusing than ordinary functions. It just seems that you need more mental energy and concentration to work with them.

    2) You are definitely right that people shouldn't stick to old stuff and have to keep up with the newer and better methodologies but who is there to teach them? When you get stuck in learning on your own newer and newer things you can spend more energy in learning than actually achieving anything. You are very likely to lose focus on what you first came here for. The guy who asked the question came here to learn how to connect with a database not to learn the best, most secure and most professional way to do it.

    3) How are mysql_functions insecure? Everybody is talking about it but as always there is nobody there to show you. You have to believe in it as if it is some sort of religion. I have programmed a lot of sites using only these functions to access the database. Not one of them has been hacked. So how are they insecure exactly?
     
    gandalf117, Apr 1, 2013 IP
  11. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #11
    You were in a picture that was cut up and glued to poster board? Not really seeing how that's relevant... :D

    Ah, College. That explains why you can't spell it and have difficulty with these concepts. Joke, deadpan mode... crap, not that big a joke though, but again there's a reason I have never considered a college degree worth a sheet of bog roll. When it comes to IT the bull they fill these poor kid's heads with is just mind-numbingly ignorant and outdated.

    I find them many times simpler, but then I learned about records/structures some twenty years before you went to college, and objects are just a natural extension of the concept. (as evidenced by how they are implemented in Smalltalk Oberon and Object Pascal). Admittedly, if you've never dealt with a strictly typecasted language, pointers, memory management you may lack to tools to ever truly understand objects in terms of making an managing your own -- but using existing objects like PDO is so PISS SIMPLE I really have trouble figuring how anyone who can use the convoluted mess that is the mysql_ functions could have that big a problem with it.

    I mean really... connecting to the database:

    $database = new PDO(
    	'host=localhost; dbname=databaseName
    	'username',
    	'password'
    );
    Code (markup):
    Parameterless static query without results:
    $database->exec('DROP TABLE garbage');
    Code (markup):
    Parameterless static query with results:
    $statement = $database->query('SELECT * FROM POSTS LIMIT 10');
    Code (markup):
    Preparing an auto-sanitized parameter based query:
    $statement = $database->prepare('
    	SELECT * FROM users
    	WHERE
    		name = :name,
    		password = :password
    ');
    Code (markup):
    Executing the prepared query, this can be done more than once on a statement with different data sets if desired.
    $statement->execute(array(
    	':name' = $_POST['loginUsername'],
    	':password' => hash('whirlpool',$_POST['loginPassword'])
    ));
    Code (markup):
    Iterating through a result set:
    while ($user = $statement->fetch()) {
    	echo '
    		ID: ',$user['id'],'<br />
    		Username: ',$user['name'],'<br />';
    }
    Code (markup):
    Pulling the complete result set to an array:
    $userList = $statement->fetchAll();
    Code (markup):
    Pulling the first column value from the current row, useful for things that only return one value like 'count'
    $count = $statement->fetchColumn();
    Code (markup):
    Apart from error handling with errorCode/errorInfo (since statements don't raise an exception by default) how is that any more complex/difficult than the mysql_ functions? The answer is it isn't -- it's just different. Takes someone who knows the old way more than an hour to pick it up, there's something wrong.

    Though to be fair in a people in glass houses kind of way, I've been programming for 35 years now, and still cannot grasp 'visual programming' at ALL. We all have our limitations.

    That's circular logic at best, flawed thinking at worst. If there's anything I've learned the past 35 years, is that if you're not gonna teach the best way of doing things, one best get the **** outta the way. The day you think there's nothing new to learn is the day the world leaves you behind. I mean, he came here asking for help, so who's here to teach him? WE ARE! ... and probably do a better job than most educators who aren't qualified to open their yap about a blasted thing in the subjects they're supposed to be teaching.

    They do not auto-sanitize parameters. Prepared queries do. You have to use that whole mysql_real_escape_string asshattery which NEVER actually seemed to work properly, required all sorts of logic to figure out if that stupid idiotic "safe strings" crap was involved, and all query construction boils down to blindly dumping variables into a string addition instead of parsed insertion. You CAN make it secure -- but it is not secure by design. It's a subtle but important difference.

    Though again, to be fair, PHP itself is not secure by design either.
     
    Last edited: Apr 1, 2013
    deathshadow, Apr 1, 2013 IP
  12. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    160
    #12
    If you're still getting confused about OOP after 10 years of coding and you find functions more usefull than classes , you really should start trying to understand how to code before even trying to. I bet you're still mingling PHP with HTML and not making use of AJAX and JSON.


    You are telling the man to use procedural because you like it more than OO but you don't have a good reason for it. Ups wait, the reason is just there, you said it yourself "i'm still getting confused over classes".
     
    edduvs, Apr 3, 2013 IP
  13. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #13
    I don't use AJAX or JSON, but that's because the former usually pisses on accessibility from orbit, and the other isn't as useful for server-side operations as I'd like. Unless you're building a full blown web application, there's no reason to use either of those -- and to be brutally frank, web applications get down on their knees behind the proverbial equine; particularly if the same thing could have been delivered as flat simple accessible web pages.

    Mind you, I know HOW to use them, and that's WHY I don't. Kinda like the half-baked idiocies known as HTML 5 or jQuery.
     
    deathshadow, Apr 3, 2013 IP
  14. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    160
    #14
    Beg a pardon, was I speaking with you ?
     
    edduvs, Apr 3, 2013 IP
  15. YoGem

    YoGem Active Member

    Messages:
    676
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    90
    #15
    Hey all... I wanted to let you read well what OP says:

    Now I see that many people here are expert but you need to understand to who you are answering, I bet OP have no clue how to use objects as well no idea of what MySQL is. So the friendly approach of gandalf, to start, is the best one.
     
    YoGem, Apr 5, 2013 IP
  16. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    160
    #16
    YoGem, you know PDO is still a object oriented mysql handle right ? So whatever he'll try to learn, he must NOT start with simple procedural mysql. Why learning the old 90's in the 21st century. He will surely find it hard to pass from procedural mysql to object oriented style pdo or even mysqli. Even if MySQLi has it's own procedural styles, it's better if he tries to understand what's an object before working with it.
     
    edduvs, Apr 6, 2013 IP
  17. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #17
    That was a "Hey, I resemble that comment" even though we're kind of on the same side of this one. Not using those tech really didn't seem to apply. Friendly ribbing? Humor?
     
    deathshadow, Apr 6, 2013 IP
  18. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #18
    Hope i may answer aswel :+


    Most people start driving a three weeler and then drive just a normal cycling bike and most will be able to drive a car after (16/17/18) but then again they (almost) all learned to drive the cycling bike first. Just the same as learning scripting, its not bad to learn old methods... its just wise... most times its easier to understand... and when you see new methods you understand them faster (my opinion)

    I've started learning with mysql_functions to and now working with PDO but in some cases i still use the mysql_functions, why? coz its easier... lol...

    Good morning (from the Netherlands..)
     
    EricBruggema, Apr 6, 2013 IP
  19. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    160
    #19
    Eric, people tend to code the easier way, but that and I'm sure as hell it doesn't mean it's the good way. I started on MySQL too but after 3 years of using it , tried to improve my code along the way so I read about MySQLi and PDO. Aren't you supposed to learn new and better ways of coding everyday ? If you're pleased with what your knowledge at some point, than coding is not your vocation.
     
    edduvs, Apr 7, 2013 IP
  20. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #20
    Shure, that's human isn't it? the less engery it cost the faster we choose that option ;) but for learning, you start from point zero and most php books i have red began with just the mysql(i)_ functions and later about PDO. And learning we do all day, every day even if we don't notice it even with coding, by seeing examples/scripts from other people or just by reading the manual again..
     
    EricBruggema, Apr 7, 2013 IP