LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Advice for a "beginner" using PHP with MySQL (https://www.linuxquestions.org/questions/programming-9/advice-for-a-beginner-using-php-with-mysql-728050/)

custangro 05-23-2009 02:43 PM

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

eco 05-23-2009 03:11 PM

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.

elby 05-23-2009 07:17 PM

Quote:

Originally Posted by custangro (Post 3550490)

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.

graemef 05-23-2009 09:04 PM

As books go I liked the earlier edition of PHP and MySQL Web Development

chrism01 05-23-2009 09:12 PM

Definitely worth bookmarking the relevant online docs:
http://dev.mysql.com/doc/refman/5.0/en/
http://www.php.net/manual/en/

custangro 05-23-2009 10:34 PM

Quote:

Originally Posted by elby (Post 3550622)
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 :D

-C

wa3fkg 08-14-2009 03:17 PM

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.

TB0ne 08-14-2009 03:33 PM

Quote:

Originally Posted by wa3fkg (Post 3643605)
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....

graemef 08-15-2009 08:58 PM

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.


All times are GMT -5. The time now is 12:05 AM.