• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 613
  • Last Modified:

Where did I go wrong?

Warning: mysql_fetch_array() expects parameter 2 to be long, resource given in C:\wamp\www\ralph\connecttest.php on line 17

      $row1 = mysql_fetch_array(mysql_query("SELECT ip FROM whitelist", $link_resumedb), $link_resumedb);
0
RalphS007
Asked:
RalphS007
1 Solution
 
Chakravarthi AyyalaDatabase AdministratorCommented:
In the mysql_query("SELECT ip FROM whitelist", $link_resumedb), is it not just "mysql_query("SELECT ip FROM whitelist")" ?
0
 
Mattijs33Commented:
I expect the that $link_resumedb is the connection to your database.
The second argument in mysql_fetch_array is optional, but needs to ben an int.

PHP documentation
array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )

Open in new window

0
 
saimazzCommented:
in mysql query function $link_resumedb  is your link to MYSQL database.

in mysql_fetch_array function the second parameter need to be a constant, available options are : MYSQL_ASSOC - you will get associative indices, MYSQL_NUM you will get number indices, and MYSQL_BOTH you will get both
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
Beverley PortlockCommented:
The way you have written this leaves you open to other errors

      $row1 = mysql_fetch_array(mysql_query("SELECT ip FROM whitelist", $link_resumedb), $link_resumedb)

If anything goes wrong with the query it will return FALSE  which means that mysql_fetch_array will then throw an error similar to the one you are now getting. To be safe, rewrite your query as

      $rs = mysql_query("SELECT ip FROM whitelist", $link_resumedb);
      if ( $rs ) {
            // Query was successful

                  $row1 = mysql_fetch_array( $rs  );
                  .... more code...
      }

0
 
Ray PaseurCommented:
Compound statements like this one are guaranteed to cause you trouble.  Avoid them. Break your code out so you have no more than one function call per line.  Then the error messages that contain line numbers will have meaning.  Never construct your query in a function call.  Instead create a separate string variable.  Why? So that if the query fails, you will be able to print out the fully resolved string along with the error message.

The code snippet shows some of the basics about how to use PHP and MySQL.  The man page references are worth your time to read.  If, as you are reading them, you find that you have questions about how these functions work or how they are used together, please post a question here.  We have many experts who are very well-versed in the way these things work!

Read the code over and try to apply the principles to your script.  If you get stuck, post back with questions.

Best regards, ~Ray
<?php // RAY_mysql_example.php
error_reporting(E_ALL);


// THE ABSOLUTE MINIMUM YOU MUST UNDERSTAND TO USE PHP AND MYSQL
// MAN PAGE: http://php.net/manual/en/ref.mysql.php
// MAN PAGE: http://php.net/manual/en/mysql.installation.php
// MAN PAGE: http://php.net/manual/en/function.mysql-connect.php
// MAN PAGE: http://php.net/manual/en/function.mysql-select-db.php
// MAN PAGE: http://php.net/manual/en/function.mysql-real-escape-string.php
// MAN PAGE: http://php.net/manual/en/function.mysql-query.php
// MAN PAGE: http://php.net/manual/en/function.mysql-errno.php
// MAN PAGE: http://php.net/manual/en/function.mysql-error.php
// MAN PAGE: http://php.net/manual/en/function.mysql-num-rows.php
// MAN PAGE: http://php.net/manual/en/function.mysql-fetch-assoc.php
// MAN PAGE: http://php.net/manual/en/function.mysql-fetch-array.php
// MAN PAGE: http://php.net/manual/en/function.mysql-insert-id.php



// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";


// OPEN A CONNECTION TO THE DATA BASE SERVER
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}
// IF THE SCRIPT GETS THIS FAR IT CAN DO QUERIES




// ESCAPE AN EXTERNAL DATA FIELD FOR USE IN MYSQL QUERIES
$safe_username = mysql_real_escape_string($_POST["username"]);




// CREATE AND SEND A SELECT QUERY AND TEST THE RESULTS
$sql = "SELECT id FROM my_table WHERE username='$safe_username'";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, SHOW THE ERROR
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}
// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESOURCE-ID IN $res SO WE CAN NOW USE $res IN OTHER MYSQL FUNCTIONS




// DETERMINE HOW MANY ROWS OF RESULTS WE GOT
$num = mysql_num_rows($res);
if (!$num)
{
    echo "<br/>QUERY FOUND NO DATA: ";
    echo "<br/>$sql <br/>";
}
else
{
    echo "<br/>QUERY FOUND $num ROWS OF DATA ";
    echo "<br/>$sql <br/>";
}




// ITERATE OVER THE RESULTS SET TO SHOW WHAT WE FOUND
while ($row = mysql_fetch_assoc($res))
{
    var_dump($row);
}




// A WAY OF DETERMINING HOW MANY ROWS WE HAVE IN A TABLE
$sql = "SELECT COUNT(*) FROM my_table";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}
// GET THE RESULTS SET ROW IN AN ARRAY WITH A NUMERIC INDEX - POSITION ZERO IS THE COUNT
$row = mysql_fetch_array($res, MYSQL_NUM);
$num = $row[0];
$fmt = number_format($num);
echo "<br/>THERE ARE $fmt ROWS IN THE TABLE";




// MAKING AN INSERT QUERY AND TESTING THE RESULTS
$sql = "INSERT INTO my_table (username) VALUES ('$safe_username')";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}
// GET THE AUTO_INCREMENT ID OF THE RECORD JUST INSERTED - PER THE DB CONNECTION
$id  = mysql_insert_id($db_connection);
echo "<br/>YOU JUST INSERTED A RECORD WITH AUTO_INCREMENT ID = $id";

Open in new window

0
 
RalphS007Author Commented:
Revision:
require '../resumedb.php';	
	$link_resumedb = mysql_pconnect($hostname_resumedb, $username_resumedb, $password_resumedb) or trigger_error(mysql_error(),E_USER_ERROR);
	$calldb_resumedb = mysql_select_db($database_resumedb, $link_resumedb);
	$_REQUEST['id'] = $_SERVER['REMOTE_ADDR'];
	$query = "SELECT ip FROM whitelist";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);

Open in new window

0
 
RalphS007Author Commented:
output: Resource id #4Array
0
 
RalphS007Author Commented:
arning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\ralph\Connections\connection\master.php on line 13
Call Stack
#      Time      Memory      Function      Location
1      0.0006      367704      {main}( )      ..\connecttest.php:0
2      0.0014      374944      require( 'C:\wamp\www\ralph\Connections\connection\master.php' )      ..\connecttest.php:5
3      0.0025      375736      mysql_fetch_array ( )      ..\master.php:13

( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\ralph\Connections\connection\resume.php on line 13
Call Stack
#      Time      Memory      Function      Location
1      0.0006      367704      {main}( )      ..\connecttest.php:0
2      0.0034      376912      require( 'C:\wamp\www\ralph\Connections\connection\resume.php' )      ..\connecttest.php:9
3      0.0042      376944      mysql_fetch_array ( )      ..\resume.php:13
0
 
RalphS007Author Commented:
( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\ralph\Connections\connection\master.php on line 13
Call Stack
#      Time      Memory      Function      Location
1      0.0006      367704      {main}( )      ..\connecttest.php:0
2      0.0014      374944      require( 'C:\wamp\www\ralph\Connections\connection\master.php' )      ..\connecttest.php:5
3      0.0025      375736      mysql_fetch_array ( )      ..\master.php:13

( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\ralph\Connections\connection\resume.php on line 13
Call Stack
#      Time      Memory      Function      Location
1      0.0006      367704      {main}( )      ..\connecttest.php:0
2      0.0034      376912      require( 'C:\wamp\www\ralph\Connections\connection\resume.php' )      ..\connecttest.php:9
3      0.0042      376944      mysql_fetch_array ( )      ..\resume.php:13
0
 
Ray PaseurCommented:
Line 2
mysql_pconnect() -- do you know why you are using that instead of mysql_connect()?  If not, use mysql_connect().

Line 4
$_REQUEST is a superglobal variable that is set for your script by PHP before your script is started.  It is usually inadvisable to modify these arrays inside your script.

Line 6
Maybe you want to test the query for success before proceeding to use the $result variable. See lines 60-66 of my teaching example script above.

You would expect the value in $result to be either FALSE if the query failed or a Resource.  If it is a resource, you can use it in an iterator as shown in lines 89-92 of the teaching example.  MySQL_Fetch_Assoc() takes the Resource as input and returns an associative array as output.  The keys to the array are the column names.  In this case, each array that is returned from MySQL_Fetch_Assoc() will contain one key, ip, because that is all that was selected.  The values will be whatever is contained in the data base.  The order of the rows is, technically speaking, unpredictable because there is no ORDER BY clause, but it will probably return the rows in the same order that they were inserted into the table.
0
 
Ray PaseurCommented:
For better or worse computer programming is a fairly precise activity.  You posted a script with 7 lines and an error message coming from line 13.  We cannot work with contradictory information like that.  So buy this excellent little book and work through the exercises.  It will not make you a pro, but it is very readable with great examples.  It will help you get some of the foundation you need to use PHP and MySQL successfully.
http://www.sitepoint.com/books/phpmysql4/


I am going to sign off on the question now.  Best of luck with it, ~Ray
0
 
RalphS007Author Commented:
Basically this is what i want to get accomplished:

1. i have a myisam database with a whitelist table of ip addresses that will have access to a resume and portfolio i've put together.

2. i'm trying to get the ip validation to work properly.
0
 
RalphS007Author Commented:
I see that someone posted that they were confused regarding line 13 v. a 7 line post.  Here's my reply to that:

Connections/connection/master.php


 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php

		require 'auth_master.php';	
		
		$link_masterdb = mysql_pconnect($hostname_masterdb, $username_masterdb, $password_masterdb) or trigger_error(mysql_error(),E_USER_ERROR); 
		
		$calldb_masterdb = mysql_select_db($database_masterdb, $link_masterdb);
		
		$_REQUEST['id'] = $_SERVER['REMOTE_ADDR'];
		
		$query = "SELECT ip FROM whitelist";
		$result = mysql_query($query);
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		
		echo $result;
		echo $row;
	
?>

</body>
</html>

Open in new window


Connections/connection/resume.php

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php
		require '../auth/resume.php';	
		
		$link_resumedb = mysql_pconnect($hostname_masterdb, $username_masterdb, $password_masterdb) or trigger_error(mysql_error(),E_USER_ERROR); 
		
		$calldb_masterdb = mysql_select_db($database_resumedb, $link_resumedb);
		
		$_REQUEST['id'] = $_SERVER['REMOTE_ADDR'];
		
		$query = "SELECT ip FROM whitelist";
		$result = mysql_query($query);
		$row = mysql_fetch_array($result);
		
		echo $result;
		echo $row;
	
?>

</body>
</html>

Open in new window

0
 
RalphS007Author Commented:
the files in Connections/auth/ have the username, pass, db name, etc for each database in a separate file.
0
 
RalphS007Author Commented:
Dreamweaver generated the pconnect.  I'm not sure what the difference between connect and pconnect is.
0
 
jaxbrianCommented:
When you use pconnect it looks for "persistent" link. if your going to be using the same connection allot, you may want to use pconnect so that it doesn't have to open and then close the connection every time.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now