[SOLVED]mysql_fetch_array()

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

[SOLVED]mysql_fetch_array()

Post by ol4pr0 »

I got this problem with retrieving information from the database.

2 errors occure.
Notice: Undefined index: Paqnr in c:\apache\htdocs\retrieve.php on line 31

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\apache\htdocs\retrieve.php on line 33
Script;

Code: Select all

// formulario, solo pone el codigo paquete
ECHO <<<HTML
<html>
<head>
<body class="v">
<form name="retrieve" action={$_SERVER['PHP_SELF']} method="GET" class="center">
<table width="250" border="0" class="center" cellpading="3">
 <tr>
  <td>
   <div class="center"><b>Required information?</b></div>
   </td>
  </tr>
  Enter Paq. number:<input type="Paqnr" name="paqnr">
  <br>
  <Div class="r"><input name="Paqnr" type="submit" ID="Paqnr" WIDTH="5"
  CLASSID="CLSID:B6FC3A14-F837-11D0-9CC8-006008058734"></div>
HTML;

// connectarse con el base datos.

$dbase ="home";
$tablename ="pongo";
$connect= mysql_connect('localhost')
	or die("Cant connect: " .mysql_error());

// consultar el tabla pongo 

$query = "SELECT * FROM pongo WHERE Paqnr='" .$_GET['Paqnr']. "'"; //line 31 ( i also tried ='" .$paqnr. " ' ";
$result = mysql_query($query);
if($row = mysql_fetch_array($result)) // line 33
{
 echo "<tr class="V"> ";
 echo "<td width="100" class="v"> ";
 echo "<div class="r">Numero Paquete</div> ";
 echo "</td>\n ";
 echo "<td width="150"> ";
 echo $row['Paqnr'];
 echo "		</td>\n ";
 echo "	   </tr>\n ";

and so On...... 
}
?>

What am i doing wrong.
Last edited by ol4pr0 on Mon Jan 26, 2004 9:52 am, edited 1 time in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

yes, i see the problem, nothing wrong with your PHP, its your HTML

this line

Code: Select all

<input type="Paqnr" name="paqnr">
Should be

Code: Select all

<input type="text" name="paqnr">
Mark
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

HAd it like that

doesnt make any differance.
line 31
line 33
is whats making my life difficult on this sunny day.

Thanks for noticing tho.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

also,

your form element name is "paqnr", but you are requesting "Paqnr"

Notice the different case, should be the same!


Mark
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

[Edit: that'll teach me to write a post, get caught up in other things and then submit it - too slow :lol:]

It's because of this:

Code: Select all

<input type="Paqnr" name="paqnr">
Firstly, there is no such 'Paqnr' type - you probably want it to be of type text:

Code: Select all

<input type="text" name="paqnr" />
Now PHP is case-sensitive so $_GET['Paqnr'] is not the same as $_GET['paqnr']. You'll end up confusing matters even more because your submit button is called 'Paqnr' - you should really change its name. You should also check that the variable is in the URL before trying to use it and that it is of a type you'd expect, using user inputted data directly in a SQL statement is asking for trouble.

I started trying to correct your code but ended up confused over what it was trying to do - you select a number from the form and then redisplay this number before selecting another? I guess I'm just missing what's in the 'and so On...' bit.

Mac
Last edited by twigletmac on Mon Jan 26, 2004 9:21 am, edited 1 time in total.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Oke i think you are aming for the Capital P here. so i did change that also changed the input type='text'. Just to make sure i am not a complete idiot.

However it didnt resolve my issue
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If the variable is not in the URL you'll get the undefined index error - you need to do some checking before you use the variable.

Mac
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

can you now show us your ammended code

Mark
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Well i had it all like input='text' and yes mysql also has Paqnr. not paqnr.

since i tried to figure out why it wouldnt reconize my Paqnr i start messing around with the Paqnr in places i could mess with it. To see if it would reconize.

mysql database looks like this.
CREATE TABLE pongo (
Empresa varchar(20) NOT NULL default '',
Codigo int(16) NOT NULL default '0',
Paqnr int(16) NOT NULL default '0',
Desde varchar(20) NOT NULL default '',
Envio varchar(20) NOT NULL default '',
Peso_Total varchar(50) NOT NULL default '',
Peso varchar(10) NOT NULL default '',
Valor int(9) default NULL,
Information varchar(20) NOT NULL default '',
Comentario varchar(20) NOT NULL default '',
Codigo_Env int(15) default NULL,
PRIMARY KEY (Paqnr)
) TYPE=MyISAM;
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Maybe to make your debugging easier, ill briefly tell you what the eror messages mean
Notice: Undefined index: Paqnr in c:\apache\htdocs\retrieve.php on line 31
This bascially means that you are trying to access an index in an array that doesn't exist. In theis case, you are trying to access "paqnr" in the array "$_GET".
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\apache\htdocs\retrieve.php on line 33
The reason you are getting this is because the query you are supplying isn't correct, this is due to the reason described above.

Fix the first problem, and in turn you fix the second.

also, in the address bar, when you submit the form, can you see Paqnr=(the value you eneterd)?

Mark
Last edited by JayBird on Mon Jan 26, 2004 9:32 am, edited 1 time in total.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

the code like it was before my messing around with it. and how it is Now!

Code: Select all

<?
require ("include/styles.php");

// formulario, solo pone el codigo paquete
ECHO <<<HTML
<html>
<head>
<body class="v">
<form name="retrieve" action={$_SERVER['PHP_SELF']} method="GET" class="center">
<table width="250" border="0" class="center" cellpading="3">
 <tr>
  <td>
   <div class="center"><b>Required information?</b></div>
   </td>
  </tr>
  Enter Paq. number:<input type="text" name="Paqnr">
  <br>
  <Div class="r"><input name="submit" type="submit" ID="Paqnr" WIDTH="5"
  CLASSID="CLSID:B6FC3A14-F837-11D0-9CC8-006008058734"></div>
HTML;

// connectarse con el base datos.

$dbase ="home";
$tablename ="pongo";
$connect= mysql_connect('localhost')
	or die("Cant connect: " .mysql_error());

// consultar el tabla pongo ( cambiar cuando listo )

$query = "SELECT * FROM pongo WHERE Paqnr='" .$_GET['Paqnr']. "'";
$result = mysql_query($query);
if($row = mysql_fetch_array($result))
{
 echo "<tr class="V"> ";
 echo "<td width="100" class="v"> ";
 echo "<div class="r">Numero Paquete</div> ";
 echo "</td>\n ";
 echo "<td width="150"> ";
 echo $row['Paqnr'];
 echo "		</td>\n ";

AND SO ON
}
?>
same errors.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

i don't see you selecting the DB anywhere.

Try inserting this line before your query

Code: Select all

mysql_select_db($dbase, $connect);
Mark
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

yea i have that line in top in my explorer
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Now i do feel like a complete idiot. Caramba..
mysql_select_db($dbase, $connect);


one more question

should this return a error

Code: Select all

echo "<div class="r">Peso Total</div> ";
 echo "</td>\n ";
 echo "<td width="150"> ";
 echo $row['Peso_total']; // Peso_Total
 echo "		</td>\n ";
is it because of _ the underscore ?

never mind "Case intensive " ;-)
thanks
Last edited by ol4pr0 on Mon Jan 26, 2004 9:44 am, edited 2 times in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

glad its sorted :)

hopefully we ironed out a few other potential problems along the way too.

Mark
Post Reply