473,800 Members | 2,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class, Connecting to mysql

3 New Member
Hey, i have been programming php for a month now, well learning. And a friend asked me if i have done classes, as in class function etc and i said no. So he said its easy and that.
Spent awhile looking for help but no where actually gives you the info i need.
Anywayi have a class file called class.inc.php. And because i am using mysql i want a DB class.
Here is my code
[PHP]
<?
class db
{
var $db;
var $con;

function getdb()
{
$db['dbhost'] = 'localhost';
$db['dbusername'] = 'root';
$db['dbpassword'] = '';
$db['dbname'] = 'logg';
return $db;
}
function connect()
{
$host = $db['dbhost'];
$db = $db['dbname'];
$user = $db['dbusername'];
$pass = $db['dbpassword'];
mysql_connect($ host, $user, $pass);
mysql_select_db ($db);
return $con;
}
$db = new db();
?>[/PHP]
So my next file index.php basically shorten down to look like this
[PHP]
require('class. inc.php');
$db->con;
$result = mysql_query("SE LECT * FROM news");

while($row = mysql_fetch_arr ay($result))
{
echo $row['id'];
echo "<br />";
}
[/PHP]
And nothing appears, and i have 7 records and not even 1 shows.
This is the error that appears
Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localho st' (using password: NO) in C:\ini\htdocs\r yan\2\index.php on line 46

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\ini\htdocs\r yan\2\index.php on line 46

Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource in C:\ini\htdocs\r yan\2\index.php on line 48
Any help would be appreaciated alot! Thanks
Oct 31 '07 #1
3 1289
kenno66
3 New Member
Any help please???
I been looking everywhere for answers
Nov 1 '07 #2
jgentes
32 New Member
[PHP]
require('class. inc.php');
$db->con;

[/PHP]
And nothing appears, and i have 7 records and not even 1 shows.
This is the error that appears
Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localho st' (using password: NO) in C:\ini\htdocs\r yan\2\index.php on line 46

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\ini\htdocs\r yan\2\index.php on line 46

Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource in C:\ini\htdocs\r yan\2\index.php on line 48
Any help would be appreaciated alot! Thanks
you have 2 variables trying to do two multiple things...
scope wise this poses a problem, because you are declaring $db in the beginning, using it as 1 thing in your getdb() function than using it entirely different in your connect() function. if you had initiated them inside the functions it would be fine, but you are decarling it outside the function(s) thus giving it global scope to the class.

I would first just change db to dbname in the connect() function to take out any confusion on that.

then you also want to be careful of declaring db as the same name of the class. if you want the getdb() function to run every time the class is initiated, you can make it a __constructor.
Nov 1 '07 #3
amitabhcy
4 New Member
[PHP]
class db
{
var $db;
var $con;

function getdb()
{
$db['dbhost'] = 'localhost';
$db['dbusername'] = 'root';
$db['dbpassword'] = '';
$db['dbname'] = 'logg';
return $db;
}
function connect()
{
$host = $db['dbhost'];
$db = $db['dbname'];
$user = $db['dbusername'];
$pass = $db['dbpassword'];
mysql_connect($ host, $user, $pass);
mysql_select_db ($db);
return $con;
}
$db = new db();
?>[/PHP]
So my next file index.php basically shorten down to look like this
[PHP]
require('class. inc.php');
$db->con;
$result = mysql_query("SE LECT * FROM news");

while($row = mysql_fetch_arr ay($result))
{
echo $row['id'];
echo "<br />";
}
[/PHP]
Within the getdb function call $this->db instead of $db.
Nov 1 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
3639
by: Google Mike | last post by:
After a lot of thought and research, and playing with FreeTDS and InlineTDS, as well as various ODBC connections, I have determined that the fastest and cheapest way to get up and going with PHP on Linux, connecting to MS SQL Server, unless it was already pre-installed by your Linux installation, is to build your own multithreaded TCP socket server on Windows and connect to it through the socket API in PHP on Linux (if you have installed...
3
6175
by: kamilla | last post by:
I have a mysql 3.5 server installed on a suse linux 8.1, with address 10.0.0.100. Now I want to access that db from a W2K pc, address 10.0.0.200. I am able to ping 10.0.0.100, but I cannot connect to the db, and get error 2013. I have tried with MySQL Administrator 1.0 and also with ODBC. The db on linux has grant all on *.* to ''@'10.0.0.%' and also tried .... to root@10.0.0.200 and others seen on posted messages. I can access that db...
2
1774
by: news | last post by:
We currently have our mySQL server on the same box as the Apache server. For security and load balancing, we're going to be moving the mySQL server to another box. We're already using a single included connection file in all of our PHP pages that has the server, username, password line that connects to the database. Aside from changing "localhost" to the IP/port number of the new server, what else should be done, especially in the...
2
1557
by: Jim | last post by:
I'm writing an Invoicing Windows app but I'm writing it to make the code as easy to maintain as possible. Basically, to get any records from my DB, I use two classes: one that sets up the SQL statement, and another that makes the connection (plus the class that contains the windows form). Now, is this the best way to divide my tasks? And by using this structure, how would I be able to update my dataset when I don't have direct access to...
3
2248
by: Jeremy Dillinger | last post by:
I am trying to design a program that will use data from a MySQL database. Currently all the data is being used with PHP scripts from a website. I am also trying to build a software solution that can use the same data. I have gone through all the data connectors in Visual Basic.net and none of them have the options for connecting to MySQL. Does anybody know how I would go about doing this? Thanks in advance! Jeremy
1
2360
by: mm | last post by:
I have several korn shell scripts I use with a MySQL database on the same server (Solaris). I am moving to a Linux environment where the MySQL database is on another server. I would like to convert the database connection code to connect to the database on another Linux server. I see alot of information on Perl DBI CLI and PHP DBI CLI but cannot find any examples of Korn Shell connecting to a database on another server. Is there...
7
4066
by: Frances | last post by:
this seems pretty straight-forward.. got this code $link = mysql_connect('localhost:3306', '<uid>', '<pswd>'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); from here..
3
2707
by: Paradox Synthesist | last post by:
hi, i am a newbie to asp.net and have started learning mainly from websites. i have a question which IS very silly and trivial. http://msdn2.microsoft.com/en-us/library/system.data.odbc.odbcconnectionstringbuilder.dsn(VS.80).aspx i looked through this url to see how could i connect to a mysql database...where does the dsn figure out? why do i need to give it? i cant see any dsn specified anywhere on the database control panel. i am a...
0
2586
Dormilich
by: Dormilich | last post by:
Hi, I’m having a problem connecting to my local MySQL db. my PHP DB code throws the following Exception: I suspect the socket being wrong since all my MySQL GUI clients connect via /opt/local/var/run/mysql5/mysqld.sock. does anyone know, if that’s a correct assumption? thanks Dormi
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10279
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10255
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7582
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6815
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.