Jump to content

Warning: mysql_fetch_array(): supplied argument ...


ballhogjoni

Recommended Posts

can someone tell me why my api call is not working? I get this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in .../xxxxxx.php on line 14


[code]
<?php

$username="xxxxx";
$password="xxxxx";
$database="xxxxx";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$result = mysql_query("SELECT first, last, email, address, city, state, zipcode, phone FROM xxxxx ORDER BY id DESC");

$result2 = mysql_query("SELECT expyear, expmonth, ccnumber FROM xxxxx ORDER BY id DESC");

$result1 = mysql_fetch_array($result);
$result3 = mysql_fetch_array($result2);

mysql_close();

$phone = rawurlencode($result1['phone']);
$first = rawurlencode($result1['first']);
$last = rawurlencode($result1['last']);
$email = $result1['email'];
$address = rawurlencode($result1['address']);
$city = rawurlencode($result1['city']);
$state = $result1['state'];
$zipcode = rawurlencode($result1['zipcode']);
$ccnumber = rawurlencode($result3['ccnumber']);
$expyear = $result3['expyear'];
$expmonth = $result3['expmonth'];
$username = 'xxxxx';
$password = 'xxxxx';
$command = "user.configure";
$user_id = "NEW";
$url = "https://www.xxxxx.com/auto/api.php?partner_login=$username&partner_password=$password&test=TRUE&command=$command&user_id=$user_id&service=\"code=DS,frequency=M,action=add,renewal_billed_to=U,trial=14\"&output=php&phone=$phone&first_name=$first&last_name=$last&bill_address1=$address&bill_address2=&bill_city=$city&bill_state=$state&bill_postal=$zipcode&bill_country=US&username=foousername&password=foopassword&cc_num=$ccnumber&cc_exp=$expyear-$expmonth&email=$email";

$ch = curl_init();
curl_setopt( $ch , CURLOPT_URL , $url); //the url
curl_setopt($ch, CURLOPT_FAILONERROR, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable 
curl_setopt($ch, CURLOPT_TIMEOUT, 3); // times out after 4s 
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
$ret = curl_exec($ch);
curl_close($ch);

?>
[/code]
One method of checking if the query was successful:

[code]
$result2 = mysql_query("SELECT expyear, expmonth, ccnumber FROM xxxxx ORDER BY id DESC") or die(mysql_error());
$result2_num = mysql_num_rows($result2);

if ($result2_num > 0) {
$result3 = mysql_fetch_array($result2);
}

if ($result2_num < 1) {
echo "Unsuccessful";
}
[/code]
That will still throw errors if the query fails. The safest way to execute a query would be....

[code]
<?php
  $sql = "SELECT * FROM foo";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      // it is now safe to use your result in here.
    } else {
      echo "No records found";
    }
  } else {
    echo "Query failed ".mysql_error()."<br />$sql";
  }
?>
[/code]

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.