FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Mysql





lvideos
How to select and show all data from a mysql table in php?
coreymanshack
lvideos wrote:
How to select and show all data from a mysql table in php?


Code:
$con = mysql_connect(localhost, $password, $username);
mysql_select_db($dbname);
$result = mysql_query("SELECT * FROM `"$tablename"`");
while($row = mysql_fetch_array($result))
{
 echo $row['ColName'];
 echo $row['ColName2'];
}

//mysql_close($con);
//You don't need to explicitly close mysql connections they auto close when the php is done being //parsed


That's the basics code wasn't tested i may have left out a parenthese or semicolon. Smile
blueray
Sorry for said that.

On my suggestion you should close the mysql connection as quick as possibe.
As database connection is a limited and important resource.
This does't seems important on small web sites.
But on large web sites.
You can quickly see the different.

So, it should be a practice to do that.

Of course, it depends on the design of you script.

I'am assuming you are new to this.
You can implement it using a class so that you can just use a few statement to do all stuffs
such as connect, query, and close connect.

Code:

class db
{
    var $_db;
    function connect($host, $userid, $pass, $db)
    {
        // do stuffs mentioned in above post, such as
        $this->$_db = mysql_connect( ... )
        mysql_select_db( ... )
    }

    function query($sql)
    {
        // your sql query script goes here
    }

    function close()
    {
        if (this->$_db)
        {
             mysql_close($this->$_db);
        }
    }
}   

oly0015
If your new to the subject, at least for a start, you can read the information from w3schools on php that helps a bit for beginners. Although its not worth much, if you do finish there is a certificate you can gain from taking a test at the end.

http://www.w3schools.com/PHP/DEfaULT.asP

I hope this helps in your endeavors
coreymanshack
blueray wrote:
Sorry for said that.

On my suggestion you should close the mysql connection as quick as possibe.
As database connection is a limited and important resource.
This does't seems important on small web sites.
But on large web sites.
You can quickly see the different.

So, it should be a practice to do that.

Of course, it depends on the design of you script.

I'am assuming you are new to this.
You can implement it using a class so that you can just use a few statement to do all stuffs
such as connect, query, and close connect.

Code:

class db
{
    var $_db;
    function connect($host, $userid, $pass, $db)
    {
        // do stuffs mentioned in above post, such as
        $this->$_db = mysql_connect( ... )
        mysql_select_db( ... )
    }

    function query($sql)
    {
        // your sql query script goes here
    }

    function close()
    {
        if (this->$_db)
        {
             mysql_close($this->$_db);
        }
    }
}   



How do you see a difference in closing the connection as soon as possible? Connections are closed automatically when the script is done executing, why would you need to close?
oly0015
[/quote]

How do you see a difference in closing the connection as soon as possible? Connections are closed automatically when the script is done executing, why would you need to close?[/quote]


At least to try to back you up on this one for some proof:

http://php.net/manual/en/function.mysql-close.php
http://www.w3schools.com/PHP/func_mysql_close.asp

On both:
Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution. See also freeing resources.

I know at least for me, even though this is true, usually as a force of habit I still put that in on the end of all my connections.
badai
my C++ program run 24/7, i definitely need to use mysql_free_result() and mysql_close(). especially if i lock before read/insert. otherwise nobody can use the database.

it just a good idea to start doing everything in proper way, even if it's not necessary. i have a programmer here, damn good in web programming (javascript and php). since he's not very good in C++, he just do everything in php, and since we are doing "enterprise level" application involving table with millions of rows, the php script sometime run up to few days. and this guy was so used to working on php for web application that usually run in few seconds, he have no idea about closing the connection. so, needless to say, the first few weeks he work with us, we always find the tables are locked when we came in to work.

now you should start doing it properly, or you would get so used to bad programming habit that can cost money, time and make everybody hate you.
coreymanshack
badai wrote:
my C++ program run 24/7, i definitely need to use mysql_free_result() and mysql_close(). especially if i lock before read/insert. otherwise nobody can use the database.

it just a good idea to start doing everything in proper way, even if it's not necessary. i have a programmer here, damn good in web programming (javascript and php). since he's not very good in C++, he just do everything in php, and since we are doing "enterprise level" application involving table with millions of rows, the php script sometime run up to few days. and this guy was so used to working on php for web application that usually run in few seconds, he have no idea about closing the connection. so, needless to say, the first few weeks he work with us, we always find the tables are locked when we came in to work.

now you should start doing it properly, or you would get so used to bad programming habit that can cost money, time and make everybody hate you.


sure in C++ ....
badai
if you notice, i also wrote this:

Quote:
i have a programmer here, damn good in web programming (javascript and php). since he's not very good in C++, he just do everything in php, and since we are doing "enterprise level" application involving table with millions of rows, the php script sometime run up to few days. and this guy was so used to working on php for web application that usually run in few seconds, he have no idea about closing the connection. so, needless to say, the first few weeks he work with us, we always find the tables are locked when we came in to work.


that's what happen when you use PHP without closing the connection after query.

and if you really read like a programmer, you should notice that only 2 sentences are about C++, and the rest (minus 1 sentence) are about PHP.

and you didn't do work in proper order like a programmer should: reply to oly0015 first before reply to me.

so, you don't read like a programmer, and don't act like a programmer . i rest my case.
coreymanshack
badai wrote:
if you notice, i also wrote this:

Quote:
i have a programmer here, damn good in web programming (javascript and php). since he's not very good in C++, he just do everything in php, and since we are doing "enterprise level" application involving table with millions of rows, the php script sometime run up to few days. and this guy was so used to working on php for web application that usually run in few seconds, he have no idea about closing the connection. so, needless to say, the first few weeks he work with us, we always find the tables are locked when we came in to work.


that's what happen when you use PHP without closing the connection after query.

and if you really read like a programmer, you should notice that only 2 sentences are about C++, and the rest (minus 1 sentence) are about PHP.

and you didn't do work in proper order like a programmer should: reply to oly0015 first before reply to me.

so, you don't read like a programmer, and don't act like a programmer . i rest my case.


You didn't read a thing we wrote about mysql closing the connection automatically did you? Are your php scripts so long that it takes 1hr or more to run them so the tables stay locked? Sounds like you should be using something other than PHP to do what you are donig.
Quote:

and you didn't do work in proper order like a programmer should: reply to oly0015 first before reply to me.
so, you don't read like a programmer, and don't act like a programmer . i rest my case.

I didn't know you ever had a "case". Oh yeah, I'm not a programmer idk anything about php but yet you keep posting that your tables are locked when the php documentation clearly states how it treats mysql connections http://php.net/manual/en/function.mysql-close.php

Spam someone elses topic with your nonsense and quit trolling.



Edit: I misread what you wrote in bad english the first few times, I see now that what you are doing SHOULD be being done in a different language but yet the guy still does it in PHP (sounds like your company needs to hire someone else). Why was this even an argument? You can still read from tables while someone else is reading from them. Do you have to update your 1mil+ rows every day?
rvec
problem solved and people having an off-topic argument.... hmm let's -close-
Related topics
mysql connection question
php admin and mysql admin console
Mysql And PHP HELP PLZ
Do you use a CMS script?
Question about MySQL!
PHP, MySQL...
PHP Book (Php and Mysql for Dynamic Web Sites)
few mysql questions
E-Cards with PHP & MySQL
HTTP AUTH with PHP and mySQL
Build an online dictionary by PHP/MySQL
Make search engine With PHP and mySQL, for your site
PHP without mySQL
Multiple versions of PHP/MySQL
Help me to Import mysql databases from web into home pc !
This topic is locked: you cannot edit posts or make replies.    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.