By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,747 Members | 2,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,747 IT Pros & Developers. It's quick & easy.

Getting results from MySQL Database in PHP

P: n/a
Hi all. I am trying to write a php page which connects to a MySQL
Database which is supposed to get the results from a table within a
database and display the results in a table.

Below is the code that I am using:

<?php
function connectDatabase() {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$db = @mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error
connecting to mysql');
echo 'connected';
$dbname = 'pmoneytest';
mysql_select_db($dbname);
}
echo '
<form action="#" method="POST" FormName="FormName">
<title>Test ResultSet Database</TITLE>
<BODY>
<B>Hello World</B><B><IHow are you today?</B></I>
</BODY>
';
connectDatabase();
echo "<table border = '1'>";
for ($x = 0; $x < $db->m_numRows; $x++) {
$row = $db->getRow();
echo
"<TR><TD>" . $row['first'] . "</td><td>";
}
?>

The problem that I am having is that it keeps on coming with the
following error and I can't find out how to fix the problem.
PHP Notice: Undefined variable: db in C:\Inetpub\wwwroot\PHPTest
\ShowDatabase.php on line 22 PHP Notice: Trying to get property of non-
object in C:\Inetpub\wwwroot\PHPTest\ShowDatabase.php on line 22.

It does say that it has connected to the MySQL Database but its the
problem with getting the results from the database.

Any help would be appreciated. Thanks very much

May 11 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
C.
On 11 May, 22:39, christopher_bo...@yahoo.co.uk wrote:
<?php
function connectDatabase() {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$db = @mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error
connecting to mysql');
echo 'connected';
$dbname = 'pmoneytest';
mysql_select_db($dbname);}

echo '
<form action="#" method="POST" FormName="FormName">
<title>Test ResultSet Database</TITLE>
<BODY>
<B>Hello World</B><B><IHow are you today?</B></I>
</BODY>
';

connectDatabase();
echo "<table border = '1'>";
for ($x = 0; $x < $db->m_numRows; $x++) {
$row = $db->getRow();
echo
"<TR><TD>" . $row['first'] . "</td><td>";
}

?>
$db is out of scope at the time of the call to getRow, you're mixing
up mysql and mysqli commands, and you've not executed any query on the
database. Throw this in the bin, RTFM and start again.

C.

May 11 '07 #2

P: n/a
On May 11, 5:39 pm, christopher_bo...@yahoo.co.uk wrote:
for ($x = 0; $x < $db->m_numRows; $x++) {
$row = $db->getRow();
The MySQL extension (the one you connect through) is not object-
oriented. You must have gotten the code I quoted above from an example
that uses MySQLi (improved), which was designed to connect to MySQL 5.
MySQLi is more fun to use, in my opinion, and should be easier to
learn. Actually, the example might have been using PDO MySQL, too.

Whether you are using MySQLi or not, you should not iterate through
returned rows using a for() loop like this because the row count MySQL
claims to have is not always accurate. Also, you didn't query the
database.

The code I quoted and everything after it should be replaced with
something like:

$results = mysql_query( 'SELECT * FROM `table_name_here`' );
while( $row = mysql_fetch_assoc( $results ) )
{
echo '<tr>';
foreach( $row as $item )
{
echo '<td>'.$item.'</td>';
}
echo '</tr>';
}
echo '</table>';
?>

....and you should fill in the name of the table you are getting data
from. All of the code after what I quoted got messy and didn't make
sense, so I changed it.

I think I should mention that you can just pass the strings right into
functions, you don't have to use
$host = ...
$username = ...
....
mysql_connect( $host, $username, ...

You can instead just put
mysql_connect( 'localhost', 'root', 'password' );

-Mike PII

May 11 '07 #3

P: n/a
Mike P2 wrote:
On May 11, 5:39 pm, christopher_bo...@yahoo.co.uk wrote:
>for ($x = 0; $x < $db->m_numRows; $x++) {
$row = $db->getRow();

The MySQL extension (the one you connect through) is not object-
oriented. You must have gotten the code I quoted above from an example
that uses MySQLi (improved), which was designed to connect to MySQL 5.
MySQLi is more fun to use, in my opinion, and should be easier to
learn. Actually, the example might have been using PDO MySQL, too.

Whether you are using MySQLi or not, you should not iterate through
returned rows using a for() loop like this because the row count MySQL
claims to have is not always accurate. Also, you didn't query the
database.

The code I quoted and everything after it should be replaced with
something like:

$results = mysql_query( 'SELECT * FROM `table_name_here`' );
while( $row = mysql_fetch_assoc( $results ) )
{
echo '<tr>';
foreach( $row as $item )
{
echo '<td>'.$item.'</td>';
}
echo '</tr>';
}
echo '</table>';
?>

...and you should fill in the name of the table you are getting data
from. All of the code after what I quoted got messy and didn't make
sense, so I changed it.

I think I should mention that you can just pass the strings right into
functions, you don't have to use
$host = ...
$username = ...
...
mysql_connect( $host, $username, ...

You can instead just put
mysql_connect( 'localhost', 'root', 'password' );

-Mike PII
Mike,

Actually, he is using the OO mysqli code. But he has multiple problem,
including:

1) he never issued a SELECT statement.
2) $db is out of scope where it is used
3) he's trying to mix mysql commands and mysqli objects

And once he gets these problems fixed he'll have more. He needs a new
book...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 12 '07 #4

P: n/a
On May 11, 10:28 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Mike,

Actually, he is using the OO mysqli code. But he has multiple problem,
including:

1) he never issued a SELECT statement.
2) $db is out of scope where it is used
3) he's trying to mix mysql commands and mysqli objects

And once he gets these problems fixed he'll have more. He needs a new
book...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
I realize that. I fixed those things in my example. The DB being out
of scope doesn't really matter, he's not passing $db to the MySQL
functions anyway. The connection is open, and PHP will find it.

I think that after looking at my example of how it should be, he
should be able to do some more research and/or figure out what he
needs to be doing.

-Mike PII

May 13 '07 #5

P: n/a
Mike P2 wrote:
On May 11, 10:28 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Mike,

Actually, he is using the OO mysqli code. But he has multiple problem,
including:

1) he never issued a SELECT statement.
2) $db is out of scope where it is used
3) he's trying to mix mysql commands and mysqli objects

And once he gets these problems fixed he'll have more. He needs a new
book...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

I realize that. I fixed those things in my example. The DB being out
of scope doesn't really matter, he's not passing $db to the MySQL
functions anyway. The connection is open, and PHP will find it.

I think that after looking at my example of how it should be, he
should be able to do some more research and/or figure out what he
needs to be doing.

-Mike PII
Mike,

But what you're doing is even worse - mixing OO and non-OO calls to the
database. Your mysql() calls will not update the variables in the
mysqli objects for instance.

You shouldn't try to mix and match mysql and mysqli code in the same
page - it's just asking for trouble.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 13 '07 #6

P: n/a
On May 12, 11:07 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Mike,

But what you're doing is even worse - mixing OO and non-OO calls to the
database. Your mysql() calls will not update the variables in the
mysqli objects for instance.

You shouldn't try to mix and match mysql and mysqli code in the same
page - it's just asking for trouble.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
I did not mix OO and non-OO calls.

-Mike PII

May 13 '07 #7

P: n/a
Mike P2 wrote:
On May 12, 11:07 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Mike,

But what you're doing is even worse - mixing OO and non-OO calls to the
database. Your mysql() calls will not update the variables in the
mysqli objects for instance.

You shouldn't try to mix and match mysql and mysqli code in the same
page - it's just asking for trouble.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

I did not mix OO and non-OO calls.

-Mike PII

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 14 '07 #8

P: n/a
Mike P2 wrote:
On May 12, 11:07 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Mike,

But what you're doing is even worse - mixing OO and non-OO calls to the
database. Your mysql() calls will not update the variables in the
mysqli objects for instance.

You shouldn't try to mix and match mysql and mysqli code in the same
page - it's just asking for trouble.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

I did not mix OO and non-OO calls.

-Mike PII
Oops - sorry for the blank reply.

Actually, you did. To quote your previous message:

"I realize that. I fixed those things in my example. The DB being out
of scope doesn't really matter, he's not passing $db to the MySQL
functions anyway. The connection is open, and PHP will find it."

$db is a mysqli (OO) object. You were making mysql_xx (non-OO) calls to
the database.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 14 '07 #9

P: n/a
On May 14, 10:45 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
$db is a mysqli (OO) object. You were making mysql_xx (non-OO) calls to
the database.
$db is not a mysqli (OO) object.

-Mike PII

May 14 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.