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
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
selecting DB as 4th parameter in mysql_connect is wrong - the functions returns false and no mysql_num_rows() following is possible
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.
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.
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
Related
I got the warning:
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in (...) on line 6
My code is here:
<?php
require_once 'conn.php';
$sql = "SELECT user_id, access_lvl, name FROM cms_users ";
$result = mysqli_query($sql, $conn);
It's exactly as the error states as you're passing arguments to mysqli_query() incorrectly. Assuming $conn is your mysqli connection generated at some point by new mysqli() it should be:
$result = mysqli_query( $conn,$sql) or trigger_error(mysqli_error($conn)));
The way you were calling it you were passing a string, $sql as the first argument.
I was having this this problem but after swiping
$result = mysqli_query($sql, $conn) to $result = mysqli_query( $conn,$sql)
I manage to solve this error
I am 3 years too late but all you need to do is swap the parameters of mysqli_query()
$result = mysqli_query($conn, $sql)
I have the same error, although the $result = mysqli_query($conn, $sql) is the correct way around.
I var_dump() the $conn object and it is a set object at the time I run the query, but still returns a 'string given' error.
I was accessing the $conn object after being parsed into a function that I was using it with, in the same way I've done throughout the whole project without error.
Re-declaring the $conn object inside the function, instead of passing it into the function stopped the errors, although this behaviour doesn't occur anywhere else in my project. This isn't an ideal solution either.
To note: I'm using a .env for local development, which causes no issues and helps with deployment locally/remotely via .git.
After many hours, I honestly believe there is a PHP bug here, I'm using 7.3.0, but occurred in 7.2.5 as well as I'm definitely parsing it a db connection object, not a string.
Just posting this for information purposes, in case anyone else runs into it. Thanks.
PS. Passwords shouldn't be stored in the database in plain text and is a major security concern. If the author hasn't adjusted this yet (I know it's an old post), it's important to read:
Secure hash and salt for PHP passwords
I want to fetch some databases according to a specific name.
I have:
$sql="SHOW DATABASES LIKE `'%backup%'`";
$query=mysql_query($sql,$connect);
$row=mysql_fetch_assoc($query);
I need to fetch the databases in an array according to a specific name,but it gives me an error:
mysql_fetch_assoc() expects parameter 1 to be resource, boolean
Any idea how to do this?(And yes i know mysql is deprecated but i have to use it)
I have tested it
use:
SHOW DATABASES LIKE '%backup%'
here we go, its tested and working, but i will suggest you to use PDO, since mysql_* api is deprecated.
$sql="SHOW DATABASES LIKE '%backup%'";
$connect = mysql_connect("localhost", "root", "");
$query=mysql_query($sql,$connect) or die(mysql_error());
while($row = mysql_fetch_array($query)) {
echo $row[0]."<br/>";
}
Have you considered just listing all databases, then iterating through all results, matching them via regex/stripos and appending them into an array?
$sql="SHOW DATABASES";
$query=mysql_query($sql,$connect);
while ($row=mysql_fetch_assoc($query)) {
$var = preg_match("/backup/", $row['database'])
# Do whatever - append to array, echo, etc
}
EDITED
use mysql_select_db to select databases.see here http://php.net/manual/en/function.mysql-select-db.php
or you can use mysql_list_dbs but the problem is it will give all database. so using some loop you have to use your like operator to get what you want. see this:http://php.net/manual/en/function.mysql-list-dbs.php
I have marked many of developers using
$con1 = mysql_connect('localhost', 'username', 'password');
$rv1 = mysql_select_db('db1'); // not added connection
Instead
$con1 = mysql_connect('localhost', 'username', 'password');
$rv1 = mysql_select_db('db1', $con1); // added connection
Can I know the difference between this both?
Actually this both are giving same result
The mysql_ functions will implicitly use the last connection which was opened with mysql_connect, if you do not pass an explicit connection parameter. So, yes, in this case it's the same result whether you pass the parameter or not. You will get different results should you happen to open more than one connection using mysql_connect.
Note that implicitly relying on an open connection is bad practice, precisely because your application will screw up if you need to open more connections sometime later. Also note that the mysql_ API is dead, switch to mysqli or PDO.
Take a look at the PHP manual:
http://www.php.net/manual/en/function.mysql-select-db.php
Without a link identifier, mysql_select_db will use the last connection opened with mysql_connect or it will try to connect without any parameter. So the second one is safer and you could use multiple connections.
mysql_select_db and mysql_connect is deprecated though. You should switch to PDO or mysqli.
Taken from the documentation:
bool mysql_select_db ( string $database_name [, resource $link_identifier = NULL ] )
link_identifier
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.
Basically it will used the last known connection object if none is provided. In your case, the last known connection was created on the previous line. This is an optional parameter so the function will operate in the same way if the connection is passed explicitly or not.
On a related note, please notice the big red warning at the top of the documentation page I have linked to. The mysql_* extensions are deprecated and it is recommended that you use a different extension. For a more detailed explanation, please take a look at this thread:
Why shouldn't I use mysql_* functions in PHP?
I am having a problem in getting the string name of the currently selected database.
this is what i did.. i am trying to echo the name of the database but it produces an error "Warning: mysql_fetch_array() expects parameter 1 to be resource"
<?php
$dbcon = mysql_connect("localhost", "root", "")
or die(mysql_error()."Not Connected");
$sql = mysql_query("select database()");
$row = mysql_fetch_array($sql);
echo "db ".$row[0];
?>
i need to get the selected database name because i will use it for
mysql_select_db()
You should either select a database right after connecting using your login credentials. Or, if you want to see what databases the root user has access to, use:
SHOW DATABASES;
select Database() shows the name of the name of the slected database.
It will work only after you have already selected the database using mysql_select_db().
If you want to get the name of the databases available to you then use SHOW DATABASES
You yourself need to select a database after connecting to MySQL, it's not selected by default, like this:
$dbcon = mysql_connect("localhost", "root", "")
or die(mysql_error()."Not Connected");
mysql_select_db("databasename"); # add this line
$sql = mysql_query("select database()");
$row = mysql_fetch_array($sql);
echo "db ".$row[0];
Please don't use mysql_* function as they're deprecated, instead use mysqli_*.
first, I would suggest you use mysqli functions instead of mysql as they are being deprecated.
Then, you just select the database by adding the database name in the parameter to mysql_select_db():
mysql_select_db('dbname');
note that another advantage of mysqli is you can specify the database in the connect string
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
How do you connect to multiple MySQL databases on a single webpage?
If I want to connect to one db do some query, and then later do another query from another DB. How do I do it? Do I just
mysql_pconnect("host:3306", "user", "password") or die(mysql_error());
mysql_select_db("Test") or die(mysql_error());
//do some query
mysql_pconnect("host2:3306", "user", "password") or die(mysql_error());
mysql_select_db("Test") or die(mysql_error());
//do another query
Is that how you do it? A couple of questions. Notice I used pconnect, does that affect calling it twice on the same page? Also, do I have to close the connection for the first one before calling the second one?
You need to store database connection link in separate variable. For example
$connection_1 = mysql_connect("host:3306", "user", "password") or die(mysql_error());
mysql_select_db("Test", $connection_1) or die(mysql_error());
$connection_2 = mysql_pconnect("host2:3306", "user", "password") or die(mysql_error());
mysql_select_db("Test", $connection_2) or die(mysql_error());
mysql_query("your query", $connection_1); // run query for first connection
mysql_query("your query", $connection_2); // run query for second connection
You need to store the resource returned from mysql_connect and use it when doing mysql_select_db.
$res1 = mysql_pconnect(...);
mysql_select_db("Test", $res1);
$res2 = mysql_pconnect(...);
mysql_select_db("Test", $res2);
Then use $res1 or $res2 when querying the corresponding db.
mysql_query("select * from test_table", $res1);
mysql_query("select * from test_table", $res2);
Is that how you do it?
This will leave your script with two open connections to different hosts until it ends.
You may reuse either of these connections by calling mysql_pconnect again.
Notice I used pconnect, does that affect calling it twice on the same page?
From the docs:
The function would first try to find a (persistent) link that's already open with the same host, username and password
Since your hosts are different, there will be two different connections
Also, do I have to close the connection for the first one before calling the second one?
You cannot explicitly close a connection open with mysql_pconnect.
You did RTM, right, because you're not using the $link_identifier?
http://us.php.net/mysql_select_db :
bool mysql_select_db ( string $database_name [, resource $link_identifier ] )
Sets the current active database on the server that's associated with the specified link identifier. Every subsequent call to mysql_query() will be made on the active database.
Parameters
database_name
The name of the database that is to be selected.
link_identifier The MySQL connection.
If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.
Whenever you have use the pconnect, you're sharing the connection (not only within the page, but possibly with other pages) -- in your case here, don't do that. You want isolated links, therefore isolated transactions. You should probably consider mysql_connect instead, and explicitly using the new_link parameter. Lastly, use the $link_identifier explicitly, so you are clear what you are connecting to.