Log in

View Full Version : Resolved mysql_fetch_array(): supplied argument is not a valid MySQL result resource



james438
11-14-2008, 08:58 PM
dunno why, but it says that I cannot connect to the database. any ideas why?


<?php
include 'include/db.php';
$query = "SELECT * FROM news2";
$result = mysql_query($query) or die ("Couldn't execute query.");
$a=1;
while ($value = mysql_fetch_array($result)){
$news[1]=$value['ID'];
$news[2]=$value['energy'];
$news[3]=$value['date'];

$db="INSERT INTO news (ID,energy,date) VALUES ('$news[1]','$news[2]','$news[3]'";
$result = mysql_query($db) or die ("Couldnnt execute query.");
echo "$db<br>";
}
?>

EDIT: For those curious I created this program to pull the contents of one table and insert it onto the end of another table. I have too many tables in my database right now and I am trying to prune them down a bit. See the post# 4 for the fixed version of this script.

james438
11-14-2008, 09:53 PM
silly me. I forgot the closing parentheses. It should read:

<?php
include 'include/db.php';
$query = "SELECT * FROM news2";
$result = mysql_query($query) or die ("Couldn't execute query.");
$a=1;
while ($value = mysql_fetch_array($result)){
$news[1]=$value['ID'];
$news[2]=$value['energy'];
$news[3]=$value['date'];

$db="INSERT INTO news (ID,energy,date) VALUES ('$news[1]','$news[2]','$news[3]')";
$result = mysql_query($db) or die ("Couldnnt execute query.");
echo "$db<br>";
}
?>

james438
11-14-2008, 10:44 PM
New problem. I am getting the error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/a/n/i/animeviews/html/test.php on line 5.

The database updates, but only for one row.


<?php
include 'include/db.php';
$query = "SELECT * FROM news2";
$result = mysql_query($query,$conn) or die ("Couldn't execute query.");
while ($value = mysql_fetch_array($result,MYSQL_ASSOC)){
$marriage=$value['energy'];
$date=$value['date'];
$db="INSERT INTO news (marriage,date) VALUES ('$marriage','$date')";
$result = mysql_query($db) or die (mysql_last_error());
}

?>

james438
11-15-2008, 12:15 AM
There were two errors. In order to add content to the database I needed to add slashes. The second error involved resetting of the variable $result on line 9. I simply renamed the first $result variable to $result1. Problem solved.


<?php
include 'include/db.php';
$query = "SELECT * FROM news2";
$result1 = mysql_query($query) or die ("Couldn't execute query.");
while ($row = mysql_fetch_array($result1,MYSQL_ASSOC)){
$marriage=$row["energy"];$marriage=addslashes($marriage);
$date=$row["date"];
$db="INSERT INTO news (marriage,date) VALUES ('$marriage','$date')";
$result = mysql_query($db) or die (mysql_error());
}
echo "all done";
?>