I have triggered the stored procedure from php. I have passed the input parameters also as shown.
$id = 1;
$nameDetail = 'raj';
$result = mysqli_query('CALL InsertDetails($id,$nameDetail)');
But getting below error.
mysqli_query() expects at least 2 parameters, 1 given ...
Please suggest a solution.
The issue is that you're not set the mysqli connection.please try this
$connection = mysqli_connect('localhost','username','password','db');
$result = mysqli_query($connection,'CALL InsertDetails($id,$nameDetail)');
You need to pass in the connection as the first parameter:
// connect to DB
$connect = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
// run procedure
$result = mysqli_query($connect, 'CALL InsertDetails($id,$nameDetail)');
Related
Decided to move to procedural style coding. I've tried the below code and receive no errors or returned data. What am I doing wrong? Blanked out the mysqli_connect parameters because I am using a shared server - assume the parameters are correct. Thanks in advance.
function login(){
$connect = mysqli_connect("**********", "******", "****", "*****");
$result = mysqli_query($connect, "SELECT * FROM `new_base`");
$bang = mysqli_fetch_all($result);
echo $bang; }
NOTE: The call to the function does work echoing a string does get returned to the page.
I am having problems updating mysql with php using varables.
mysqli_query($connection, "UPDATE passwords SET used=1, time_used='{$time}'
WHERE password='{$key}'
");
I was given the error:
Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in C:\wamp\www\key_check.php on line 47
any ideas why?
Thanks!
EDIT: Whole Code: http://pastebin.com/raw.php?i=W5cx8pBP
The "new mysqli" solution seems to be giving problems when trying to
$result = mysql_query("SELECT * FROM passwords", $connection);
Thanks :)
Your connection setting must look like
$connection = new mysqli($host,$username,$pass,$db);
Then execute the query using your way or by this way also
$query="UPDATE passwords SET used=1, time_used='{$time}'
WHERE password='{$key}'
";
$stmt = $connection->query($sql);
note: using prepared statements for mysqli can also possible and great. By somehow you also needed to bind parameters in there..
You have to declare $connection by creating a new mysqli object. If you fail to do so, you can check the documentation for mysqli constructor
Here's the code from the documentation.
$connection = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
if ($connection->connect_error) {
die('Connect Error (' . $connection->connect_errno . ') '
. $connection->connect_error);
I am getting errors (not really errors, just not working properly) and I'm trying to figure out if it's related to my sql connections. I have 2 separate connections to two entirely different databases.
$dshost = "kjbkb";
$dsdatabase = "kjhk";
$dsusername = "hjgfytdf";
$dspassword = "jhv";
mysql_connect($dshost,$dsusername,$dspassword);
mysql_select_db($dsdatabase) or die( "Unable to select database");
$sql = "SELECT * FROM users WHERE `username` = '".$_POST['paydl']."'";
$cheeseburger = mysql_query($sql);
$res = mysql_fetch_array($cheeseburger);
$autobus_user = $res['id'];
mysql_close(); //close first connection
$db_host = "khgv";
$db_user = "trdstx";
$db_password = "txz";
$db_database = "gfxx";
mysql_connect($db_host, $db_user, $db_password) or die("Unable to connect to host");
mysql_select_db($db_database) or die( "Unable to select database");
$ASDFASDF = "SELECT * FROM `autobus` WHERE `user_id` = '".$autobus_user."' LIMIT 3";
$BobDoleDontNeedThis = mysql_query($ASDFASDF);
$resnumbatwo = mysql_fetch_array($BobDoleDontNeedThis);
mysql_close(); //close 2nd connection
Am I doing this right? Why is $resnumbatwo returning false?
When using multiple MySQL connections in PHP, you have to supply a fourth argument telling PHP to actually create new connections like this (this is very important, if you are using two connections to the same host):
$db1 = mysql_connect($host1, $user1, $passwd1, true);
$db2 = mysql_connect($host2, $user2, $passwd2, true);
If the fourth argument is not used, and the parameters are the same, then PHP will return the same link and no new connection will be made.
After this you should use "mysql_query" with an extra parameter than defines which connection to use:
$res1 = mysql_query($sql1, $db1) or die(mysql_error($res1));
$res2 = mysql_query($sql2, $db2) or die(mysql_error($res2));
The description of the fourth parameter from php.net is:
"If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored."
See more at:
http://www.php.net/manual/en/function.mysql-connect.php
mysql_fetch_array will return false if there are no matching records returned from the query. Please see the following entry from the API reference docs:
Return Values:
Returns an array of strings that corresponds to the fetched row, or
FALSE if there are no more rows. The type of returned array
depends on how result_type is defined. By using MYSQL_BOTH
(default), you'll get an array with both associative and number
indices. Using MYSQL_ASSOC, you only get associative indices (as
mysql_fetch_assoc() works), using MYSQL_NUM, you only get number
indices (as mysql_fetch_row() works).
Better save each connection handler in a variable and transmit it to correspondent mysql_query().
Example:
$dbch1 = mysql_connect($dshost, $dsusername, $dspassword);
$dbch2 = mysql_connect($db_host, $db_user, $db_password);
$cheeseburger = mysql_query($sql, $dbch1);
$BobDoleDontNeedThis = mysql_query($ASDFASDF, $dbch2);
You can use
$dbch1 = mysql_connect($dshost, $dsusername, $dspassword,true);
$dbch2 = mysql_connect($db_host, $db_user, $db_password,true);
$cheeseburger = mysql_query($sql, $dbch1);
$BobDoleDontNeedThis = mysql_query($ASDFASDF, $dbch2);
Using php7
you can use the mysqli_change_user() function.
Like in the examples at the php.net page: http://php.net/manual/en/mysqli.change-user.php
require("includes/connect.php");
$result = mysql_query("SELECT * FROM entries", $link);
while ($row = mysql_fetch_array($result)) {
htmlentities($row['quotes']);
}
I am trying to display data that is in the database, but I keep on getting:
Warning: mysql_real_escape_string() expects parameter 1 to be string
Is there anything wrong in the above code that is causing the problem? I am quite new to PHP and I am trying to understand what's going on and why it's doing it.
connect.php
$link = mysql_connect("localhost", "root", "");
if (!$link) {
die("Could not connect to the db");
}
mysql_select_db("ENTRIES", $link);
(I'm working on this locally, so user/pass really isn't important right now)
I don't see the point with escaping the above query, but you could do it like this:
$result = mysql_query(mysql_real_escape_string("SELECT * FROM entries"), $link);
You should read the documentation: mysql_real_escape_string()
As the error explains mysql_real_escape_string() takes a string as a parameter. In your code you posted as a comment you are passing $link which isn't a string, it's a database connection.
As #kristen, has said to solution should be to wrap you sql statement like so
$result = mysql_query(mysql_real_escape_string("SELECT * FROM entries"), $link);
If you are still receiving the error after this, you must be using the function elsewhere.
Okay I'm new to php and mysql I tried to build a web page hit counter but my page counter dosen't update correctly and it gets the following warnings listed below.
Warning: mysqli_query() expects at least 2 parameters, 1 given in
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in
Here is the php code below.
<?php
$page = $_SERVER['SCRIPT_FILENAME'];
// Query member data from the database and ready it for display
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT id page FROM mysql_counter_logs WHERE page = '$page'");
if (mysqli_num_rows($dbc) == 0) {
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"INSERT INTO mysql_counter_logs (page) VALUES ('$page')");
mysqli_query($dbc);
} elseif (mysqli_num_rows($dbc) == 1) {
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"UPDATE mysql_counter_logs SET hits = hits + 1 WHERE page = '$page'");
mysqli_query($dbc);
}
//Retreives the current count
$count = mysqli_fetch_row(mysqli_query($mysqli,"SELECT hits FROM mysql_counter_logs"));
if (!$dbc) {
// There was an error...do something about it here...
print mysqli_error();
}
//Displays the count on your site
print "$count[0]";
?>
An extract of your code :
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"INSERT INTO mysql_counter_logs (page) VALUES ('$page')");
mysqli_query($dbc);
First of all, according to the manual of mysqli_query, when used as a function :
mixed mysqli_query ( mysqli $link , string $query [, int $resultmode ] )
Which means you have to pass your $mysqli variable as first parameter, and your query as a second parameter.
What I don't get is why you are using mysqli_query this way on the third line I copy-pasted, while you are using it correctly in the other parts of your code ?
Are you sure you mean to use that function, here ?
Also, knowing on which lines you are getting those error messages might help ;-)
Check you don't do anything else "wrong" there -- like " mysqli_query() expects at least 2 parameters, 1 given "
Thinking about it : mysqli_query will return boolean false if there is an error.
In that case, you should not call mysqli_num_rows on the return value of mysqli_query, as it's not a valid mysqli_result -- hence the second error message.
You are checking for $dbc at the end of your script, but it might be interesting to check it too a biit sooner -- actually, after each call to mysql_query, I'd say.
Also, as a sidenote : you probably don't need to instance the mysqli class that many times : just instanciate it once, and then use the $mysqli variable for there rest of your script.
Just for security, and to avoid any trouble, you might also want to use mysqli_real_escape_string on the $page variable, before injecting it into the queries.
Oh, and, btw : you might be interested by the INSERT ... ON DUPLICATE KEY UPDATE Syntax that MySQL allows to use ;-)
(Well, that is if page is the primary key of your table, at least...)