Log in

View Full Version : PHP/SQL tutorial



badboyxlr
21-03-2011, 09:08 PM
For this you will need:
PHP 5.1+
mySQL
A server, Or localhost.
Ok, lets get started. (spoilers are just acting as tabs.)
First, lets get the sql connection up and running:

connect.php file:

<?php
$con=mysql_connect(servername,username,password);
if (!$con){
die ('Could not connect: '.mysql_error());
} else {
// code for connected.
}
?>
Now, we will do a basic database create.
Code:

<?php
include ('connect.php');//the file we made above
$dbname="name of database";
if (mysql_query("CREATE DATABASE ".$dbname.",$con)){
echo "Database created";
} else {
echo "Error creating database: " . mysql_error();
}

mysql_close($con);// to close the database
?>
this is how to insert:
Code:

<?php
include ('connect.php');
$dbname="name of database";
$table="table to insert";
mysql_select_db($dbname, $con);

mysql_query("INSERT INTO ".$table." (row1, row2, row3)
VALUES ('value1', 'value2', 'value3')");

//close connection
mysql_close($con);
?>
How to create a table:
Code:

<?php
include ('connect.php');
$dbname="name of database";
$table="table to make";
mysql_select_db($dbname, $con);
$sql="CREATE TABLE ".$table.
"(
personID int NOT NULL AUTO_INCREMENT, // make the "personID" row auto increase.
PRIMARY KEY(personID), // part of the auto increasing.
row1 varchar(15),//the number "15" is the max length of the value's that can be inserted
row2 varchar(15),
row3 int // int is number onaly
)";
// Execute query
mysql_query($sql,$con);
//close connection
mysql_close($con);
?>
How to delete:
Code:

include ('connect.php');
$dbname="name of database";
$table="table to make";
mysql_select_db($dbname, $con);

mysql_query("DELETE FROM ".$table." WHERE ".$row."='".$value."'");

//close connection
mysql_close($con);

How to read:
Code:


include ('connect.php');
$dbname="name of database";
$table="table to make";
mysql_select_db($dbname, $con);
$result = mysql_query("SELECT * FROM Persons
WHERE FirstName='Peter'");

$row1="a row to put out, e.g. name";
$row2="a row to put out, e.g. email";

while($row = mysql_fetch_array($result)){
echo $row[$row1] . " " . $row[$row2];
echo "<br />";
}

//close connection
mysql_close($con);

Trinity
21-03-2011, 10:53 PM
This isn't a tutorial, it doesn't explain the code. It's more of a collection of code snippets, just like your other 'tutorial'.

badboyxlr
21-03-2011, 10:59 PM
how about you read whats called a "comment" or the // bits in the code.. ide say that explains my code tyvvm.

Trinity
21-03-2011, 11:18 PM
how about you read whats called a "comment" or the // bits in the code.. ide say that explains my code tyvvm.

Ok, I'll read the comment in the first code snippet. What? It just says 'code for connected'? I'm reading this tutorial because I'm a beginner to PHP, how am I supposed to know what those functions do and why you're checking if $con is true?

Second snippet. Oh look, only one comment and all it tells me is that we've just made connect.php. It doesn't tell me what the include() function does, or any of the other code there.

Third snippet. Only one comment in there, but it is a tiny, tiny bit helpful. Has the same problem as the first two.

Fourth snippet. Ok, you've explained a tiny, tiny, tiny bit of the SQL, but it isn't helpful. Has the same problem as the others.

Fifth snippet. Same problems.

Sixth snippet. Same problems.

tyvvm gg

Dentafrice
22-03-2011, 12:54 AM
This isn't a tutorial. This is posting code snippets as Trinity said. You're not explaining the WHERE syntax, or anything to do with anything. You're just posting code snippets to do certain things.

And usually it's the same file over and over.. this shouldn't be labeled as a tutorial, as it's not. A bad one at that.

LMS16
22-03-2011, 08:47 PM
Also, when you make a connection file, you usually dont need to tell the script that includes the connect file to use that mysql connection again, it knows!

When using " . $blah . ", you dont need to close it if you're not doing anything more to be outputting into the query, so its either "Hello {$name}" or "Hello " . $name; or "Hello " . $name . "";

The way you have coded is in my opinion, bad practice, learn to code badly, expect to keep coding badly.

For example, Im self taught in PHP, HTML, CSS, therefore my coding may not be as good, nor my knowledge be as good as someone whos been educated in it...

Use the connect files to define everything to do with the connection...

Lew.

badboyxlr
22-03-2011, 08:55 PM
i am also self taught, and the way you have put that i dont know what your getting at, i have put it like this so that each item of the script is apart, so they dont have to have 1 huge script were if they want to insert they get a load of errors.

---------- Post added 22-03-2011 at 08:56 PM ----------

yes i agree, but i put tutorial. Shame i cant edit it.. and do you always be so rude on posts, i am providing people with free snippets to copy and past, and all you do is criticize it?

Trinity
22-03-2011, 09:03 PM
i am also self taught, and the way you have put that i dont know what your getting at, i have put it like this so that each item of the script is apart, so they dont have to have 1 huge script were if they want to insert they get a load of errors.

---------- Post added 22-03-2011 at 08:56 PM ----------

yes i agree, but i put tutorial. Shame i cant edit it.. and do you always be so rude on posts, i am providing people with free snippets to copy and past, and all you do is criticize it?

I'm pretty sure we're all self taught here, it's no reason for us to use bad practices.

You're the only person that's been rude in this thread, we're just being honest so that people reading the thread understand that this isn't the way to do things, then they'll learn from other places and become PHP gods.
You can learn a lot on this forum too if you lose the attitude.

LMS16
22-03-2011, 09:06 PM
One of my favorite PHP lines is
<?=(($this=$that) ? 'This &amp; That' : 'That &amp; This')?>

You'll soon learn how to deal with "errors" in php ;)

Lew.

badboyxlr
22-03-2011, 09:08 PM
or, you could do it the easy way.. if (!$string){code for error}

LMS16
22-03-2011, 09:10 PM
or, you could do it the easy way.. if (!$string){code for error}

... Or<?php if($things) code to execute ?>

Lew.

badboyxlr
22-03-2011, 09:15 PM
Yes, you could do that. But that is more for JavaScript.

Trinity
22-03-2011, 09:28 PM
error_reporting(E_ALL);

For actual errors.

badboyxlr
22-03-2011, 09:30 PM
no, to show "all" errors.

LMS16
22-03-2011, 10:08 PM
Or, to fix all troubles, error_reporting(0);

Nuff said :)

Lew.

Source
23-03-2011, 01:17 AM
Oooooor spend less time bickering on forums and writing rather un-helpful tutorials and more time on learning more about the wonders of coding.

Learn about objects, programming structures, security, speed and just general awesome before you get ahead of yourself and start trying to teach people yourself. Normally I'd ignore a thread like this, but the arrogance you seem to be conveying on your extremely amateur coding knowledge, plus being rude to Trinity (<3)... is just out of place.

Spiffing.

badboyxlr
23-03-2011, 03:38 PM
*removed*

Edited by Trinity (Trialist Forum Moderator): Please do not be rude to other forum members/post to cause arguments.

Dentafrice
23-03-2011, 08:36 PM
@ the if statement.. You're saying that's easy because you don't understand the power of the ternary operator. Do you even know how to use it, it's GREAT for situations, and I use it daily.

Look bud, this isn't a tutorial, you're not helping people by posting code for people to "copy and paste" without even understanding what it is doing. It's different if you explain what mysql_query() does, how to handle errors from it, what it returns, etc.

It's great you're "trying" to help, but when someone who knows an extreme amount more than you criticizes you, take that criticism to heart and listen, not act all big and bad like you know it all, because as we can all tell from your code.. you don't have any experience in programming in general.

Edit: Just to prove that you are not an experienced programmer, because I have a feeling you're going to throw the "how do you know card", this is proof:



mysql_query("CREATE DATABASE ".$dbname.",$con)


If you were an experienced programmer you'd know that this is sort of ironic. You're using double quotes ("") which means that the string WILL be parsed by PHP, yet you're terminating and using concatenation to parse the string. You don't need to close the " and concatenate the string with double quotes.



mysql_query("CREATE DATABASE `{$dbname}`");


Using double quotes and parsing does use more memory than single quotes. Could argue minimal, but in a large program it adds up.

badboyxlr
26-03-2011, 08:27 PM
and if you were an experienced programmer, you would know that that is invalid syntax.

Trinity
26-03-2011, 09:03 PM
and if you were an experienced programmer, you would know that that is invalid syntax.

You're talking to Dentafrice, one of the most experienced and talented programmers that has ever blessed this forum with his presence. Do your research before you start casting aspersions.

badboyxlr
26-03-2011, 09:04 PM
urm, why talk you dont even know php gg.

Trinity
26-03-2011, 09:25 PM
urm, why talk you dont even know php gg.

Based on this thread and your other posts, I know a lot more than you. What would make you think I don't know PHP? I must contact all of my old freelancing clients and let them know that I don't actually know PHP, the work I gave them was actually coded by unicorns (or penguins, I like penguins).

Can't you just accept that other people know more than you and would be more than glad to help you if you lost the attitude?

badboyxlr
26-03-2011, 09:27 PM
you are very wrong, as my habbox posts are ****e, and habbox.. is ****e. Every body on habbox are rude ****es, like you. Thinking they know everything. When the truth is you want to think you know more then everybody, your like 25, get a life ..

Trinity
26-03-2011, 09:33 PM
you are very wrong, as my habbox posts are ****e, and habbox.. is ****e. Every body on habbox are rude ****es, like you. Thinking they know everything. When the truth is you want to think you know more then everybody, your like 25, get a life ..

Calm it with the attitude baby, we're all friends here, can't we just cuddle?
I don't think I know everything, nobody on here thinks they know everything (well, one exception, but he's awesome enough to get away with it).
I don't want to think I know more than everybody, that would be boring, I'd have nothing left to learn. I like to think that no matter how much I learn, I'm still rubbish compared to everyone else, it inspires me to work harder and learn more.
I'm not 25, I'm 18.

hamheyelliot
26-03-2011, 10:07 PM
you are very wrong, as my habbox posts are ****e, and habbox.. is ****e. Every body on habbox are rude ****es, like you. Thinking they know everything. When the truth is you want to think you know more then everybody, your like 25, get a life ..
To be honest, you come across as somebody who thinks that an exceptional bit of PHP knowledge (something I don't have) gives you some sort of power to cast an overruling decision over everyone.

Hell, if people had told me it wasn't a true tutorial I would have ticked over- but if somebody started using their so-called 'bragging rights' to comment on somebody's experience and skills like you did with no prior knowledge: it's a totally different level.

Having Tux as your avatar and arguing in code snippets is not going to make you come across any better a programmer. If you believe Habbox is **** and we're all really, really rude why don't you at least be the bigger person?

Source
27-03-2011, 02:38 AM
you are very wrong, as my habbox posts are ****e, and habbox.. is ****e. Every body on habbox are rude ****es, like you. Thinking they know everything. When the truth is you want to think you know more then everybody, your like 25, get a life ..

No-one is -trying- to come across as superior to you, people are just arguing their opinions that your coding knowledge isn't to the high level that you yourself think it to be. This 'heated discussion' started because of your serious attitude problem.

If you were genuinely being a nice contributing member than we would probably be sharing cucumber sandwiches watching Scrubs together - however you over react to all posts against you! You see them as a really deep personal attack when in reality they are merely frustrated responses (down to you arrogance) trying to give you some advice.

A few of us here actually earn a living from doing design and development, it makes sense to listen to those people and take it into consideration - even if you ultimately think they are totally incorrect. I don't know why I pop onto HxF as often as I do... no clue, probably because I love Caleb and Trinity. We don't bite, we just don't appreciate being spoken to in such a poor attitude. Surely at the end of the day, you want to learn how to make seriously cool ****.

Feel free to post code samples etc. of some more complex/structured code you've done if you really feel the need to prove your skills. But this isn't the place for such activities.

Dentafrice
28-03-2011, 03:22 AM
I love you Matt <3 and Trinity <3.

At OP: Haha, you think I don't know that missing a ; in the code example is a syntax error.. the post wasn't about the method call mysql_query(), it was about your method of concatenation since you're such the programmer.

We don't claim to know everything. Programming is such a diverse learning experience, that when you know one method of doing something.. there is ALWAYS a better method, and you never stop learning.

The fact is, there is some of us here that know quite a bit more than you. We've been together a while now. We've helped each other learn. We know more than you, that's proven.

If you want to improve, listen to us. We listen to each other when we need to improve.

It takes more than remembering syntax to become a programmer. It takes developmental and problem solving logic to create an efficient algorithm to make something work. Sure you can combine little bits and bombs to "rig" something together.. but until you finally understand HOW something works and WHY something works, you're nothing more than a snip-coder.. not a programmer.

<3 Java.



package Habbox;

public class BadProgrammingOP {
public static void main(String[] args) {
System.out.println("OP won't listen.");
System.out.println("Cya!");
System.out.println("Love ya matt and trinity <3");
}
}

LMS16
28-03-2011, 08:52 AM
I love you Matt <3 and Trinity <3.

At OP: Haha, you think I don't know that missing a ; in the code example is a syntax error.. the post wasn't about the method call mysql_query(), it was about your method of concatenation since you're such the programmer.

We don't claim to know everything. Programming is such a diverse learning experience, that when you know one method of doing something.. there is ALWAYS a better method, and you never stop learning.

The fact is, there is some of us here that know quite a bit more than you. We've been together a while now. We've helped each other learn. We know more than you, that's proven.

If you want to improve, listen to us. We listen to each other when we need to improve.

It takes more than remembering syntax to become a programmer. It takes developmental and problem solving logic to create an efficient algorithm to make something work. Sure you can combine little bits and bombs to "rig" something together.. but until you finally understand HOW something works and WHY something works, you're nothing more than a snip-coder.. not a programmer.

<3 Java.



package Habbox;

public class BadProgrammingOP {
public static void main(String[] args) {
System.out.println("OP won't listen.");
System.out.println("Cya!");
System.out.println("Love ya matt and trinity <3");
}
}


I like your reply! PHP & any other programming language is void if you dont understand how it works or how to use it correctly :)

Thread should now close :)

Lew.

Pegle
29-03-2011, 07:41 PM
This is NOT a tutorial. I want to learn PHP, this just forces me to search it up on the internet for what it actually does.

Want to hide these adverts? Register an account for free!