Page 1 of 1

php warning

Posted: Thu Apr 26, 2012 7:13 am
by minotron
Hi

I have this code:

Code: Select all

$sql = "SELECT game_id, rival_id, result, h_a, UNIX_TIMESTAMP(datum) AS datum FROM game ORDER BY datum ASC";
$result = mysql_query($sql, $db) or die(mysql_error($db));
if(mysql_num_rows($result) > 0)
{
echo '<table>';
echo '<tr><td>Dátum</td><td>Domáci</td><td>Hostia</td><td>Výsledok</td></tr>';
while($row = mysql_fetch_array($result))  //at this line is the warning
{
extract($row);
It writes me this error: "Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in H:\xampp\htdocs\rozpis.php on line 18"
I can't find problem in this code. Do you know what is bad? Thank you for reply.

Re: php warning

Posted: Thu Apr 26, 2012 7:44 am
by egami
mysql_error($db)

this should not have any variables..

should be just die ("ERROR: ".mysql_error());

Re: php warning

Posted: Thu Apr 26, 2012 7:48 am
by minotron
I have changed that, but it still writes the same warning.

Re: php warning

Posted: Thu Apr 26, 2012 7:57 am
by egami

Code: Select all


$sql = "SELECT game_id, rival_id, result, h_a, UNIX_TIMESTAMP(datum) AS datum FROM game ORDER BY datum ASC";
$result = mysql_query($sql) or die(mysql_error());
$numrow = mysql_num_rows($result);
if($numrow > 0)
  $count = 0;
  while($row = mysql_fetch_array($result)) { 
    $++count;
    if ($count = 15) { // This is so we don't fill the page up.. who knows how many rows you have...
      break;
    }
    echo '<pre>'; print_r($row); echo '</pre>';
  }
} else { 
  echo "QUERY: $sql <br />";
  echo "Found $numrow entries..<br />";
  echo "Try running the above query in your phpMyAdmin or SQL Manager to see if the query works outright<br/>";
}

 

Re: php warning

Posted: Thu Apr 26, 2012 8:14 am
by minotron
When I run query in phpmyadmin it shows correct values...but when I run it in localhost it shows this message and writes only first row from table...and when I changed it so how you have written it, it showed the same warning...it is very mysterious :) :help:

Re: php warning

Posted: Thu Apr 26, 2012 8:17 am
by minotron
and when I try to run:

Code: Select all

echo mysql_num_rows($result)
it writes right value

Re: php warning

Posted: Thu Apr 26, 2012 8:32 am
by egami
show me your entire script please.

Re: php warning

Posted: Thu Apr 26, 2012 8:37 am
by minotron

Code: Select all

<?php
include 'connect.inc.php';
include 'header.inc.php';



$mesiace = array("Január", "Február", "Marec", "Apríl", "Máj", "Jún", "Júl", "August", "September", "Október", "November", "December");

echo '<div id="rozpis">';
$sql = "SELECT game_id, rival_id, result, h_a, UNIX_TIMESTAMP(datum) AS datum FROM game ORDER BY datum ASC";
$result = mysql_query($sql, $db) or die("ERROR: ".mysql_error());
if(mysql_num_rows($result) > 0)
{
echo '<table>';
echo '<tr><td>Dátum</td><td>Domáci</td><td>Hostia</td><td>Výsledok</td></tr>';
while($row = mysql_fetch_array($result))
{
extract($row);
$prikaz = 'SELECT rival_id, name FROM rival WHERE rival_id = '. $rival_id;
$vystup = mysql_query($prikaz, $db) or die(mysql_error($db));
$rival = mysql_fetch_array($vystup);
echo '<tr>';
echo '<td>' .date("j. F Y", $datum). '</td>';
if ($h_a == 1)
{
echo '<td> Capitals </td>';
echo '<td>' .$rival['name']. '</td>';
}
else
{
echo '<td>' .$rival['name']. '</td>';
echo '<td> Capitals </td>';
}
echo '<td><a href="zapas.php?zapas='. $game_id.'">' .$result. '</td>';
echo '</tr>';
}
echo '</table>';
}
else
echo 'No matches';

echo '</div>';

include 'zahlavie.php';
?>

Re: php warning

Posted: Thu Apr 26, 2012 9:41 am
by egami
first things first..
change the first mysql_fetch_array to mysql_fetch_assoc

You have two SQL calls, and you're trying to extract() names that don't exist.

So change the first one to assoc, and not array, and then try to run it again.. I want to see if the error is calling on the second SQL and not the first.

Re: php warning

Posted: Thu Apr 26, 2012 10:30 am
by minotron
When I changed the first mysql_fetch_array($result) to mysql_fetch_assoc($result) it had written warning about mysql_fetch_assoc($result) so the problem is in the first one...

Re: php warning

Posted: Thu Apr 26, 2012 10:50 am
by egami
$sql = "SELECT game_id, rival_id, result, h_a, UNIX_TIMESTAMP(datum) AS datum FROM game ORDER BY datum ASC";

change that to
$sql = "SELECT * FROM game ORDER BY datum ASC";

Re: php warning

Posted: Thu Apr 26, 2012 11:49 am
by minotron
It writes the same error and now writes else problem with date: Notice: A non well formed numeric value encountered in H:\xampp\htdocs\rozpis.php on line 24 ...it's this line:

Code: Select all

echo '<td>' .date("j. F Y", $datum). '</td>';

Re: php warning

Posted: Fri Apr 27, 2012 6:40 am
by minotron
I have solved it so, that I deleted: extract($row) and now it goes, thank you for your help