-2

Hi I have been struggling with this query for a while now

I need to get the number of rows inserted into my database table, but I keep getting this error I can't seem to get rid of.

$result3=mysql_query("INSERT INTO dest_table.create_info SELECT * from 
Profusion.source_cdr");
$num_rows = array($result3);
$progress=mysql_num_rows($num_rows);
echo $progress;

The error I get is

mysql_num_rows() expects parameter 1 to be resource, array given

It will be highly appreciated if I get this resolved

user1002713
  • 41
  • 1
  • 1
  • 9

2 Answers2

0

You have to pass the resource not the array.

$result3=mysql_query("INSERT INTO dest_table.create_info SELECT * from 
Profusion.source_cdr");
$progress=mysql_num_rows($result3);
echo $progress;

But since you are using INSERT you should read the manual and use mysql_affected_rows() because mysql_num_rows() only works with SELECT and SHOW.

http://www.php.net/manual/de/function.mysql-affected-rows.php

$result3=mysql_query("INSERT INTO dest_table.create_info SELECT * from 
Profusion.source_cdr");
$progress=mysql_affected_rows();
echo $progress;
tuxmania
  • 906
  • 2
  • 9
  • 28
  • oh I see, but the problem is I still get a -1 answer if I ask for the number of rows affected, the thing is I want the number of rows in the affected table so that I can pass them into a progress bar that I have created. Thanks for the help though! – user1002713 Dec 18 '13 at 10:46
  • If you get -1 then your sql query failed. Debug this by doing: $result3=mysql_query("INSERT INTO dest_table.create_info SELECT * from Profusion.source_cdr") or die(mysql_error()); – tuxmania Dec 18 '13 at 10:48
  • Will this equate to an integer value that I can pass into my progress bar to get the percentage. – user1002713 Dec 18 '13 at 10:52
  • Again the reason for getting -1 from mysql_affected_rows() is that you SQL query failed e.g. it contains errors. So of course 0 rows were affected because the query is probably illegal by some syntax issues. Hence your first approach should be debugging your sql query by asking mysql_error() as i stated before. After you fixed your query you can use mysql_affected_rows() properly – tuxmania Dec 18 '13 at 10:53
  • yes I debugged it there seems to be an error here Thread stack overrun: 6312 bytes used of a 131072 byte stack, and 128000 bytes needed. Use 'mysqld -O thread_stack=#' to specify a bigger stack. – user1002713 Dec 18 '13 at 10:55
  • 1
    there must be something wrong with my table, I'll let you know just now. thanks tuxmania – user1002713 Dec 18 '13 at 10:57
  • It worked you guys helped alot, thank you very much, I really appreciate it. – user1002713 Dec 18 '13 at 11:04
0

When using an INSERT statement, mysql_query() doesn't return an array. It returns true on success or false on failure. Use mysql_affected_rows().

For example:

mysql_query("INSERT INTO table (field1,field2) VALUES ('foo', 'bar')");
$num_affected_rows = mysql_affected_rows();

echo $num_affected_rows;

//output: 1
Brandon M.
  • 278
  • 1
  • 4
  • 15