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
3 1289
Any help please???
I been looking everywhere for answers
[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.
[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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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
| |
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...
|
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..
|
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...
|
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
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| | |