HP MySQL Array help

The error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in /home/rvathete/public_html/vdf/vdf.php on line 39

Line 39 is thhe fetch array; $data is defined in each conditional; however, is stating that the array is not defined; not sure if this is an improper method or i’ve been pounding away for too long…

code:
[php]<?php
$link = mysql_connect(“localhost”,“rvathete_test”,“vdftest”);
if (!link)
{die('Could not connect: ’ . mysql_error());}
mysql_select_db(“rvathete_test”, $link);
$lname = $_GET[‘Lastname’];
$fname = $_GET[‘firstname’];
$rank = $_GET[‘rank’];
// method one

if (isset($_GET[‘Lastname’])) {
if (empty($_GET[‘rank’]) && (empty($_GET[‘firstname’]))) {$data = (“SELECT * FROM test1 WHERE Lastname LIKE ‘$lname%’”);}
if (empty($_GET[‘rank’]) && (isset($_GET[‘firstname’]))) {$data = (“SELECT * FROM test1 WHERE Lastname LIKE ‘$lname%’ AND firstname LIKE ‘$lname%’”);}
if (empty($_GET[‘firstname’]) && (isset($_GET[‘rank’]))) {$data = (“SELECT * FROM test1 WHERE Lastname LIKE ‘$lname%’ AND rank LIKE ‘$rank%’”);}
if (isset($_GET[‘firstname’]) && (isset($_GET[‘rank’]))) {$data = (“SELECT * FROM test1 WHERE Lastname LIKE ‘$lname%’ AND rank LIKE ‘$rank%’ AND firstname LIKE ‘$fname%’”);}
}

if (isset($_GET[‘rank’])) {
if (empty($_GET[‘firstname’]) && (empty($_GET[‘Lastname’]))) {$data = (“SELECT * FROM test1 WHERE rank LIKE ‘$rank%’”);}
if (empty($_GET[‘firstname’]) && (isset($_GET[‘Lastname’]))) {$data = (“SELECT * FROM test1 WHERE rank LIKE ‘$rank%’ AND Lastname LIKE ‘$lname%’”);}
if (empty($_GET[‘Lastname’]) && (isset($_GET[‘firstname’]))) {$data = (“SELECT * FROM test1 WHERE rank LIKE ‘$rank%’ AND firstname LIKE ‘$fname%’”);} if (isset($_GET[‘firstname’]) && (isset($_GET[‘Lastname’]))) {(“SELECT * FROM test1 WHERE Lastname LIKE ‘$lname%’ AND rank LIKE ‘$rank%’ AND firstname LIKE ‘$fname%’”);}}

if (isset($_GET[‘firstname’])) {
if (empty($_GET[‘rank’]) && (empty($_GET[‘Lastname’]))) {$data = (“SELECT * FROM test1 WHERE firstname LIKE ‘$fname%’”);}
if (empty($_GET[‘rank’]) && (isset($_GET[‘Lastname’]))) {$data = (“SELECT * FROM test1 WHERE firstname LIKE ‘$fname%’ AND Lastname LIKE ‘$Lname%’”);}
if (isset($_GET[‘rank’]) && (empty($_GET[‘Lastname’]))) {$data = (“SELECT * FROM test1 WHERE firstname LIKE ‘$fname%’ AND rank LIKE ‘$rank%’”);}
if (isset($_GET[‘firstname’]) && (isset($_GET[‘Lastname’]))) {$data = (“SELECT * FROM test1 WHERE Lastname LIKE ‘$lname%’ AND rank LIKE ‘$rank%’ AND firstname LIKE ‘$fname%’”);}}

if (isset($_GET[‘rank’])) {
if (empty($_GET[‘firstname’]) && (empty($_GET[‘Lastname’]))) {$data = (“SELECT * FROM test1 WHERE rank LIKE ‘$rank%’”);}
if (empty($_GET[‘firstname’]) && (isset($_GET[‘Lastname’]))) {$data = (“SELECT * FROM test1 WHERE rank LIKE ‘$rank%’ AND Lastname LIKE ‘$lname%’”);}
if (empty($_GET[‘Lastname’]) && (isset($_GET[‘firstname’]))) {$data = (“SELECT * FROM test1 WHERE rank LIKE ‘$rank%’ AND firstname LIKE ‘$fname%’”);}
if (isset($_GET[‘firstname’]) && (isset($_GET[‘Lastname’]))) {$data = (“SELECT * FROM test1”);}}

if (empty($_GET[‘Lastname’])) {
if (empty($_GET[‘firstname’]) && (empty($_GET[‘rank’]))) {$data = (“SELECT * FROM test1”);}}

Print “

Last Name First Name Rank
”;
while ($info = mysql_fetch_array($data) or die(mysql_error()))
{
Print $info[‘Lastname’]. “
”. $info[‘firstname’]. “ ”. $info[‘rank’]. “
”;
}
Print “
”;
mysql_close($link);
?>[/php]

As the error said, that function requires a resource passed to it - you are setting $data as a string (the query). What you need to do is create a mysql result resource which you then pass to the mysql_fetch_array function. Simply add this line above the while loop:

[php]$data = mysql_query($data);[/php]

Thanks that solved the definition issue; however, I’ve found something else out; the only conditional that seems to pull the correct data is the GET rank argument; the other arguments are to pull their perspective information from the database; i’ve tried rewording the request and still no response; it apparently jumps down to the SELECT * FROM table; instead of doing the portions where the form fields for the variables are populated. For instance, I ask to find a record where lastname is Doe; it gives me the complete field; but if i ask it to give me records where rank is 1; it gives me the fields where that is found…

This is probably because you are using all ‘if’ statements and no ‘elseif’ statements. If the url has this: ‘?Lastname=’ then both of these statements will evaluate to true:

[php]if (isset($_GET[‘Lastname’])) {
if (empty($_GET[‘Lastname’])) {
[/php]

The if statements may well be setting the correct statement higher up, but if ‘Lastname’ is empty, it will get overwritten. Try rewriting the statements using elseifs or maybe try just changing the last one to being an ‘else if’

trying to find the rewrite that will work; thanks for your help; im going back to brainstorming something that will work.

Okay, I’m trying a new method for the data pull; however, I’m running into a T_string issue and cannot seem to find where it’s at; it’s related to line 15. I tried to slash out the quotations for the font however, the error still pops up.
[php]<?php
$link = mysql_connect(“localhost”,“","*”);
if (!link)
{die('Could not connect: ’ . mysql_error());}
mysql_select_db("******", $link);
$i=0;
if (!empty($request)) {$sql = “SELECT * FROM ***** WHERE ‘$search’=”$request"";}
else {$sql = “SELECT * FROM ****”;}

$result = mysql_query($sql);
$num=mysql_numrows($result);

print “

”;
while ($i < $num) {
$f1=mysql_result($result,$i,“field1”);
$f2=mysql_result($result,$i,“field2”);
$f3=mysql_result($result,$i,“field3”);
print "
" . $f1 . " " . $f2 . " " . $f3 . " " . $f4 . " " . $f5 . "
"; $i++; } mysql_close($link); ?>[/php]

Have you tried using single quotes to wrap the string rather than double quotes?

was resolved; just moved the styling into CSS. thanks.

Now I still have to figure out why the conditionals are over riding each other…

it’s running now; thanks for y’alls help.

Sponsor our Newsletter | Privacy Policy | Terms of Service