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);
Related
So here's the problem, the script I purchase is written on PHP 5.x, and I'm using xampp with PHP7.x installed for development. Now I want to migrate my script to PHP7.x. Now I know this was asked a million times already but do you mind if you could take a look at my code and give your thoughts about it, or simply share your knowledge. I would deeply appreciate it.
Here is the code for my config.php
<?php
// mySQL information
$server = 'localhost'; // MySql server
$username = 'admin'; // MySql Username
$password = 'admin' ; // MySql Password
$database = 'arcade'; // MySql Database
// The following should not be edited
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
$con = mysql_connect($server, $username, $password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($database, $con);
// Get settings
if (!isset($install)) {
$sql = mysql_query("SELECT * FROM ava_settings");
while ($get_setting = mysql_fetch_array($sql)) {
$setting[$get_setting['name']] = $get_setting['value'];
}
}
?>
The deprecated functions are:
mysql_connect()
mysql_error()
mysql_fetch_array()
mysql_query()
mysql_select_db()
Now, I don't want to use the PDO approach, I want to use mysqli instead. Am I suppose to just replace the mysql_* into mysqli_*? So it will become like these? I don't want to hide/surpress the deprecate warnings.
mysqli_connect()
mysqli_error()
mysqli_fetch_array()
mysqli_query()
mysqli_select_db()
I just offer you that migrate to PDO driver. Because every update you may see a lot of deprecation errors.
But if you can not do it the first thing to do would probably be to replace every mysql_* function call with its equivalent mysqli_*, at least if you are willing to use the procedural API -- which would be the easier way, considering you already have some code based on the MySQL API, which is a procedural one.
Note that, for some functions, you may need to check the parameters carefully: Maybe there are some differences here and there -- but not that many, I'd say: both mysql and mysqli are based on the same library (libmysql ; at least for PHP <= 5.2)
Look at difference between mysqli and mysql:
$mysqli = mysqli_connect("example.com", "user", "password", "database");
$res = mysqli_query($mysqli, "SELECT ...");
$row = mysqli_fetch_assoc($res);
echo $row['_msg'];
$mysql = mysql_connect("example.com", "user", "password");
mysql_select_db("test");
$res = mysql_query("SELECT ...", $mysql);
$row = mysql_fetch_assoc($res);
echo $row['_msg'];
I hope this question hasn't been posted/answered elsewhere. Searching didn't yield any satisfactory results so I am posting in the hope that someone will be able to help me out.
I used the code below to display images from mysql database. Used to work perfectly with php 5.6. Today I upgraded to php7 and I simply cannot get it to work.
Displays error message:
Could not get data:.
Here is the code. Please help if you can. Will be highly appreciated.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysqli_error());
}
$sql = 'SELECT * FROM table ORDER BY id DESC LIMIT 5';
mysqli_select_db('my_db');
$retval = mysqli_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysqli_error());
}
while($row = mysqli_fetch_array($retval, mysqli_NUM))
{
echo "{$row[0]}". "<em>" . "({$row[1]})"."</em>"."<br>";
}
mysqli_close($conn);
?>
Thanx for all the helpful comments. I have edited my code. It now displays the path and image filename but not the image. (images/avatars/filename.jpg). Here is the revised code:
mysqli_select_db($conn, 'my_db');
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
die('Could not get data: ' . mysqli_error($conn));
}
while($row = mysqli_fetch_array($retval, MYSQLI_NUM))
{
echo "{$row[0]}". "<em>" . "({$row[1]})"."</em>"."<br>";
}
mysqli_close($conn);
?>
Hi guys. I managed to get it working. Thank you all for your assistance and patience. As suggested I changed to echo "<img src='".$row['0']."'/>"."<br>";
echo "{$row[1]}". "<em>" . "({$row[2]})"."</em>";
I think you have the db Link and Query params swapped..
$retval = mysqli_query( $sql, $conn );
Should be
$retval = mysqli_query( $conn, $sql );
There are a few errors in your code.
mysqli_select_db('my_db') requires a db connection as the first parameter.
mysqli_select_db($conn, 'my_db')
However, you could have just as easily used all 4 parameters:
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, 'my_db');
Reference:
http://php.net/manual/en/function.mysqli-connect.php
Then:
mysqli_error() requires a parameter for it.
mysqli_error($conn)
Then:
mysqli_query( $sql, $conn ) You need to inverse those, the connection must be first.
mysqli_query( $conn, $sql )
References:
http://php.net/manual/en/mysqli.select-db.php
http://php.net/manual/en/mysqli.error.php
http://php.net/manual/en/mysqli.query.php
And just to be certain, table if that's your real table name, is a MySQL reserved word:
https://dev.mysql.com/doc/refman/5.5/en/keywords.html
So wrap it with ticks
$sql = 'SELECT * FROM `table` ORDER BY id DESC LIMIT 5';
Footnotes:
Since MYSQLI_NUM is a constant in PHP, the mysqli_NUM may fail here, so you may need to make it all in uppercase MYSQLI_NUM.
Reference:
http://php.net/manual/en/mysqli.constants.php
Additional notes:
Your question's title holds "Cannot retrieve and display images".
I don't see where you're wanting to display an image here, as there are no <img> tags.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img
"Displays error message:
Could not get data:"
This tells me that either your PHP configuration is wrong, or you're accessing it as file:/// rather than http://localhost.
Or, as I already outlined, mysqli_error() requires a db connection parameter where yours does not include it and is erroring out but you're not seeing the message it's throwing.
Use error reporting also and to catch and display:
http://php.net/manual/en/function.error-reporting.php
As Blake outlined in comments:
"Did you update the mysql package, too? PHP7 uses a different package."
So, make sure your installation was successful and that all system files were correctly updated and pointing to the right path/.ini file(s), and that you restarted all services and are running.
Consult Migrating from PHP 5.6.x to PHP 7.0.x:
http://php.net/manual/en/migration70.php
As stated by RiggsFolly in comments:
"You are using mysqli_fetch_array() after doing a SELECT * so we have ZERO idea what columns are returned and what is in any of the columns."
and...
"PS: Using mysqli_fetch_array() with SELECT * is quite dangerous. If someone alters the order of the columns in the database, you are likely to get different columns returned in $row[0] than you where when you tested this code. Use mysqli_fetch_assoc() then you get NAMED columns like $row['id'] and $row['filename']."
I will say this again; you need to use <img src...> in order to show the images. Your code as shown, will not automagically show the images, that's what <img> is for.
I believe that you used to use mysql_*() functions in the previous php version and changed to mysqli when you upgraded to php7.
Although mysqli can be used very similarly to the mysql extension, there are subtle differences.
You can select the default db when you create the connection.
The connection and query parameters are reversed in mysqli_query().
There are other differences as well, so pls read the documentation before adding a letter i to the mysql_*() functions!
I am working on a login script with prepared statements in PHP procedural mysqli syntax. Here is my current code:
<?php
include "/ssincludes/functions.php";
$host = HOST;
$username = USER;
$password = PASSWORD;
$db_name = DATABASE;
$table = TABLEU;
//These includes and constants are fine I checked them all
$con = mysqli_connect($host, $username, $password, $db_name);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$myusername='test';
$mypassword='password1';
$sql="SELECT * FROM $table WHERE user_name=? and password=?";
$result=mysqli_prepare($con, $sql);
mysqli_stmt_bind_param($result, 'ss', $myusername, $mypassword);
mysqli_execute($result);
mysqli_stmt_fetch($result);
$row_cnt = mysqli_num_rows($result);
echo $row_cnt;
?>
The error returned is: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, object given
I thought I took out all instances of OO PHP in my script? Also I understand that this may mean my query is incorrect so I ran it on MySQL in the database and all seems to be fine there:
So I am lost as to what the problem could be. I read many similar posts (maybe I'm missing one that is exactly similar to mine) and none seem to handle the problem. I appreciate your time and help.
P.S. I understand the security issues with plain text passwords and using "password1". I plan to use better security practices as I build this but I just want to get prepared statements down first.
You should use
mysqli_stmt_execute
mysqli_stmt_num_rows
Instead of the mysqli_execute and mysqli_num_rows.
This question already has answers here:
How to change mysql to mysqli?
(12 answers)
Closed 7 years ago.
i have a website that is online about 5 years. just now I noticed that i use mysql connection.
I want to change it to mysqli.
So, I have some questions.
To change mysql to mysqli I just need to put "i" after all mysql words?
query
eg
$i=mysql_query("INSERT
to
$i=mysqli_query("INSERT
num rows:
$num = mysql_num_rows($rs);
to
$num = mysqli_num_rows($rs);
string escape:
mysql_real_escape_string
to
mysqli_real_escape_string
fetch arraw
$l = mysql_fetch_array($re);
to
$l = mysqli_fetch_array($re);
is that simple? or i need to know something else?
No it's not that simple..mysqli functions usually take a second parameter...so check the syntax of whatever functions you are changing.
For instance..instead of mysql_connect("localhost", "my_user","my_password"); for mysqli you will use mysqli_connect("localhost","my_user","my_password","my_db");
This will actually guide you through: http://lv1.php.net/manual/en/class.mysqli.php
While mysql was only the procedure-style function, the new mysqli is both: the procedure-style and object-style.
You can use the object-oriented style, like:
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
or procedural style:
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
The parameters of mysqli are reversed I.e. mysql_query($q, $dbc) becomes mysqli_query($dbc, $q). Also some functions take extra parameters, usually the connection . Php.net has a full list of all changes.
UPDATE:
//your connection script should look somthing like this.
$db_connnect = mysqli_connect('localhost','username','password','db_name');
//the page you require the connection on should look something like this.
require_once ('db_connect.php');
//a mysqli query would look something like this.
$q = "select * from user limit 12";
$r = mysqli_query("$db_connnect, $q");
while($row = mysqli_fetch_array($r)) { ... }
This question already has answers here:
Warning: mysqli_error() expects exactly 1 parameter, 0 given error
(4 answers)
Closed 2 years ago.
I am trying to get my head around mysql. Can someone tell my why this mysql query is not working? I am getting the following error:
Warning: mysqli_error() expects
exactly 1 parameter, 0 given in
/home/freebet2/public_html/test.php on
line 11
test.php
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/db.php');
$conn = db_connect();
$result = $conn->query("ALTER TABLE users ADD COLUMN refer_old INT(10) AFTER refer_id");
if(!$result){
echo "Error with MySQL Query: ".mysqli_error();
}
?>
db.php
<?php
function db_connect() {
$result = new mysqli('localhost', 'user', 'password', 'db');
if (!$result) {
throw new Exception('Could not connect to database server');
} else {
return $result;
}
}
?>
If I change the alter string to something like : $result = $conn->query("SELECT * FROM users refer_id"); I get no error for some reason.
You are mixing the object-oriented and the procedural styles of the mysqli API :
You are using object-oriented :
$result = new mysqli('localhost', 'user', 'password', 'db');
And, then, procedural :
echo "Error with MySQL Query: ".mysqli_error();
You should use either OO, or procedural -- but not both ; and if you choose procedural, the functions expect the link identifier passed as a parameter.
For instance, mysqli_error should be called either using the object-oriented API :
$link = new mysqli(...);
echo $link->error;
Or the procedural API :
$link = mysqli_connect(...);
echo mysqli_error($link);
(Of course, it will not change the fact that you are having an error in your SQL query, but it'll allow you to get the error message, which should help finding the cause of that error)
As far as the sql error is concerned, does 'user' have permissions to alter the table?
Use mysqli_error($result) as mysqli_error expects the connection to be passed as a parameter.