Page 1 of 1

some error, please help?

Posted: Fri Apr 04, 2008 2:56 am
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

Re: some error, please help?

Posted: Fri Apr 04, 2008 3:07 am
by onion2k
If you close the connection with mysqli_close ($conn); you're going to find it difficult to use it later.

Re: some error, please help?

Posted: Fri Apr 04, 2008 3:15 am
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);