-1

I am trying to connect to database through php.I did it lots of time. But I want to know how many rows exist in my table. I've tried to use php manual, however, I was confused.

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);

The above code worked on :

$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);

But when I decided to use

$link = mysql_connect("localhost", "mysql_user", "mysql_password","database");

It occurred an error like this:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in ...

What is difference between codes? And how can I solve it? Thanks in advance

AMiSH13
  • 97
  • 3
  • 8
  • 2
    use mysqli instead of mysql, and u can not pass database in mysql_connect statement `mysql_connect("localhost", "mysql_user", "mysql_password","database");`.. – Suhel Meman Apr 02 '13 at 06:47

5 Answers5

2

The 4th parameter to mysql_connect is supposed to be a boolean type indicating whether you always want a new connection (even if mysql_connect was called earlier), not a string. As a result, $link is false because mysql_connect failed.

PHP documentation on mysql_connect

Tushar
  • 8,019
  • 31
  • 38
0

selecting DB as 4th parameter in mysql_connect is wrong - the functions returns false and no mysql_num_rows() following is possible

Ilya
  • 169
  • 6
0

The mysql_connect function accept 5 arguments. In that in 4th argument this should be either true or false. But you have passed the string. This is not correct. you cann't pass the database name here.

For more tutorial see here and also this link.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
0

You can not select database by mysql_connect() function. This function is used to connect to the database, not to query the database. To query the database use mysql_select() instead.

YasirA
  • 9,531
  • 2
  • 40
  • 61
Sandeep Kapil
  • 984
  • 8
  • 14
-1

this parameters are allowed in mysql_connect

mysql_connect(server,user,pwd,newlink,clientflag)

please check http://www.w3schools.com/php/func_mysql_connect.asp

Nunser
  • 4,512
  • 8
  • 25
  • 37
kapil_dev
  • 86
  • 4