mysql_fetch_array(): supplied argument is not a valid MySQL result resource

I've been getting this error below:

mysql_fetch_array(): supplied argument is not a valid MySQL result
resource

When running this code, I can't seem to figure out what is wrong.

<?php
require_once('functions.php');

$search = $_POST['term'];
$field = $_POST['field'];

do_html_header();

//query
$conn = db_connect();
$result = $conn->query("SELECT city, restaurant FROM massachusetts
WHERE $field LIKE '%$search%'");
if (!$result)
return false;


//And we display the results
while($data = mysql_fetch_array($result))
{
echo $data['city'];
echo " ";
echo $data['restaurant'];
echo "<br>";
echo "<br>";
}

do_html_sidebar();
do_html_footer();

?>

my db_connect(); function is below (with the login information
changed):

function db_connect()
{
$result = new mysqli('localhost', 'user', 'password', 'database');
if (!$result)
throw new Exception('Could not connect to database server');
else
return $result;
}

And this is my search form where it gets the post variables:

<form method='post' action='performsearch.php'>
<table bgcolor='#cccccc'>
<tr>
<td>Search here:</td>
<td><input type='text' name='term'></td>
<td><Select NAME="field">
<Option VALUE="college">college</option>
<Option VALUE="restaurant">restaurant</option>
<Option VALUE="phone1">phone</option>
</Select></td></tr>
<tr>
<td colspan=2 align='center'>
<input type='submit' value='Search'></td></tr>
</table></form>


Can anybody help? Thanks
dan [ Do, 30 November 2006 00:38 ] [ ID #1551705 ]

Re: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Dan wrote:
> I've been getting this error below:
>
> mysql_fetch_array(): supplied argument is not a valid MySQL result
> resource
>
> When running this code, I can't seem to figure out what is wrong.
>
> <?php
> require_once('functions.php');
>
> $search = $_POST['term'];
> $field = $_POST['field'];
>
> do_html_header();
>
> //query
> $conn = db_connect();
> $result = $conn->query("SELECT city, restaurant FROM massachusetts
> WHERE $field LIKE '%$search%'");
> if (!$result)
> return false;
>
>
> //And we display the results
> while($data = mysql_fetch_array($result))
> {
> echo $data['city'];
> echo " ";
> echo $data['restaurant'];
> echo "<br>";
> echo "<br>";
> }
>
> do_html_sidebar();
> do_html_footer();
>
> ?>
>
> my db_connect(); function is below (with the login information
> changed):
>
> function db_connect()
> {
> $result = new mysqli('localhost', 'user', 'password', 'database');
> if (!$result)
> throw new Exception('Could not connect to database server');
> else
> return $result;
> }
>
> And this is my search form where it gets the post variables:
>
> <form method='post' action='performsearch.php'>
> <table bgcolor='#cccccc'>
> <tr>
> <td>Search here:</td>
> <td><input type='text' name='term'></td>
> <td><Select NAME="field">
> <Option VALUE="college">college</option>
> <Option VALUE="restaurant">restaurant</option>
> <Option VALUE="phone1">phone</option>
> </Select></td></tr>
> <tr>
> <td colspan=2 align='center'>
> <input type='submit' value='Search'></td></tr>
> </table></form>
>
>
> Can anybody help? Thanks

is 'query' a valid function? perhaps that should be 'mysql_query'?
zac.carey [ Do, 30 November 2006 01:37 ] [ ID #1553073 ]

Re: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

> while($data = mysql_fetch_array($result))

I have not used the mysqli class but try using $result->fetch_array()
instead of the function

You are using a class as the resource id instead of an actual resource id.
Peter [ Mo, 04 Dezember 2006 00:36 ] [ ID #1556914 ]

Re: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

> is 'query' a valid function? perhaps that should be 'mysql_query'?

query is a valid method in the mysqli class:-

http://www.php.net/manual/en/function.mysqli-query.php
Peter [ Mo, 04 Dezember 2006 00:38 ] [ ID #1556915 ]

Re: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

"Dan" <dcollins5531 [at] gmail.com> wrote in message
news:1164843525.434746.8190 [at] 80g2000cwy.googlegroups.com...
> I've been getting this error below:
>
> mysql_fetch_array(): supplied argument is not a valid MySQL result
> resource
>
> When running this code, I can't seem to figure out what is wrong.
>
> <?php
> require_once('functions.php');
>
> $search = $_POST['term'];
> $field = $_POST['field'];
>
> do_html_header();
>
> //query
> $conn = db_connect();
> $result = $conn->query("SELECT city, restaurant FROM massachusetts
> WHERE $field LIKE '%$search%'");
> if (!$result)
> return false;
>
>
> //And we display the results
> while($data = mysql_fetch_array($result))
> {
> echo $data['city'];
> echo " ";
> echo $data['restaurant'];
> echo "<br>";
> echo "<br>";
> }
>
> do_html_sidebar();
> do_html_footer();
>
> ?>
>
> my db_connect(); function is below (with the login information
> changed):
>
> function db_connect()
> {
> $result = new mysqli('localhost', 'user', 'password', 'database');
> if (!$result)
> throw new Exception('Could not connect to database server');
> else
> return $result;
> }
>
> And this is my search form where it gets the post variables:
>
> <form method='post' action='performsearch.php'>
> <table bgcolor='#cccccc'>
> <tr>
> <td>Search here:</td>
> <td><input type='text' name='term'></td>
> <td><Select NAME="field">
> <Option VALUE="college">college</option>
> <Option VALUE="restaurant">restaurant</option>
> <Option VALUE="phone1">phone</option>
> </Select></td></tr>
> <tr>
> <td colspan=2 align='center'>
> <input type='submit' value='Search'></td></tr>
> </table></form>
>
>
> Can anybody help? Thanks
>

It's because you're using a mysql function (mysql_fetch_array) on a mysqli
result set.
Rincewind [ Mo, 04 Dezember 2006 00:49 ] [ ID #1556916 ]

Re: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

try [at] mysql_fetch_array
Da Dimmi de wit [ Fr, 08 Dezember 2006 20:27 ] [ ID #1562442 ]
PHP » alt.php.sql » mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Vorheriges Thema: apache2.0.55 with PHP
Nächstes Thema: not able to retrieve numeric entries