Forum Moderators: coopster

Message Too Old, No Replies

Using PHP to populate javascript arrays

help with a tutorial

         

davidhorn01

4:05 pm on Apr 13, 2004 (gmt 0)

10+ Year Member



Hi All -

I'm trying to follow this tutorial:

[devshed.com...]

and am getting an error message:

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in FILENAME on line 25

I've posted the code at the bottom of this email - line 25 is the line which begins:

while ($myrow = mysql_fetch_row($result))

If anyone has any thoughts, I'd really appreciate it. The connection to the database appears fine. When I 'print-error' it's telling me that it's a PARSE error - but I'm not sure if that's the real problem or whether I've implemented the 'print-error' bit of the code wrong!

Anyway, as you can tell, I'm a bit of a novice at this, so any help would be really appreciated. Many thanks,

David

<?php
$db = mysql_pconnect("localhost", "USERNAME", "PASSWORD");
// This establishes a link to MySQL
mysql_select_db("extranet",$db);
// The database is specified

$sql = "SELECT
p.person_id,
s.person_id,
CONCAT(last_name,', ',first_name) AS name,
skill_id ";

$sql .= "FROM
personnel p,
person_skill s
WHERE
p.person_id = s.person_id
ORDER BY
skill_id, name";

$result = mysql_query($sql);

$type = "";
$number2 = "0";
while ($myrow = mysql_fetch_row($result))

{
if ($myrow[3]!= $type) {
if ($number2!= NULL) {
$newnumber2 = ($number2 + "1");
print ("ar[$number2] = new Array();\n");
$number2 = $newnumber2;
$type = $myrow[3];
$number = "0";
}
}
print "ar[" . ($number2 - "1") . "]";
if ($number!= NULL) {
$newnumber = ($number + "1");
print ("[$number]");
$number = $newnumber;
}
print (" = new makeOption(\"$myrow[2]\", \"$myrow[1]$myrow[3]\");\n");
}
?>

ergophobe

4:32 pm on Apr 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That usually means your query failed and you are feeding an invalid result to you fetch.

Quick test: Have your script echo the query, then open your mysql client and feed it the same query. I suspect you'll find that it fails.

ergophobe

4:35 pm on Apr 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



ps, if you won't be able to ensure that the query will be valid (e.g based on user input), but you don't want the script to die, you can enclose everything in an if

if ($result = mysql_query($query)) {
do stuff;
}else{
don't do stuff;
}

In your case, though, your JS arrays would be empty which, I'm guessing, would be bad.

davidhorn01

10:16 am on Apr 15, 2004 (gmt 0)

10+ Year Member



thanks for your help with this - It was a problem with the query - so thanks.

I'm running into some other problems so I'm going to post again shortly!