This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
guys! I'm in trouble with my MySQL database. When I try to access the fields it doesn't return the exact value. Here is the code.
<?php
$host = "localhost";
$user = "******";
$pass = "******";
$db = mysql_connect($host, $user, $pass) or die("Unable to connect. Check your connection parameters.");
mysql_select_db("*****") or die("Unable to select database!");
$form_username=$_POST["username"];
$form_password=$_POST["password"];
$query="
SELECT username, password FROM users
";
$result=mysql_query($query,$db) or die("Unable to send the query".mysql_error());
$index=0;
while($row=mysql_fetch_row($result))
{
$username[$index]=row[0];
$password[$index]=row[1];
$index++;
}
for($i=0; $i<=$index; $i++)
{
if($form_username==$username[$i]&& $form_password==$password[$i])
{
session_start();
$_SESSION["login"]="OK";
header("Location: ************");
die();
}
}
The if statement inside the for operator returns false for every given value. When I echo every username and password like this:
echo $form_username." ".$username[0]." ".$form_password." ".$password[0]."<br>";
echo $form_username." ".$username[1]." ".$form_password." ".$password[1]."<br>";
echo $form_username." ".$username[2]." ".$form_password." ".$password[2]."<br>";
It echo me this:
admin r 12345 o
admin r 12345 o
admin r 12345 o
I really don't know where the problem is.
I'll really appreciate your help.
Should this bit:
while($row=mysql_fetch_row($result))
{
$username[$index]=row[0];
$password[$index]=row[1];
$index++;
}
Read:
while($row=mysql_fetch_row($result))
{
$username[$index] = $row[0];
$password[$index] = $row[1];
$index++;
}
Note missing $ on the variable names.
Related
This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 4 years ago.
Here is my php and mysql code. It don't show any data . please tell me where is my error:
<?php
$ddaa = mysql_query("SELECT ref FROM users WHERE id='$uid'");
$mallu2 = mysql_query("SELECT mallu FROM users WHERE id='$ddaa'");
$result = mysql_fetch_array($mallu2);
echo $result['mallu'];
?>
You can use Mysqli,mysql is deprecated
a little example:
conection to db test
$mysqli = new mysqli('127.0.0.1', 'user', 'password', 'test');
if ($mysqli->connect_errno) {
echo "Error: Errot on connection : \n";
echo "Errno: " . $mysqli->connect_errno . "\n";
}
// the query
$sql = "SELECT ref FROM users WHERE id=$uid";
//if don't have result
if ($resultado->num_rows === 0) {
echo "we can't find data with $uid. try again !.";
exit;
}
//print the result
while ($dato = $resultado->fetch_assoc()) {
echo $dato['ref'];
}
Mysqli Php documentation
This question already has an answer here:
Why does mysqli num_rows always return 0?
(1 answer)
Closed 6 years ago.
I want to make instant search (google like) on key up Jquery ajax must ass value from HTML input field to PHP and PHP must chec in SQL table named "title" for any words which Begin or Contain the written word/letter,if there isn't anything found it must print the results out in a div.
Here is an example:
The picture explains: Up is the input field and down box is the box for results to be printed,as we can see it is working,but PHP don't want to get data from SQL,and only printing the result for 0 value (Nothing Found) on Bulgarian language.
There is my code:
<?php
$hostname = "localhost";
$username = "shreddin";
$password = "!utf55jyst";
$databaseName = "shreddin_nation";
$connect = new mysqli($hostname, $username, $password, $databaseName);
$fsearch = "";
if (!empty($_POST['fsearch'])) {
$fsearch = $_POST['fsearch'];
$req = $connect->prepare("SELECT title FROM food_data_bg WHERE title LIKE ?");
$req->bind_param('s', $fsearch);
$req->execute();
if ($req->num_rows == 0) {
echo 'Не бяха намерени резултати!';
}
else {
while ($row = $req->fetch_array()) {
?>
<div class = "search-result">
<span class = "result-title">
<? php
echo $row['title'];
?>
</span><br>
</div>
<?php
}
}
}
?>
The code is working till else {...} only this part didn't work..;/
I tried to use echo some results after else {...} because i thought it was a problem with my code,but it didn't work either way ...Can somebody explain to me where is my mistake (with simple language please) i am not really good at coding exept with PHP.
I won't put Jquery and HTML here because all working fine there, the post method is all good, the problem is with the php. But of course if you need it to help me I will paste it with no problem.
Edited
$value = '%'.$fsearch.'%;
$req->bind_param('s', $value);
it will work :)
<?php
$hostname = "localhost";
$username = "username";
$password = "pass";
$databaseName = "dbName";
$connect = new mysqli($hostname, $username, $password, $databaseName);
$fsearch="";
if(!empty($_POST['fsearch'])) {
$fsearch = $_POST['fsearch'];
$req = $connect->prepare("SELECT title FROM food_data_bg WHERE title LIKE ?");
$value = '%'.$fsearch.'%';
$req->bind_param("s", $value);
$req->execute();
$req->store_result();
if ($req->num_rows == 0){
echo 'Няма резултати';
}
else{
echo 'ДАА';
}
}
FINALY !!! that is the final result,it is printing ДАА when there is a result found and Няма резултати when there isn't any results fixed it after 1 month of pain lol Thanks to everyone which helped me <3<3 <3 <3 <3
I´m trying to create a simple login script for my Website (with PHP & Mysql). Created the original script with plain php & mysql commands and everything worked just fine. Now i wanted to exchange the old mysql commands with mysqli commands. Somehow i´m now getting the error "Trying to get property of non-object *** on line 11" when I test my script. Could somebody explain exactly to me what causes that problemn and how to solve it (because I dont really understand the error here)?
Login Script:
<?php
session_start();
?>
<?php
include_once "db_connect.php";
$username = $_POST["username"];
$password = md5($_POST["password"]);
$abfrage = "SELECT username, password FROM login WHERE username LIKE '$username' LIMIT 1";
$ergebnis = mysqli_query($verbindung,$abfrage);
$row = mysqli_fetch_assoc($ergebnis);
if ($row->password === $password) { <--- Line 11
$_SESSION["username"] = $username;
if ($username != "admin") {
echo "Login erfolgreich. <br> Geschützter Bereich";
}
else {
echo "Login erfolgreich. <br> Geschützter Bereich";
}
}
else {
echo "Benutzername und/oder Passwort sind falsch.";
}
?>
$row is an associative array, because you have used $row = mysqli_fetch_assoc($ergebnis); but you are treating $row as an object i.e.
$row->password
So try:
if ($row['password'] === $password)
<?php
session_start();
include_once "db_connect.php";
// either use require_once + bail-out code in db_connect.php
// or check the connection resource/object here.
if ( !$verbindung || $verbindung->connect_errno ) {
die('sorry, db error. try again later');
}
$password = md5($_POST["password"]); // md5, unsalted ...not secure anymore. see http://docs.php.net/password_hash
// see http://php.net/security.database.sql-injection
$abfrage = sprintf( // password is a reserved word in mysql -> backticks around the field id
"SELECT `username`, `password` FROM login WHERE username LIKE '%s' LIMIT 1",
mysqli_real_escape_string($verbindung, $_POST["username"])
);
$ergebnis = mysqli_query($verbindung,$abfrage);
// mysqli_query may fail at any time -> error handling required
if ( !$ergebnis ) {
echo 'db query failed'; // $verbindung->error should contain more information
}
else if ( !($row = mysqli_fetch_assoc($ergebnis)) ) {
echo 'no result'; // you probably shouldn't make a distinction between "no such record" and "wrong password" - just for illustration
}
else if ($row['password'] === $password) { // fetch_assoc returns an array, not an object
$_SESSION["username"] = $username;
}
use that like this
$row = mysqli_fetch_assoc($ergebnis);
if ($row['password'] === $password) {
Try with $row["password"]==$password
If it still shows the same thing, then var_dump $row and see if it returns a result.
I am really new on php and I am trying to create my own php shop cart. After some research I got myself stuck in the "function products" below because seems to me it is not working properly. I expect to see the names of my products on my mysql database but it is not showing anything. My user name is noivaemd_etalhes, I am using my correct password and my database name is noivaemd_cart and I created on this database the table called Products with my list of products available. Can anybody help me to figure out what am I doing wrong on the php instructions below???? I appreciate any help.
<?php
session_start();
$page = 'index.php';
function products() {
$con = mysqli_connect("localhost", "noivaemd_etalhes", "mypassword", "noivaemd_cart") or die (mysqli_error());
$res = mysqli_query($con, "SELECT id, name, description, price FROM Products WHERE quantity > 0 ORDER BY id DESC");
if (mysqli_num_rows($res)==0) {
echo "<font family=verdana><font size=6px><font color= #90882C><font style=normal><font variant= normal><br>No products available<br></font>";
}
else{
while($get_row = mysqli_fetch_assoc($res)) {
echo '<p>'.$res['name'].'</p>';
}
}
}
?>
This code:
while ($get_row = mysqli_fetch_assoc($res)) {
echo '<p>'.$res['name'].'</p>';
}
Should be:
while ($get_row = mysqli_fetch_assoc($res)) {
echo '<p>'.$get_row ['name'].'</p>';
}
As your title ask also tell how to check if the mysqli database connection is successful you can use the below code:
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Reference link: http://www.php.net/manual/en/function.mysqli-connect.php
It's not good practice to connect to your database directly in your code. Open another file, and save it has 'dbconnect.php' or whatever you choose to name it. Save it in the same root.
Inside DB connect, you connect to the database like this:
<?php
$mysql_host = "localhost";
$mysql_user = "username";
$mysql_pass = "";
$mysql_dbname = "Products";
$conn_error = "Sorry, Could Not Connect";
if(!#mysql_connect($mysql_host,$mysql_user,$mysql_pass)||!#mysql_select_db($mysql_dbname)){
die($conn_error);
}
?>
In your index.php, inside your php tags, write 'require "dbconnect.php";'.
Then you get your values like this:
$InfoQuery = " SELECT id, product, name FROM table_name WHERE quantity>0 ORDER BY id DESC";
$Info = mysql_query($InfoQuery);
while($InfoRow=mysql_fetch_assoc($Info)){echo "<p>".$InfoRow['id']."<br>". $InfoRow['product']"."<br>". $InfoRow['name']."</p>";}
Edit: What you did wrong is in your while loop, you fetched the table data from $res when it should be fetched from $get_row
Firstly, Sorry for bad English :(
I want key system like steam, origin or uplay. I give keys to user, user write that code into input, and code will echo user's code.
I have 1 Table. 2 Columns in it. 2 Rows for 2 Column. Like this:
.----------------------.
| sifre | bizimkey |
|______________________|
| A5Sr2A | First Code |
|______________________|
| FaQ1fS | Scnd. Code |
|______________________|
If user enters A5Sr2A into Input, PHP Echos "First Code". If user enters wrong code, Just appear alert.
I tried so many codes. I can run the code like this:
<?php
if ($_POST['pass'] == "A5Sr2A") {
{
echo "First Code";
}
} else {
header('Location:index.html');
}
?>
But I don't want this. This is so challenging thing. I asked myself, "Why don't you use MySQL?"
I am trying 7 Hours. Really. I want to do that. I want learn MySQL. Please Help. THANKS!
<? ob_start(); ?>
<html>
<link rel=stylesheet href="style.css">
<table>
<tr>
<td>
<center>
<?php
$host = "localhost";
$user = "user";
$password = "pass";
$database = "db";
$con = mysqli_connect($host, $user, $password, $database);
if (mysqli_connect_errno()) {
echo "ERROR";
}
$result = m ysqli_query($con, "SELECT sifre FROM keyler");
if ($_POST['pass'] == $ result) {
{
$mykey = m ysqli_query($con, "SELECT bizimkey FROM keyler");
echo $mykey;
}
} else {
header('Location:index.html');
}
mysqli_close($con);
?>
</center>
</td>
</tr>
</table>
</html>
<? ob_flush(); ?>
Okay, see below for a basic implementation example:
$host = "localhost";
$user = "user";
$password = "pass";
$database = "db";
$con=mysqli_connect($host,$user,$password,$database);
if(mysqli_connect_errno()!=0)//mysqli_connect_errno() returns 0 when there are no errors
{
echo "ERROR";
}
$sifre_test = mysqli_real_escape_string($con,$_POST['pass']);
$result = mysqli_query($con,"SELECT '1' FROM `keyler` WHERE `sifre`='".$sifre_test."'");
if($result!==false)
{
//If $result !== false, the query was successful, so we'll try to grab a row
$row = mysqli_fetch_assoc($result);
//$row will be null if there wasn't a row found where `sifre`=$sifre_test
if(!is_null($row))
{
$result = mysqli_query($con,"SELECT `bizimkey` FROM `keyler` WHERE `sifre`='".$sifre_test."'");
if($result!==false)
{
$row = mysqli_fetch_assoc($result);
$bizimkey = $row['bizimkey'];
echo $bizimkey;
}
else
{
die("bizimkey query failed");
}
}
}
else
{
die("sifre query failed");
}
For a better understanding and more examples, see the documentation links below:
mysqli_query
mysqli_fetch_assoc