Problems with mysqli [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 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()

Related

I keep getting these PHP error messages when trying to use 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 4 years ago.
Improve this question
I've provided the code, so people can better help me. Okay, I'm doing a PHP tutorial based on YouTuber mmtut's tutorials. Using local PHPMyAdmin, double checked that my naming conventions are all right, and am getting these two errors:
Warning: mysqli_query() expects parameter 1 to be mysqli, string given
in C:\xampp\htdocs\PHPMySQL\index.php on line 11
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result,
null given in C:\xampp\htdocs\PHPMySQL\index.php on line 12
My PHP code:
Within the includes folder:
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "phplessons";
$conn = "mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName)";
?>
Within the index.php file:
<?php
include_once 'includes/dbh.inc.php';
?>
<!DOCTYPE html>
<html>
<body>
<?php
$sql = "SELECT * FROM posts;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row['subject'];
}
}
?>
</body>
</html>
Your connection property is a literal. You want something like this:
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

warning of mysqli_real_escape_string() for this code [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 7 years ago.
Improve this question
Query:
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$username = mysqli_real_escape_string(trim($_POST["username"]), $db);
$password = mysqli_real_escape_string(trim($_POST["password"]), $db);
$password = md5($password);
$sql = "Insert into login(username,password) values('$username','$password');";
$result = mysqli_query($db,$sql);
echo"Successful Registration";
if($result) {
echo("Successfully updated");
}else{
die ("no database");
}
}
Error:
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\test.php on line 14
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\test.php on line 15
Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in C:\xampp\htdocs\test.php on line 19
Successful Registrationno database
Your parameters are in the wrong order, read the documentation again. For example:
$username = mysqli_real_escape_string(trim($_POST["username"]), $db);
Should be:
$username = mysqli_real_escape_string($db, trim($_POST["username"]));
See http://php.net/mysqli_real_escape_string (the procedural style) for the right parameter order.

What is the issue with this mysqli statement [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 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'")

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)

mysql_query() expects parameter 2 to be resource, string given in /home/u921305435/public_html/phpinfo.php on line 9 [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
I know this has been asked many times already, but me being thick, havent been able to solve it
Ive got this code and its giving me an error
<?
$username="username";
$password="password";
$database="databse";
mysql_connect("host",$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query($con, "SELECT ProductName FROM ProductTrans");
echo "<table border='1'>
<tr>
<th>ProductName</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
What is the problem with it?
I think it should be like :
$con = mysqli_connect("localhost", "my_user", "my_password", "world");
mysqli_query($con, "SELECT ProductName FROM ProductTrans");
For more info on mysqli_query: http://in2.php.net/manual/en/mysqli.query.php
OR if you want to use mysql_query() function which is deprecated you should do like this:
$result = mysql_query("SELECT ProductName FROM ProductTrans");
for mysql_query() :http://in2.php.net/mysql_query
From the documentation on php.net:
resource mysql_query ( string $query [, resource $link_identifier = NULL ] )
You have the parameters swapped in your code in the call to mysql_query
It should be query followed by the connection. Interchange the parameters !
$result = mysql_query("SELECT ProductName FROM ProductTrans",$con);
Sidenote : Also, stop using mysql_* functions as it deprecated. Switch to PreparedStatements !
you're using the syntax of mysqli_query with mysql_query. My advice is replace all your mysql with mysqli, as mysql_* is deprecated
It should be like this.
$con = mysql_connect("host",$username,$password); // SEE HERE
#mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query($con, "SELECT ProductName FROM ProductTrans");

Categories