-2

Why isnt this working?

mysql_select_db('a2943462_Pages');
$num_rows = mysql_query("SELECT COUNT(*) FROM PagesInfo");
echo $num_rows;
mysql_query("INSERT INTO `a2943462_Pages`.`PagesInfo`(`ID`, `Title`, `Video`, `Posted`) VALUES ('".$num_rows."', '".$Title."', '".$Embed."', '".$name."')");
printf("Last inserted record has id %d\n", mysql_insert_id());

Because it now echoes: Resource id #9

but i tought it would echo 4 instead because i have 3 rows inside the Table

a coder
  • 546
  • 4
  • 23

3 Answers3

2

Change this:

$num_rows = mysql_query("SELECT COUNT(*) FROM PagesInfo");

To this:

$num_rows = mysql_result(mysql_query("SELECT COUNT(*) FROM PagesInfo"), 0);
Krimson
  • 7,386
  • 11
  • 60
  • 97
  • This is the only answer for me that did the Job smoothly, All the Others Thanks for the fast Responses i realy apreciate it. – user3588568 May 09 '14 at 17:46
1

This is the PHP method:

$sql="SELECT * FROM pages_info";
$result = mysql_query($sql, $con) or die (echo "$sql" );
$rows = mysql_num_rows($result);
a coder
  • 546
  • 4
  • 23
  • Though this will work its not efficient since the mysql database will send Everything in the table to the php script which is waste of resources... Its better to just count the rows in the db and send the value to the php script – Krimson May 09 '14 at 17:29
  • It's a good answer for Dirty code. The user included the PHP option for the script, so I thought I'd provide a solution in PHP. – a coder May 09 '14 at 17:32
  • You can always do smart escaping on every variable you receive. – a coder May 09 '14 at 17:37
-2

you should do two queries:

first query what you want to show:

SELECT SQL_CALC_FOUND_ROWS * FROM PagesInfo

and second query to get number rows returned:

SELECT FOUND_ROWS()

The in your insert the number of rows returned

The query in php is

$rows = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM PagesInfo");
$rs = mysql_query("SELECT FOUND_ROWS()"); 
$r = mysql_fetch_row($rs); 
mysql_free_result($rs); 
$row_num = $r[0]; 
Dave
  • 7,028
  • 11
  • 35
  • 58
  • Do i have to put the querys inside a variable or just the second one? ? and if i do this how can i add 1 to the value found?? just by $num_rows = mysql_query("SELECT FOUND_ROWS()")++; – user3588568 May 09 '14 at 17:25
  • you have to put the result in the second one as normal query and you will get the number of rows, it will return a register with one field and you could access using some alias – Dave May 09 '14 at 17:29
  • $rs = mysql_query("SELECT FOUND_ROWS()"); $r = mysql_fetch_row($rs); mysql_free_result($rs); $row_num = $r[0]; – Dave May 09 '14 at 17:37