What is the issue with this mysqli statement [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am not quite sure how to convert the mysql code here to a mysqli version. I keep getting the error like:
Warning: mysql_result() expects parameter 1 to be resource, object given in....
Can you please help? thanks.
<?php
function the_user($username) {
$myqli = mysqli_connect("localhost", "root", "", "sometable");
$username = sanitize($username);
$user_query = mysqli_query($myqli, "SELECT COUNT('user_id') FROM 'users' WHERE 'username' = '$username'");
return (mysql_result($user_query, 0) == 1) ? true : false;
}
?>

The 's around the column names & table name. Should be -
SELECT COUNT(user_id) FROM users WHERE username = '$username'
Or backticks.
Also mixing mysql & mysqli.

Instead of mysql_result you should use mysqli like mysqli_fetch_array.
You should use :
$row = mysqli_fetch_array($user_query, MYSQLI_NUM);
return ($row[0] == 1) ? true : false;
Instead of:
return (mysql_result($user_query, 0) == 1) ? true : false;
Ofcourse don't forget to fix sql syntax as described in earlier answer:
$user_query = mysqli_query($myqli, "SELECT COUNT(user_id) FROM users WHERE username = '$username'")

Related

I got the json data false. [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Here Is my PHP code to encode the JSON data from MySQL database. And this is my URL http://fwtest.ga/appoint.php; I tested on the online website JSONLint to validate my JSON data. It is valid, but I got the result [false] instead of the data in JSON format. Can anybody tell what am I doing wrong?
<?php
$host = "my_host";
$user = "user";
$password = "pass";
$db = "db_name";
$con = mysqli_connect($host, $user, $password, $db);
$sql = "select time, date from table_name;";
$result = mysqli_query($con, $sql)
or die("Error: ".mysqli_error($con));
$response = array();
while ($row = mysqli_fetch_array($result))
{
array_push($response, array("time" >= $row[1], "date" >= $row[2]));
}
echo json_encode(array("server_response">= $response));
echo (json_last_error()=== JSON_ERROR_UTF8);
mysqli_close($con)
?>
Probably because you're returning a single boolean here:
json_encode(array("server_response">= $response));
↑
That's not the array operator.

PDO Update statement, work in PHPmyadmin but not in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to make an update statement with PDO and i found out it doesn´t work.
I have testet the SQL statement in phpMyadmin and it works if i put '' arround the passkey, but why wont it work with this ?
INFO:
The passkey is a md5 string
<?php
include('../mysql/pdoconn.php');
$passkey = $_GET['passkey'];
$stmt = $conn->prepare("UPDATE user SET com_code='' WHERE com_code = :passkey");
$stmt->bindParam(':passkey', $passkey , PDO::PARAM_STR);
$stmt->execute;
$error = "Jon Snow";
$stmt1 = $conn->prepare("SELECT com_code from user where com_code =''");
$stmt1->execute;
$result = $stmt1->fetchColumn();
if($result === "")
{
$error = 'Your account is now active. You may now Log in';
$conn = null;
} else
{
$error = $passkey;
$conn = null;
}
?>
i have tested that it gets the passkey, and it does, but it dont update the table...
I have tried anything, but i cant get it to work
$stmt = $conn->prepare("UPDATE user SET com_code='' WHERE com_code = :passkey");
$stmt->bindParam(':passkey', $passkey , PDO::PARAM_STR);
$stmt->execute();
execute() is a function
You don't need to quote bound parameters

Error in mysqli_num_rows [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
What's wrong with this code:
<?php
session_start();
if(!isset($_SESSION['username']) && isset($_COOKIE['username'], $_COOKIE['password']))
{
$checkQuery = "SELECT password, id FROM accounts WHERE username='".$db->real_escape_string($_COOKIE['username'])."'";
$checkResult = mysqli_query($db, $checkQuery);
$check = mysqli_fetch_array($checkResult);
if($check['password'] == $_COOKIE['password'] && mysqli_num_rows($checkQuery)>0)
{
$_SESSION['username'] = $_COOKIE['username'];
$_SESSION['userid'] = $check['id'];
}
}
?>
It shows this error:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result,
string given...
Looks like you should change
mysqli_num_rows($checkQuery)
to
mysqli_num_rows($checkResult)

Problems with mysqli [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to read some data from a form and then use them using php. I use only mysqli in my php file but I get some warnings and i don't understand why. Here is some code:
<?php
// Connects to your Database
$con = mysqli_connect("localhost", "root", "password", "database");
error_reporting(E_ALL);
ini_set('display_errors', 1);
$all_string = $_GET['name'];
if($all_string == NULL) exit("Nothing is written\n");
echo "$all_string"."<br/>";
$tok = strtok($all_string, ",");
$sql = "SELECT * FROM Suggrammata";
/*line 22*/$results = mysqli_query($sql, $con);
/*line 24*/while($is = mysqli_fetch_array($results))
{
.
.
.
?>
Here are the warnings:
Warning:
mysqli_query() expects parameter 1 to be mysqli, string given in
/var/www/php_anazhthshs/euresh_suggrammatwn_aplh.php on line 22
Warning:
mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given
in/var/www/php_anazhthshs/euresh_suggrammatwn_aplh.php on line 24
Can you help? Thanks in advance!
Arguments must be inverted.
mysqli_query($con, $sql);
mysqli_query()

Unexepected T_VARIABLE [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
Unexecpected T_VARIABLE in SQL Query on Line 5
How to fix this?
<?php
include "system.php";
$usersystem = $_SESSION['username'];
$passw = $_SESSION['password'];
$query= "SELECT * FROM users WHERE username = "$usersystem" AND password = "$passw";
$autoexec= $mysqli->query($query);
$earnings = $autoexec['earnings'];
$completed = $autoexec['completed'];
if ($_SESSION['loggedin'] !=1){
header ('Location: index.php);
}
?>
The syntax highlighter makes your issue obvious: quotes. You need to use single quotes for your strings in your query:
$query= "SELECT * FROM users WHERE username = '$usersystem' AND password = '$passw'";
Basic PHP syntax:
$query= "SELECT * FROM users WHERE username = "$usersystem" AND pas
^-- ^---
You cannot use the same type of quotes that you've used to delimit the string. Try
$query= "SELECT * FROM users WHERE username = \"$usersystem\" AND pas
^--- ^--- note the escapes
And since this is a simple typo-type problem, voting to close the question...

Categories