This question already has answers here:
mysql query result in php variable
(5 answers)
Closed 10 years ago.
How can I get the results?:
$check = mysql_query("select username, email, case
when max(username = '$username') > 0 and max(email = '$email') > 0 then 'both'
when max(username = '$username') > 0 then 'username'
when max(email = '$email') > 0 then 'email'
end
from school_users
WHERE username = '$username' or email = '$email'") or die(mysql_error());
I need "username" or "email" or "both" when it exists. How do I receive these variables?
You should use fetch functions.
mysql_query() returns resource of data, for getting this data, you need to use either mysql-fetch_assoc or mysql_fetch_array.
As already mentioned, mysql_* functions are deprecated as of >PHP 5.5, and they are bad practice of using. You should learn about mysqli or PDO.
Resources:
http://php.net/mysql_query
http://php.net/mysql_fetch_assoc
http://php.net/mysql_fetch_array
http://php.net/mysqli
http://php.net/pdo
Ok I think I understand what you are trying to do. You have alot more work than you think you do. What you need is template to work with so you can make it your own. The link below will allow you to create your own php API to communicate with a MySQL database in the manner you are talking about. It is step by step and he gives you the code to work with, all of it!
android login and registration with php mysql and sqlite
I used it and it it awesome, if you have any questions feel free to message me.
put the green check if this helps
Related
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 3 years ago.
Im trying to populate a php veriable from a MySql query to use later on when sending an email via php. Please see below below code for populating the veriable. I have not included the mysql_connect and mysql_select_db as this this a live db and i know the connections work. Also before you state that i should be using mysqli or POD i know but the server cannot be updated as there is a large number of pages that rely on the old code.
Error - Parse error: syntax error, unexpected 'echo' (T_ECHO)
$emailaddress = "SELECT e_mail FROM frm_change_approver WHERE user_id LIKE '$approvingmanagername'";
$result = mysql_fetch_array($emailaddress);
$approveremail = echo $result['e_mail'];
I need $approveremail to be populated via the above query as i already have the email address for the user in the database and dont want a user to type the wrong one, i only capture the users user_id in the form as i dont want there to be an email address field at all. I will then use the populated veriable to send the email to that person.
any help will be greatly appreciated.
You cannot assign an echo statement to a variable.
Change this:
$approveremail = echo $result['e_mail'];
To this:
$approveremail = $result['e_mail'];
echo $approveremail;
Or even:
echo $result['e_mail'];
Furthermore, please consider using mysqli or PDO instead of mysql_ functions. mysql_ function are deprecated and no longer supported in PHP 7.0 and above.
Take a look at this page
https://www.php.net/manual/en/function.mysql-fetch-array.php
You need to run the query and then fetch the result
$emailaddress = "SELECT e_mail FROM frm_change_approver WHERE user_id = '$approvingmanagername'";
$result = mysql_query($emailaddress);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$approveremail = $row['e_mail'];
Also, please consider to use mysql_real_escape_string() to sanitize your inputs https://www.php.net/manual/en/function.mysql-real-escape-string.php
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 3 years ago.
I try to select username from the database, and when I fetch it, I got the error message.
mysqli_stmt::fetch() expects exactly 0 parameters, 1 given in
<?php
require 'dbh.inc.php';
if(!empty($_POST['username']) && !empty($_POST['password'])):
$records = $conn->prepare('SELECT id, username, password FROM users WHERE username = ?');
$records->bind_param('s', $_POST['username']);
$records->execute() or die($records->error);
$result = $records->fetch(PDO::FETCH_ASSOC);
if(count($result) > 0 && password_verify($_POST['password'], $result['password'])){
die('We have a login');
}else{
die('Something went wrong!');
}
endif;
?>
You appear to be calling mysqli_stmt::fetch(), but expecting it to act like PDOStatement::fetch().
Even though the functions have the same name, they come from different extensions, and you can't mix them. If you want to use the PDO fetch(), you should connect to your database using PDO.
Also, the mysqli_stmt::fetch() function does not return a row of data. It relies on you binding results to variables.
I prefer the PDO style, where PDOStatement::fetch() returns a row. I recommend you convert your code to use PDO throughout.
You are using PDO fetch style with the fetch() MySQLi method.
Try this :
$result = $records->fetch();
If you really want to use the PDO fetch style, I recommend to use PDO instead of MySQLi.
This question already has answers here:
mysqli_affected_rows in PHP insert
(3 answers)
Closed 6 years ago.
I'm working with PHP and mysqli, what the program is doing is that it is asking for a reset code and email address if the email add and reset code are found in the database it sets the password,this part of the function is working,
I need help with this part: what I need to do is tell the user if the password was set or not so if the update was successful or not.
What I'm working on:
$uinsert = "UPDATE member SET password = '$password' WHERE emailadd = '$emailadd' AND resetCode = '$resetcode'";
$update = mysqli_query($mysqli, $uinsert) or die(mysqli_error($mysqli));
if(mysqli_affected_rows($update) == 1 ){ //ifnum
header("location: ../index.php"); // Redirecting To Other Page
}
else{
echo "<script> alert('Incorrect code, try again!');</script>";
}
Note: $mysqli is my connection string
"#Fred-ii- Thank you so much that works! – Coffee coder 58 secs ago"
Use if(mysqli_affected_rows($mysqli) >0 ) or no comparison at all.
Sidenote: ==1 is only comparing for 1, as opposed to >0 which you may be trying to update more than one row. However and on the rare occasion, >0 is required where this has also happened to me before; that is the reason of my answer.
affected_rows() uses the connection, not the one for the query.
http://php.net/manual/en/mysqli.affected-rows.php
Plus, if you're storing plain text passwords, use password_hash() since it's much safer:
http://php.net/manual/en/function.password-hash.php
Sidenote: If you do decide to move over to that function, make sure that you do not manipulate the password at all. Hashing/verifying it takes care of that and you may be doing more harm than good in doing so and limiting passwords.
I.e.: A valid password of test'123 would be interpreted as test\'123 and rendering FALSE when using real_escape_string() for example.
Or you may still be using hash_hmac as per your other question Comparing/check if correct Password from mysqli database [hash_hmac]
and a prepared statement:
https://en.wikipedia.org/wiki/Prepared_statement
It is also best to add exit; after header. Otherwise, your code may want to continue to execute.
header("location: ../index.php");
exit;
Change the parameter of mysqli_affected_rows(), the parameters must be the mysql connection
mysqli_affected_rows($update)
to
mysqli_affected_rows($mysqli)
Please see this reference
https://www.w3schools.com/php/func_mysqli_affected_rows.asp
if (mysqli_affected_rows($mysqli) == 1 ) {
Because mysqli_affected_rows() does not use the query $update as its parameter, it uses the connection variable: $mysqli
pass your mysqli connection object ($connection) to mysqli_affected_rows(connection_object) to check affected rows.
connection_object is like - $con=mysqli_connect("localhost","bd_user","db_password","your_db_name");
So , code will be
if(mysqli_affected_rows($con)== 1 ){
header("location: ../index.php");
}
This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 5 months ago.
I am trying to get an old PHP script to work but I keep getting this notice:
Notice: Trying to get property of non-object in ...
The notice is based on the last line of this code:
$result_id_check = mysql_query("select ses_userid from fe_sessions where ses_id = '".$_COOKIE['fe_typo_user']."';");
$row_check = mysql_fetch_object($result_id_check);
if ($row_check->ses_userid) {
I also tried using mysqli_query and mysqli_fetch_object but that won't take any changes.
Any ideas how I can resolve this?
This error normally means that the query failed.
You should be checking for errors as you go like this, also the string concatenation can be made simpler if you use either the {$_COOKIE['fe_typo_user']} form of variable expansion inside a double quoted string, or alternatively this will also work $_COOKIE[fe_typo_user]
$sql = "select ses_userid
from fe_sessions
where ses_id = '{$_COOKIE['fe_typo_user']}'"
$result_id_check = mysql_query($sql);
if ( $result_id_check === false ) {
echo mysql_error();
echo 'Bad SQL : ' . $sql;
exit;
}
$row_check = mysql_fetch_object($result_id_check);
This way when you make small mistakes with your SQL, you get told about them directly and you dont have to wonder what nay have gone wrong.
Please dont use the mysql_ database extension, it
is deprecated (gone for ever in PHP7)
Specially if you are just learning PHP, spend your energies learning the PDO database extensions.
Start here
You should also be using parameterized queries ( available in the mysqli_ and PDO database extensions, but not the old deprecated mysql_ extension) to avoid SQL Injection Attack Specially if you are using data got from $_POST or $_GET or $_COOKIE
If you are considering moving to mysqli_ or PDO you should read also read this Can I mix MySQL APIs in PHP?
Before using $row_check->ses_userid just check whether $row_check is true or false.
if ($row_check) {
if ($row_check->ses_userid) {
}
}
This question already has answers here:
MYSQL query returning 'resource id#12 instead of the numerical value it should return
(2 answers)
Closed 9 years ago.
EDIT::
The current code being used is :
//$username= 'lmfsthefounder';
$type = "SELECT account_type from user_attribs WHERE username='lmfsthefounder';";
$type_again = mysql_query($type);
$row = mysql_fetch_row($type_again);
$usertype = $row['account_type'];
echo $usertype;
which returns like this: Home Login Register Usertype is
When I search this query in mysql query explorer, it shows me the value is 1. I need that 1 to echo in my page.
(This should display 'Usertype is 1' in my navigation bar)
The mysql function you are looking for is mysql_fetch_assoc (link) instead of mysql_fetch_row.
But please, PLEASE don't use mysql_ functions. The link to the PHP manual tells you the same.
I would recommend trying to teach yourself how to use the mySQL PDO. There are many great tutorials online.