some error, please help?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
johnjalani
Forum Newbie
Posts: 6
Joined: Tue Apr 01, 2008 5:45 am

some error, please help?

Post by johnjalani »

please help me, an error appear when i try to make a connection with my database,

connected to localhost via TCP/IP
Warning: mysqli::query() [function.mysqli-query]: Couldn't fetch mysqli in C:\wamp\www\GNSolutions\List.php on line 19

Fatal error: Call to a member function fetch_row() on a non-object in C:\wamp\www\GNSolutions\List.php on line 20

this is my code:
<?php
$conn = mysqli_connect("localhost","root","sa","gnsolutions");
if (empty($conn)) { die("mysqli_connect failed: " . mysqli_connect_error()); }
print "connected to " . mysqli_get_host_info ($conn) . "\n";
mysqli_close ($conn);

$result = $conn->query("SELECT productName FROM product");
while ($row = $result->fetch_row()) {
print $row[0] . "<br>\n";
}
$result->free();
$conn->close();
please help, SALAMAT PO,
Thanks
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: some error, please help?

Post by onion2k »

If you close the connection with mysqli_close ($conn); you're going to find it difficult to use it later.
gatekeeper
Forum Newbie
Posts: 2
Joined: Fri Apr 04, 2008 1:32 am

Re: some error, please help?

Post by gatekeeper »

HI there kababayan, seems you already closed the connection before actually fetching rows...

mysqli_close() is used to close a previously opened database connection.

Unless you have already fetched or is done fetching rows, you're not supposed to close connections yet.

your other line $conn->close() is another way to close the connection, you actually closed your connection twice.

This is how you should do it...

Code: Select all

 
<?php
$conn = mysqli_connect("localhost","root","sa","gnsolutions");
if (empty($conn)) { die("mysqli_connect failed: " . mysqli_connect_error()); }
print "connected to " . mysqli_get_host_info ($conn) . "\n";
 
$result = $conn->query("SELECT productName FROM product");
while ($row = $result->fetch_row()) {
print $row[0] . "<br>\n";
}
$result->free();
$conn->close();
 
just delete the line mysqli_close($conn);
Post Reply