mysql_fetch_array Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeva39
Forum Commoner
Posts: 45
Joined: Sun Aug 27, 2006 11:45 pm
Location: Panamá

mysql_fetch_array Problem

Post by jeva39 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello

I use this simple code to try something in my Web Server (Using PHP 4.4 and mySql 4.1):

Code: Select all

<?php
$con = mysql_connect("localhost", "username", "pass");
mysql_select_db("jv");

$query  = "SELECT tema, ritmo FROM temas where ritmo='BALADA' order by tema";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_NUM)) // Line 8
{
    echo "Tema : {$row[0]} " . " Ritmo : {$row[1]} <br>";
} 

mysql_close($con);

?>
But I receive this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\Inetpub\vhosts\prolatin.com\httpdocs\php\jv_mysql.php on line 8


The Database don't have problem. Please do you can help me what is wrong?

In my computer all run OK.

Thanks in advanced....


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Are you sure you haven't mistyped your field names in the query?
jeva39
Forum Commoner
Posts: 45
Joined: Sun Aug 27, 2006 11:45 pm
Location: Panamá

Post by jeva39 »

No! They are correct. As I say you above, in my computer the same code run OK.

Thanks....
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

You may get some more information placing this at the end of the script:

Code: Select all

echo 'error:' . mysql_error();
jeva39
Forum Commoner
Posts: 45
Joined: Sun Aug 27, 2006 11:45 pm
Location: Panamá

Post by jeva39 »

Thanks for your help. With your code I detect the error (a Database configuration in PLESK) and now all work fine!!!

Regards...
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

De nada -- good luck
jeva39
Forum Commoner
Posts: 45
Joined: Sun Aug 27, 2006 11:45 pm
Location: Panamá

Post by jeva39 »

Qué bueno que habla español...;-)

I have another little (or big) problem: (I know this is not theme for this forum but....)

I create a Virtual directory in my Hosting Web Server but I don't know how to uploads files to this Dir. Review many info in Internet but all is about create and managing Virtuals in the IIS local (my computer) I send tickets to support in Web Hosting but only send me two links to Microsoft that don't help me for nothing. The manual of PLESK don't say nothing about the topic. I can use a FTP client like WS_FTP to upload these files?

If you know something about this, can help me? Sorry if abuse of your time....
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

I'm not sure either, but you might be able to find some help here
jeva39
Forum Commoner
Posts: 45
Joined: Sun Aug 27, 2006 11:45 pm
Location: Panamá

Post by jeva39 »

Thanks for the link. I send a post and.....waiting!
jeva39
Forum Commoner
Posts: 45
Joined: Sun Aug 27, 2006 11:45 pm
Location: Panamá

Post by jeva39 »

Now I try to load the same data into a Table but the output repeat in the two colums the same field and only for the first record. No rows for the rest of records...

For example, the first record is "A la candela" and the rhythm is "Merengue". The output is

"A la candela" "A la candela"

And repeat this record forever

You can check this at: http://www.prolatin.com/php/jv_mysql2.php


This is the code:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<html> 
<head> 
<title>Pruebas</title> 
<link rel="stylesheet" type="text/css" href="lib.css"> 
</head> 
<body bgcolor="#FFFFFF" text="#996600" link="#996600" vlink="#996600" alink="#996600"> 
<table width="90%" border="1" align=center bgcolor="#FFFFFF"> 
<tr> 
<td> 
<?php 
ini_set('error_reporting', E_ALL); 
ini_set('display_errors', 1); 
$con = mysql_connect("localhost", "username", "pass")or die('MySQL Connect Error: '.mysql_error()); 
mysql_select_db("jv"); 
$sql = "select tema,ritmo from temas order by tema"; 

$queryexe = mysql_query($sql); 
while(mysql_fetch_row($queryexe)) 
    { 
  $tema = mysql_result($queryexe, "tema");  
  $ritmo = mysql_result($queryexe, "ritmo"); 
  
?> 
   <tr> 
   <td class="listas" bgcolor="#f7efde"> <?php print ("$tema"); ?></td> 
   <td class="listas" bgcolor="#f7efde"> <?php print ("$ritmo"); ?></td> 
   </tr> 
<?php }
   echo "Error: " . mysql_error(); 
?> 
</td> 
</tr> 
</table>                                        
</body> 
</html>
Thanks for your help!!!
Last edited by jeva39 on Sun Sep 24, 2006 4:57 pm, edited 3 times in total.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

What does

Code: Select all

while($row = mysql_fetch_array($result, MYSQL_NUM))
do?

I'm intregued.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

To access the data you now put

Code: Select all

echo $result[1] 
  instead of
  echo $result["NameOfField"]
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

If you want numeric indexes on fields use mysql_fetch_row, to get an array indexed by the field name use mysql_fetch_assoc.

Personally I always get the associated array so I know what I am dealing with.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

impulse() wrote:What does

Code: Select all

while($row = mysql_fetch_array($result, MYSQL_NUM))
do?

I'm intregued.
It is a normal while loop, that says basically:

assign mysql_fetch_array($result, MYSQL_NUM) to the $row variable. If this doesn't work (because we've reached the end of the resultset), exit the while loop.

It is basically evaluating the statement

Code: Select all

$row = mysql_fetch_array($result, MYSQL_NUM);
if it's true, then continue with the loop, if it's false, then it means that the value of mysql_fetch_array(...) is null or false, which in turn means we're at the end of the result set (or it was empty), and so we don't continue with the loop.
jeva39
Forum Commoner
Posts: 45
Joined: Sun Aug 27, 2006 11:45 pm
Location: Panamá

Post by jeva39 »

Thanks very much CoderGoblin. All work fine with this modification:

Code: Select all

$result = mysql_query($sql); 
while($row=mysql_fetch_assoc($result)) 
    { 
?> 
   <tr> 
   <td class="listas" bgcolor="#f7efde"> <?php echo $row["tema"]; ?></td> 
   <td class="listas" bgcolor="#f7efde"> <?php echo $row["ritmo"]; ?></td> 
   </tr> 
<?php }
?>
Post Reply