Page 1 of 1

mysql_fetch_array Problem

Posted: Sat Sep 23, 2006 3:51 pm
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]

Posted: Sat Sep 23, 2006 3:55 pm
by aaronhall
Are you sure you haven't mistyped your field names in the query?

Posted: Sat Sep 23, 2006 4:15 pm
by jeva39
No! They are correct. As I say you above, in my computer the same code run OK.

Thanks....

Posted: Sat Sep 23, 2006 4:55 pm
by aaronhall
You may get some more information placing this at the end of the script:

Code: Select all

echo 'error:' . mysql_error();

Posted: Sat Sep 23, 2006 11:35 pm
by jeva39
Thanks for your help. With your code I detect the error (a Database configuration in PLESK) and now all work fine!!!

Regards...

Posted: Sat Sep 23, 2006 11:46 pm
by aaronhall
De nada -- good luck

Posted: Sun Sep 24, 2006 12:09 am
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....

Posted: Sun Sep 24, 2006 12:13 am
by aaronhall
I'm not sure either, but you might be able to find some help here

Posted: Sun Sep 24, 2006 2:01 am
by jeva39
Thanks for the link. I send a post and.....waiting!

Posted: Sun Sep 24, 2006 11:51 am
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!!!

Posted: Sun Sep 24, 2006 12:01 pm
by impulse()
What does

Code: Select all

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

I'm intregued.

Posted: Mon Sep 25, 2006 3:19 am
by Rovas
To access the data you now put

Code: Select all

echo $result[1] 
  instead of
  echo $result["NameOfField"]

Posted: Mon Sep 25, 2006 5:10 am
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.

Posted: Mon Sep 25, 2006 7:00 am
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.

Posted: Mon Sep 25, 2006 7:38 pm
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 }
?>