Hello;

I am working on a project and in order to save resources, my supervisor says I should pass variables in an array. Some of which were working perfectly. However the fetching of the records from the database gives me the error below;

Warning: mysql_query() expects parameter 2 to be resource, string given in /nfs/c01/h12/mnt/3250/domains/zeta-clovis.sendairtime.com/html/support_mail/includes/find_order_records_function.php on line 37

The find_order_records_function.php file is as below;

<?php
// Fetch the functions file that contains the database connection
include_once $_SERVER['DOCUMENT_ROOT'].'/src/dbInfo.php';

// Run a query to fetch the records from the database table using a function
function fetch_records($input_array) 
{   
    // Connect to the database using the function connect2DB2
    $input_array['connection'] = connect2DB2(COUNTRY_ABBREVIATION);
    $connection = $input_array['connection'];

    $get_record_query = "SELECT * FROM Ordered_Cart_Items";
    $execute_get_record = mysql_query($get_record_query,$connection) or handleDatabaseError(''. mysql_error(),$get_record_query);

//Check whether there are any rows being returned by the query 
    if (mysql_num_rows($execute_get_record) == 0) {
        $table = "No record returned";

        $input_array['table'] = $table;
        return $input_array;
    } else {
        $table = "
        These are the Items that are in the ordered_cart_items database table. Cross check these records with the report from Paypal and Yo payements.
        <table border='0'>
        <tr bgcolor='#999999'>
        <td>Order Id</td>
        <td>Session Id</td>
        <td>Date added</td>
        <td>Receiver</td>
        <td>Receiver phone</td>
        <td>Reciever email</td>
        </tr>";

        while ($returned_records = mysql_fetch_array($execute_get_record)) {
            $order_id = $returned_records['order_id'];
            $client_session_id = $returned_records['session_id'];
            $date_added = $returned_records['date_added'];
            $receiver_name = $returned_records['receiver_first_name'] . " " . $returned_records['receiver_last_name'];
            $receiver_phone = $returned_records['receiver_phone'];
            $receiver_email = $returned_records['receiver_email'];
            $receiver_network_id = $returned_records['receiver_network_id'];
            $item_id = $returned_records['item_id'];
            $button_id = $returned_records['button_id'];
            $paypal_fee = $returned_records['paypal_fee'];
            $ip_country = $returned_records['ip_country'];
            $ip_state = $returned_records['ip_state'];
            $ip_city = $returned_records['ip_city'];
            $ip_address = $returned_records['ip_address'];
            $note_to_receiver = $returned_records['note_to_receiver'];

            $table.="
        <tr>
        <td>$order_id</td>
        <td>$client_session_id</td>     
        <td>$date_added</td>    
        <td>$reciever_name</td>
        <td>$receiver_phone</td>
        <td>$receiver_email</td>
        </tr>";
        }
        $table.= "</table>";

        $input_array['table'] = $table;
        return $input_array;
    }
    echo $table;
}

?>

I dunno. Is it something that I may have missed out?? Pliz help

probably the problem is here:
$connection = $input_array['connection'];

check the variable type with: var_dump($connection);

it should be resource, not string

I checked it, and it shows me the message;

resource(13) of type (mysql link persistent) string(1) "R"

Dunno how to go about it.

Well there certainly seems to be no error on line 37 of the script you presented to us. Could you echo your query and post it here? (E.g. $query = [your query goes here]; echo $query; )

try

echo (is_resource($connection)) ? get_resource_type($connection) : 'not a resource';

@Evolution, I tried out

echo (is_resource($connection)) ? get_resource_type($connection) : 'not a resource';

But I got the message below;
mysql link persistent
Warning: mysql_query() expects parameter 2 to be resource, string given in /nfs/c01/h12/mnt/3250/domains/zeta-clovis.sendairtime.com/html/support_mail/includes/find_order_records_function.php on line 37

@minitauros, when I tried to echo the query, I got this message;

Resource id #14
Warning: mysql_query() expects parameter 2 to be resource, string given in /nfs/c01/h12/mnt/3250/domains/zeta-clovis.sendairtime.com/html/support_mail/includes/find_order_records_function.php on line 37

@minitauros, when I tried to echo the query, I got this message;
Resource id #14
Warning: mysql_query() expects parameter 2 to be resource, string given in /nfs/c01/h12/mnt/3250/domains/zeta-clovis.sendairtime.com/html/support_mail/includes/find_order_records_function.php on line 37

I meant echoing your query statement ^^. The command that you're giving to your MySQL. The query, like "SELECT FROM...". If you have that we can take a look and see if there's something wrong with it.

Also, what exactly is on line 37? Because in your starting post, on line 37 is:

$date_added = $returned_records['date_added'];

Which does not even look like a mysql_query() command :).

I think dorco has a point. The problem is with the connection string.

My query is;

$get_record_query = "SELECT * FROM Ordered_Cart_Items";

just as seen in line 12.
As for the error, the line 37 on my script is the line that has;

$execute_get_record = mysql_query($get_record_query,$connection) or handleDatabaseError(''. mysql_error(),$get_record_query);

If your server says

Warning: mysql_query() expects parameter 2 to be resource, string given in /nfs/c01/h12/mnt/3250/domains/zeta-clovis.sendairtime.com/html/support_mail/includes/find_order_records_function.php on line 37

about this line:

mysql_query($get_record_query,$connection)

then it means that $connection contains a string, while it should contain a resource, which means that your connect2DB2() function should be returning a resource (it should return the value of your mysql_connect() function). Did you check if it does? :) For there seems to be nothing wrong with your query.

And this is indeed what dorco and evolutionfallen already mentioned, sorry guys! Wasn't paying enough attention.

Ok, lemme first test whather the connection is being made.

Thanks for your input.

Ok, so I have been able to check whether the connection is a resource; and now the error message is gone. However, the code is now displaying no records. My current code is as follows;

<?php
// Fetch the functions file that contains the database connection
include_once $_SERVER['DOCUMENT_ROOT'].'/src/dbInfo.php';

// Run a query to fetch the records from the database table using a function
function fetch_records($input_array) 
{   
    // Connect to the database using the function connect2DB2
    $input_array['connection'] = connect2DB2(COUNTRY_ABBREVIATION);
    $connection = $input_array['connection'];

    if(is_resource($connection))    
    {
    $get_record_query = "SELECT * FROM Ordered_Cart_Items";
    $execute_get_record = mysql_query($get_record_query,$connection) or handleDatabaseError(''. mysql_error(),$get_record_query);

//Check whether there are any rows being returned by the query 
    if (mysql_num_rows($execute_get_record) == 0) {
        $table = "No record returned";

        $input_array['table'] = $table;
        return $input_array;
    } else {
        $table = "
        These are the Items that are in the ordered_cart_items database table. Cross check these records with the report from Paypal and Yo payements.
        <table border='0'>
        <tr bgcolor='#999999'>
        <td>Order Id</td>
        <td>Session Id</td>
        <td>Date added</td>
        <td>Receiver</td>
        <td>Receiver phone</td>
        <td>Reciever email</td>
        </tr>";

        while ($returned_records = mysql_fetch_array($execute_get_record)) {
            $order_id = $returned_records['order_id'];
            $client_session_id = $returned_records['session_id'];
            $date_added = $returned_records['date_added'];
            $receiver_name = $returned_records['receiver_first_name'] . " " . $returned_records['receiver_last_name'];
            $receiver_phone = $returned_records['receiver_phone'];
            $receiver_email = $returned_records['receiver_email'];
            $receiver_network_id = $returned_records['receiver_network_id'];
            $item_id = $returned_records['item_id'];
            $button_id = $returned_records['button_id'];
            $paypal_fee = $returned_records['paypal_fee'];
            $ip_country = $returned_records['ip_country'];
            $ip_state = $returned_records['ip_state'];
            $ip_city = $returned_records['ip_city'];
            $ip_address = $returned_records['ip_address'];
            $note_to_receiver = $returned_records['note_to_receiver'];

            $table.="
        <tr>
        <td>$order_id</td>
        <td>$client_session_id</td>     
        <td>$date_added</td>    
        <td>$reciever_name</td>
        <td>$receiver_phone</td>
        <td>$receiver_email</td>
        </tr>";
        }
        $table.= "</table>";

        $input_array['table'] = $table;
        return $input_array;
    }
    echo $table;
    }
    else
    {
        echo "An error occured";
        }
}

?>

Hmm well what do you get if you open your PHPMyAdmin and execute the following query?

SELECT * FROM Ordered_Cart_Items

If you do not get results, either your table may be empty or you might have miswritten your query.

@minitauros, when i run the query in my PHPMyAdmin, it returns values.

Perhaps, I am just tired and not seeing things ....

Maybe you could try and see what happens if you remove this check

mysql_num_rows($execute_get_record) == 0

from your file. So just fetch the results without checking if there are any. I don't usually use mysql_num_row, but maybe there's a problem with a comparison to 0 (I wouldn't know why but that's what I would try if I were you :))

However, the code is now displaying no records.

Is your output printing out "No record returned", or are you not seeing anything at all?

it is outputing "No record returned". However, the error is being displayed. so I dunno wat to do now.

@Minitaurus, The checking works fine, but my main worry is the error that is being displayed ...

Hm which error is being displayed right now, then? Not the same as before right? Sounded like you fixed that :). Is there a new one?

could you display the error? seems to be mysql_connect is missing the parameters

The error is;

Warning: mysql_query() expects parameter 2 to be resource, string given in /nfs/c01/h12/mnt/3250/domains/zeta-clovis.sendairtime.com/html/support_mail/includes/find_order_records_function.php on line 37

And line 37 contains this code;

  $execute_get_record = mysql_query($get_record_query,$connection) or handleDatabaseError(''. mysql_error(),$get_record_query);

@Minitaurus, it is still the same error, but the table is being displayed as well

kindly echo these 2 varialbes seperately $get_record_query,$connection

@masterjiraya, the $get_record_query is

$get_record_query = "SELECT * FROM Ordered_Cart_Items";

and $connection is;

$connection = MYSQL_CONNECT("localhost","root","password");

So, I have been able to get rid of the error, however this has only worked after I used the array itself in and not the variable. Here is the code;

<?php     
// Connect to the database
$input_array['connection'] = connect2DB2(COUNTRY_ABBREVIATION);
$connection = $input_array['connection'];

if(!is_resource($input_array['connection']))
{
    echo "The connection string is not a valid mysql resource";
}
else
{
// Run a query to fetch the records from the database table using a function
function fetch_records($input_array) 
{
    $get_record_query = "SELECT * FROM Ordered_Cart_Items";
    $execute_get_record = mysql_query($get_record_query,$input_array['connection']) or handleDatabaseError(''. mysql_error(),$get_record_query);

//Check whether there are any rows being returned by the query 
    if (mysql_num_rows($execute_get_record) == 0) {
        $table = "No record returned";

        $input_array['table'] = $table;
        $input_array = $input_array['table'];
        echo $input_array;
    } else {
        $table = "
        These are the Items that are in the ordered_cart_items database table. Cross check these records with the report from Paypal and Yo payements.
        <table border='0'>
        <tr bgcolor='#999999'>
        <td>Order Id</td>
        <td>Session Id</td>
        <td>Date added</td>
        <td>Receiver</td>
        <td>Receiver phone</td>
        <td>Reciever email</td>
        </tr>";

        while ($returned_records = mysql_fetch_array($execute_get_record)) {
            $order_id = $returned_records['order_id'];
            $client_session_id = $returned_records['session_id'];
            $date_added = $returned_records['date_added'];
            $receiver_name = $returned_records['receiver_first_name'] . " " . $returned_records['receiver_last_name'];
            $receiver_phone = $returned_records['receiver_phone'];
            $receiver_email = $returned_records['receiver_email'];
            $receiver_network_id = $returned_records['receiver_network_id'];
            $item_id = $returned_records['item_id'];
            $button_id = $returned_records['button_id'];
            $paypal_fee = $returned_records['paypal_fee'];
            $ip_country = $returned_records['ip_country'];
            $ip_state = $returned_records['ip_state'];
            $ip_city = $returned_records['ip_city'];
            $ip_address = $returned_records['ip_address'];
            $note_to_receiver = $returned_records['note_to_receiver'];

            $table.="
        <tr>
        <td>$order_id</td>
        <td>$client_session_id</td>     
        <td>$date_added</td>    
        <td>$reciever_name</td>
        <td>$receiver_phone</td>
        <td>$receiver_email</td>
        </tr>";
        }
        $table.= "</table>";

        $input_array['table'] = $table;
        $input_array = $input_array['table'];
        echo $input_array;
    }
   disconnectDB($connection);
}
}
?>

Thank you all for your contributions. You've been so helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.