473,320 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Class, Connecting to mysql

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("SELECT * FROM news");

while($row = mysql_fetch_array($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'@'localhost' (using password: NO) in C:\ini\htdocs\ryan\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\ryan\2\index.php on line 46

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\ini\htdocs\ryan\2\index.php on line 48
Any help would be appreaciated alot! Thanks
Oct 31 '07 #1
3 1270
Any help please???
I been looking everywhere for answers
Nov 1 '07 #2
jgentes
32
[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'@'localhost' (using password: NO) in C:\ini\htdocs\ryan\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\ryan\2\index.php on line 46

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\ini\htdocs\ryan\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
[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("SELECT * FROM news");

while($row = mysql_fetch_array($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
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...
3
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...
2
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...
2
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...
3
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...
1
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...
7
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...
3
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. ...
0
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.