Forum Moderators: coopster

Message Too Old, No Replies

The LIKE Command MySQL/PHP

Difference between command line and PHP Output

         

Knowles

5:35 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



All righty I am trying to make a simple search engine for mysite to go through the database and find simular output. When I do this from a command line with this:
SELECT ID FROM helpdesk WHERE COMMENT LIKE '%F%' OR ERROR LIKE '%F%'
I get 14 results and it lists the ID's I need.
Now when I put this into php then try to output the result to mysql_fetch_array I am getting:
mysql_fetch_array(): supplied argument is not valid MySQL result resource
I know there is 14 results in the query and unless I am misunderstanding mysql_fetch_array it should put those into $result[0]-$result[13] Or am I wrong about this? Would mysql_fetch_array only grab one row of info? So it would only get 'ID' once?

jatar_k

5:43 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



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

it sounds like your query is messed up. Whatever you are passing to mysql_fetch_array is not usable. Try echo'ing the query you are passing to see if there is something visibly wrong. You can even paste the exact output into the command line to make sure.

Would mysql_fetch_array only grab one row of info?

It will return an array that is all the fields from 1 returned row. You need to loop it to get all of the rows. It will return false when there are no more rows.

Knowles

5:55 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



The ECHO'ed output was:
SELECT ID FROM helpdesk WHERE ERROR LIKE '%F%' OR COMMENT LIKE '%F%'
which is exactly as it is in the command line to return values.

I have the mysql_fetch_array in a while loop:


while($row = mysql_num_array($sr)) {
echo '<tr>';
for($i = 0; $i < count($row); $i++) {
echo '<td>'.$row[$i].'</td>';
}
echo '</tr>';
}

I still cant seem to figure out what I am missing, could this error be because my while is not correct?

[edited by: Knowles at 6:06 pm (utc) on Feb. 16, 2004]

jatar_k

6:05 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



while($row = mysql_num_fields($sr)) {

?
shouldn't that be

while($row = mysql_fetch_array($sr)) {

Knowles

6:07 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



Sorry yes it should be, I was paying with different things to see if I was simply using mysql_fetch_array... I forgot to change it back. I edited the code to refect what it was.

jatar_k

6:17 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well "not valid MySQL result resource" means the mysql_query function had an error try putting

$sr = mysql_query($sql) or die ("<p>I failed and here is why: " . mysql_error());

Knowles

6:30 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



Even when I try that the only error I get is:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\www\lch\blah.php on line 17
No error msg, it doesnt seem to be dying there.
Here is the code again with the die comment added.
$sr = ("SELECT ID FROM helpdesk WHERE ERROR LIKE '%F%' OR COMMENT LIKE '%F%'")
or die ("<p>I failed and here is why: " . mysql_error());

while($row = mysql_fetch_array($sr)) {
echo '<tr>';
for($i = 0; $i < count($row); $i++) {
echo '<td>'.$row[$i].'</td>';
}
echo '</tr>';
}

ikbenhet1

6:37 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



your'e wrong about "$result[0]-$result[13]"
you need to fetch it,
$x=mysql_query($sql);
$row=mysql_fetch_array($x);
$row['id']; //would be row ID
$row['title']; //would be row title

you don't need to count($row), this will do, but is that what's creating that error? try:
$sr=mysql_query("select id where....");echo '<tr>';
while($row = mysql_fetch_array($sr)) {
echo '<td>'.$row['id'].'</td>';
}
echo '</tr>';

coopster

7:11 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You haven't executed your query yet, so there is no result set:

$sql = "SELECT ID FROM helpdesk WHERE ERROR LIKE '%F%' OR COMMENT LIKE '%F%'";
$sr = mysql_query($sql);
while($row = mysql_fetch_array($sr)) {
echo '<tr>';
for($i = 0; $i < count($row); $i++) {
echo '<td>'.$row[$i].'</td>';
}
echo '</tr>';
}

Knowles

7:25 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



Bangs head against wall! Ok I have done way to much this morning! Thank you coopster and everyone else who tried to help me find my stupidity!