LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-23-2009, 02:43 PM   #1
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , Solaris 10, RHEL
Posts: 1,935
Blog Entries: 1

Rep: Reputation: 188Reputation: 188
Advice for a "beginner" using PHP with MySQL


Hello All,

I'm a Sysadmin. I've been a sysadmin for about 6 years now. I manage various systems that include httpd, DNS, ftp, LDAP, Samba, MySQL/Postgers, etc. And not only do I do the systems, I'm also the network guy (I guess the company saves money that way ) that manages all the cisco devices and such.

I've been wanting to start learning PHP...but not only PHP, I would (eventually) want to get into writting web applications with a database backend...

As a sysadmin I write various scripts to automate tasks, so I'm familiar with "if" statements, loops, traps, etc. So I'm no stranger to the concept of programming. I just want to get more in depth.

Are there any books/resources that any of you can recommend? I am really interested in PHP with MySQL....

I found this book at Barnes and Noble...seemed good...and Amazon has some good reviews...

http://www.amazon.com/Head-First-MySQL-Lynn-Beighley/dp/0596006306/ref=cm_cr_pr_product_top

Have any of you read this book?

Any help/advice/thoughts/suggestions would be appreciated.

Thanks,

-C

Last edited by custangro; 05-23-2009 at 02:44 PM.
 
Old 05-23-2009, 03:11 PM   #2
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Rep: Reputation: 48
Hi,

I'm no PHP expert but I did get a connection going with a DB and have made my PHP script OOP, or at least I think it is

Here is a sample of what I did to get you started...


PHP Code:
<?php
/*
 * This Class will do all that's needed to connect and disconnect from the DB and return query results.
 */
class Db
{
    
/*** Put this in a seperate file ***/
    
private $_host "localhost";
    private 
$_user "jdoe";
    private 
$_pass "MyPass";
    private 
$_db "dev_accounting";
    public 
$result;
    
/*
     * Connect to the database server then to the database itsself
     */
    
private function ConnectToDatabase ()
    {
        
$link mysql_connect($this->_host$this->_user$this->_pass);
        if (! 
$link) {
            die(
'Could not connect to the server: ' mysql_error());
        }
        
$db_selected mysql_select_db($this->_db$link);
        if (! 
$db_selected) {
            die(
'Could not connect to the database"' $this->_db " get error: " mysql_error());
        }
    }
    
/*
     * Disconnect from the database and server.
     */
    
private function DisconnectFromDatabase ()
    {
        
mysql_close($link);
    }
    
/*
     * Open the database, run the SQL query, and put the result into an array then close database.
     */
    
public function ReturnSqlQuery ($sql)
    {
        
self::ConnectToDatabase();
        
$result mysql_query($sql);
        if (! 
$result) {
            
$message 'Invalid query: ' mysql_error() . "\n";
            
$message .= 'Whole query: ' $query;
            die(
$message);
        }
        
$rows = array();
        while (
$row mysql_fetch_assoc($result))
            
$rows[] = $row;
        
mysql_free_result($result);
        return 
$rows;
        
self::DisconnectFromDatabase();
    }
    
/*
     * TEST: Return the name of the fields mentioned in query
     */
    
public function ReturnFieldName ($sql)
    {
        
self::ConnectToDatabase();
        
$result mysql_query($sql);
        if (! 
$result) {
            
$message 'Invalid query: ' mysql_error() . "\n";
            
$message .= 'Whole query: ' $sql;
            die(
$message);
        }
        
$numberfields mysql_num_fields($result);
        
$rows = array();
        for (
$i 0$i $numberfields$i ++) {
            
$fields[] = mysql_field_name($result$i);
        }
        
mysql_free_result($result);
        return 
$fields;
        
self::DisconnectFromDatabase();
    }
}
?>
and a quick example of how I call it
PHP Code:
<?php
require_once 'classes/Db.php';
/*
 * Create Table to list all invoices
 */
$dbconn = new Db();
$query "
    SELECT id_invoice,coname,contype,sdate,pdate,value 
    FROM tbl_client AS tcl 
    JOIN tbl_invoice AS tin USING (id_client) 
    JOIN tbl_contract AS tco USING (id_contract) 
    ORDER BY sdate, id_invoice;"
;
$field $dbconn->ReturnFieldName($query);
$body $dbconn->ReturnSqlQuery($query);
?>
Hope this helps a bit.
 
Old 05-23-2009, 07:17 PM   #3
elby
LQ Newbie
 
Registered: May 2009
Distribution: RHEL 3
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by custangro View Post

Are there any books/resources that any of you can recommend? I am really interested in PHP with MySQL....
I've been a programmer for years. I'm not at all associated with O'Reilly publishers, but I like their books. I'm old school and need to read a book to learn something. They have a Learning PHP/MySql book. I however read through their Programming PHP book and was good to go from there (I was originally a C/C++ programmer). If you've been doing scripts, PHP is not so bad.

The best thing to do is have a project and learn PHP/MySQL while putting it together. I started with an idea for a website and tinkered with it until I got it right. PHP has well documented man pages at php.net.

I will say that it is important to understand basic database design though. I don't know what to recommend for that.
 
Old 05-23-2009, 09:04 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
As books go I liked the earlier edition of PHP and MySQL Web Development
 
Old 05-23-2009, 09:12 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Definitely worth bookmarking the relevant online docs:
http://dev.mysql.com/doc/refman/5.0/en/
http://www.php.net/manual/en/
 
Old 05-23-2009, 10:34 PM   #6
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , Solaris 10, RHEL
Posts: 1,935
Blog Entries: 1

Original Poster
Rep: Reputation: 188Reputation: 188
Quote:
Originally Posted by elby View Post
The best thing to do is have a project and learn PHP/MySQL while putting it together.
Actually that's the exact reason why I want to learn...I have an idea for a website...

Thanks to all for the references

-C
 
Old 08-14-2009, 03:17 PM   #7
wa3fkg
LQ Newbie
 
Registered: Jul 2005
Location: Oakmont, Pennsylvania
Distribution: SuSe 9.1 and Fedora Core 4
Posts: 8

Rep: Reputation: 0
Here is an additional beginners question. I am reading this thread because I Googeled "using PHP with MySQL" with the idea of looking for resources to do just that. I may however be putting the cart before the horse. What I want to do is create database systems on both Linux and Windows platforms and use a web browser as the "front end" to access the data. What I'm really looking for is a replacement for Microsoft Access. I want to create applications for churches and other non profit organizations. These applications do not necessarily need to have access via the Internet rather simply over the LAN.

Having said that am I looking at the wrong language for the purpose? Is PHP flexible enough to make a complete client side application for a database used in an office situation? It seems as though C or C++ is the language more oriented for these tasks but it also looks like a much steeper learning curve and work to create the end product entirely in either language. I'm not looking to make a living do this but would be interested in what the people who do are using for the job.
 
Old 08-14-2009, 03:33 PM   #8
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,410

Rep: Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531
Quote:
Originally Posted by wa3fkg View Post
Here is an additional beginners question. I am reading this thread because I Googeled "using PHP with MySQL" with the idea of looking for resources to do just that. I may however be putting the cart before the horse. What I want to do is create database systems on both Linux and Windows platforms and use a web browser as the "front end" to access the data. What I'm really looking for is a replacement for Microsoft Access. I want to create applications for churches and other non profit organizations. These applications do not necessarily need to have access via the Internet rather simply over the LAN.

Having said that am I looking at the wrong language for the purpose? Is PHP flexible enough to make a complete client side application for a database used in an office situation? It seems as though C or C++ is the language more oriented for these tasks but it also looks like a much steeper learning curve and work to create the end product entirely in either language. I'm not looking to make a living do this but would be interested in what the people who do are using for the job.
Well, you say 'client side', but also say "web browser as the front end". The two are different. A real client/server application *COULD* be written in PHP, but it's not idea. C/C++ for a client app, that goes out and interfaces with a database, is client/server. The app you write is independent of any other software/browser/etc....just needs your DB somewhere to read.

Web based is client-only...the web server handles interaction with the database, and uses a browser to present it to any user, any platform, providing the browser does things correctly (which leaves out IE ). In that case, PHP is ideal...I've developed several apps using it, and it's VERY robust, and hooks in with several different flavors of database (MySQL, Postgres, Oracle..). You could easily have a web and database server on the same box, if you didn't need to serve up thousands of users at once, and weren't pounding the database.

And please start your own thread for questions, rather than hijacking someone elses....
 
Old 08-15-2009, 08:58 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
To answer your specific question:
Quote:
Is PHP flexible enough to make a complete client side application for a database used in an office situation?
Then yes it most certainly is.

Is what you are proposing reasonable? I would say yes, PHP is fairly easy to learn and setting up a client server style architecture via an Intranet is much quicker than having to do it all yourself.

MySQL is a good choice of database, there are lots of tutorials out there to help with learning to set that up.

From the front end perspective are there any alternative choices? Certainly, they come down (I would say) into three groups.
  • Compiled programming language (C/C++ Java etc.) and I would suggest that is overkill for your expressed needs;
  • Scripting languages, there are lots of them, of which PHP is one. This is a good choice but there is also;
  • Database front end tools like MS Access, there are several around OO base is an offering from Open Office and provides a link to MySQL and cross platform but there are others. If you are familiar with MS Access then you may want to research this idea further.
 
  


Reply

Tags
class, mysql, php


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
yum install php-mysql fails with mysql 5.1 - "Error: mysql conflicts with MySQL" rebelde Linux - Software 2 03-13-2009 10:32 AM
which is better "mysql" or "mysqli" to use in php 5.2.6 !? robertjinx Linux - Server 1 12-07-2008 08:32 AM
"Out the box" security of Apache, PHP and MySQL on FC6 lostjohnny Linux - Security 3 05-09-2007 04:15 AM
MySQL/PHP/Horde "Failed to write session data" Big Money Linux - Software 0 11-23-2004 01:33 PM
Php or Mysql upgrade on Fedora Core 2 "Uptodate" Program geninblaze Fedora 1 07-12-2004 09:56 PM


All times are GMT -5. The time now is 11:03 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration