I have been so busy lately with school and everything. Not like anyone reads this but it's kinda for me haha.
I want to post up more things but I am very busy so if you by some strange phenomena read this I will try to post more things as soon as I get some time!
Sunday, September 14, 2008
Friday, May 30, 2008
Database! Grabbing data with php.
I have worked a lot with php and databases so I thought I would write a quick article on how to pull out data and display it on your page.
For the purposes of this post we will do a simple one table database. The database holds a user_id and a name.
So one record would be like this user_id = 1 name= Brent.
So now that we have a database set up I am going to focus on the just getting the data out, if you want me to explain how to connect to the database and all that jazz just let me know and I will write it up.
First, in order to get the data we have to write the query:
$sql = "select * from user";
The $sql variable now holds our query.
To execute the query you use mysql_query();
$results = mysql_query($sql);
This runs our $sql query and puts the results in a mysql resource called $results.
Now, we need to get out all that data with a loop. I use a while loop most of the time because it seems to work just fine for me. Right now the $results variable is a mysql resource so we need to make it an array. So all in one step I am going to put the data into an array and use the while loop to pull it out and display it on the page.
while($row = mysql_fetch_array($results)){
$name = $row['name'];
$user_id = $row['user_id'];
echo "Name is ".$name." User Id is ".$user_id."Put a line Break here!"
}
This first grabs the mysql resource into an array called $row then I grab the name and user_id out of the array and echo them out to the page.
It is a simple as that!
Please email me with any questions!
Database:
For the purposes of this post we will do a simple one table database. The database holds a user_id and a name.
So one record would be like this user_id = 1 name= Brent.
So now that we have a database set up I am going to focus on the just getting the data out, if you want me to explain how to connect to the database and all that jazz just let me know and I will write it up.
PHP:
First, in order to get the data we have to write the query:
$sql = "select * from user";
The $sql variable now holds our query.
To execute the query you use mysql_query();
$results = mysql_query($sql);
This runs our $sql query and puts the results in a mysql resource called $results.
Now, we need to get out all that data with a loop. I use a while loop most of the time because it seems to work just fine for me. Right now the $results variable is a mysql resource so we need to make it an array. So all in one step I am going to put the data into an array and use the while loop to pull it out and display it on the page.
while($row = mysql_fetch_array($results)){
$name = $row['name'];
$user_id = $row['user_id'];
echo "Name is ".$name." User Id is ".$user_id."Put a line Break here!"
}
This first grabs the mysql resource into an array called $row then I grab the name and user_id out of the array and echo them out to the page.
It is a simple as that!
Please email me with any questions!
Thursday, May 22, 2008
foreach loop
I have taken other programming classes in school like java which uses a for loop. So when I started learning php I had a little trouble understanding a use for the foreach loop, so I thought I would describe a good use for it.
Let's say you have an array of many things like this:
Let's say you have an array of many things like this:
$array = array("dog","cat","giraffe","bear");
You can use a foreach to very easily pull all of these out and display them. Here is how I do it, feel free to leave a comment with a different way because we all know there are thousands of ways to do things in programming.
Because we have saved our array in a variable called $array this is how we would do the foreach loop.
foreach($array as $animal){
echo $animal."\n";
}
Normally, I would use a br tag to break a line, but blogger picks it up and actually does a line break instead of just showing the text. So pretend like the \n is a br tag.
Explanation:
Now let me explain what this is doing:
First, for every element in the array we assign it to animal so the first time it runs $animal is "dog" so it echoes our "dog" then gives a line break. It repeats this until it echoes all the elements in the array.
You can also use a foreach loop to run through xml documents, objects, and all kinds of other neat things you need to go through to pull out data from any source.
If you have any questions or just want to know more about foreach loops please shoot me an email or leave a comment!
echo "hello world!";
I have began reading many books on php, and have become very interested in this language.
My plan is to post things that have to do with php such as neat problem solving techniques, or just cool things that I come across while learning this language.
Please feel free to comment and add anything to my ongoing discussion of php.
My plan is to post things that have to do with php such as neat problem solving techniques, or just cool things that I come across while learning this language.
Please feel free to comment and add anything to my ongoing discussion of php.
Tuesday, January 01, 2008
Wednesday, November 28, 2007
So I am just sitting around tonight reading my normal engadget, gizmodo, google blogs, etc. When it hits me, Google is ridiculous. How amazing has google made the internet. I use gmail, google calendar for my schedule, google docs for note taking, this blog to blog, google reader for reading all the sites I am interested in, and the list goes on and on. I would love to one day work for Google, even though it is near impossible, just because the innovation never stops, and I would love to be a part of it. So in short Google is just amazing and I dont know what the internet would be without it, other than crappy.
Monday, November 26, 2007
Starting a Blog
So apparently this blogging thing is all the rage among many people, so I thought I would give it a shot.
Hopefully, I can find something cool to post about or I guess just what is going on in my life.
Stay Tuned
Brent
Hopefully, I can find something cool to post about or I guess just what is going on in my life.
Stay Tuned
Brent
Subscribe to:
Posts (Atom)
