Jump to content

mysql_fetch_assoc not returning records but mysqli_num_rows shows 3


jpeplinski

Recommended Posts

I just got everything setup with PHP, MySQL, and Apache.  I created a very simple test DB which I can connect to fine as well as execute a query against and get what seems to be some results back.  However, when doing the mysql_fetch_assoc, it returns an empty row (false).  On the echo below, I see "Number of rows is 3".  I see the rest of my echos writing out, but it never executes the echo in the while loop when retrieving the records from the resultset.  What am I doing wrong?  Here is the code:

 

<?php # Script 9.2 - mysqli_connect. php

 

// This file contains the database access information.

// This file also establishes a connection to MySQL,

// selects the database, and sets the encoding.

 

// Set the database access information as constants:

DEFINE ('DB_USER' , 'xout') ;

DEFINE ('DB_PASSWORD' , 'xout' ) ;

DEFINE ('DB_HOST' , 'xout' ) ;

DEFINE ('DB_NAME' , 'test') ;

 

echo ' <h1>starting script! </h1>';

// Make the connection:

$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)

OR die ( ' Could not connect to MySQL: ' . mysqli_connect_error() ) ;

 

echo ' <h1>got past the connection! </h1>';

$q = "SELECT * FROM t_schema.t_table";

$r = @mysqli_query ($dbc, $q); // Run the query and get a result-set.

echo ' <h1>Got past the query execution! </h1>';

 

if ($r) //If data in the result set

{ // If the query ran OK.

echo '<h1>Got some Results!</h1>'; 

$num = mysqli_num_rows($r) ;

echo '<h1>Number of rows is ' . $num . '</h1>';

 

while ($row = mysql_fetch_assoc ($r))

{

echo '<h1>Inside Loop</h1>';

}

} //end if ($r)

else

{

echo ' <h1>Error querying the data from the test DB! </h1>';

echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';

}

 

mysqli_close( $dbc) ;

//the end

?>

 

You can't mix mysql_* and mysqli_* extension functions. Use one or the other.

 

Also, you should be developing on a system with

error_reporting = -1 and display_errors = On

in your php.ini file.

OK, I turned on the errors in php.ini and restarted apache - are the errors written to a log somewhere or should they display with alert messages...Other?  Sorry, I am a bit new with php.

 

As a side note, I changed all the mysqli calls to mysql to be consistent.  However, I cannot get the page to load at all in Firefox when I do this.  the same is true if I change them to all mysqli calls.  However, If I leave it like I had it, it runs, however I get the weird situation described in the earlier post.  What is the difference between mysql and mysqli function calls?

 

Thanks!

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.