Why is my PHP Mysqli code expecting a mysqli_result parameter [duplicate] - 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 6 years ago.
I have a line of code in my php that reads:
$sel_venue = "SELECT 'char_type' FROM 'character_type_allowed' WHERE status='Open'";
$run_venue = mysqli_query($con,$sel_venue);
while ($row = mysqli_fetch_array($run_venue))
if ($row['char_type'] == 'Mortal')
{ print ("<li><a href='http://houston-by-night.com/sheets/create/mortal.php'>Create Mortal</a></li>"); }
The link associated with this does nothing. Zero interaction beyond acting likeit wants to expand. My error log produces this: Why is it asking for this?
[08-Aug-2016 23:28:41 America/New_York] PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/houchat/public_html/incl/creation.php on line 8

You can't use ' as ticks for field/tablenames.
Your query is producing an error. You can see the error with mysqli_error($con).
Please see the corrected code below
$sel_venue = "SELECT `char_type` FROM `character_type_allowed` WHERE status='Open'";
$run_venue = mysqli_query($con,$sel_venue) or die(mysqli_error($con));
while ($row = mysqli_fetch_array($run_venue)) {
if ($row['char_type'] === 'Mortal') {
print ("<li><a href='http://houston-by-night.com/sheets/create/mortal.php'>Create Mortal</a></li>");
}
}

Your query failed, so $run_venue is the boolean false instead of what you expect. You should check for errors before you use any query result. Do this:
$run_venue = mysqli_query(...);
if(!$run_venue) die(mysqli_error($con));
... //<- we get here if the query succeeded
You will see the error. The problem is that your SQL statement wraps the table name between single quotes 'character_type_allowed', instead of backticks (backtick is above the tab key on my keyboard)

Related

Trying to output data as an array [duplicate]

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 3 years ago.
I made a table that holds post and likes and I'm trying to output the of everything in the table but I get the
error: mysqli_num_rows() expects parameter 1 to be mysqli_result,
boolean given on line 18.
$sql3 = "SELECT * FROM post;";
$result = mysqli_query($conn,$sql3);
$datas = array();
if (mysqli_num_rows($result)> 0){
while($row = mysqli_fetch_assoc($result)){
$datas[] = $row;
}
}
foreach ($datas as $data){
echo $data;
}
I expect the all the information to be outputted, but the actual output is
mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean
given on line 18.
This means that your query is returning false instead of a mysqli_result object. The problem appears to be from the semicolon at the end of your statement. I believe that mysqli will see that as a multistatement. From https://www.php.net/manual/en/mysqli.quickstart.multiple-statement.php:
Multiple statements or multi queries must be executed with mysqli_multi_query(). The individual statements of the statement string are separated by semicolon. Then, all result sets returned by the executed statements must be fetched.
mysqli has a method to get the last error it encountered:
https://www.php.net/manual/en/mysqli.error.php
Example:
if(!$result){
printf("Error message: %s\n", mysqli_error($conn));
}

PHP warning expects parameter 1 to be resource, object given [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 1 year ago.
I want to retrieve all data from a table, so I use this code
<?php
include("config.php");
$sql = "SELECT * FROM ".$USERS;
$sql_result = mysqli_query($connection, $sql);
if ($sql_result) {
while ($result = mysql_fetch_assoc($sql_result)) {
echo $result;
}
}
else {
die ('Could not execute SQL query '.$sql);
}
?>
but got this warning:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource,
object given in C:\xampp\htdocs\newSDP\phpscript\users.php on line 6
How can I fix it?
Change:
mysql_fetch_assoc to mysqli_fetch_assoc

mysqli_query() returning a boolean value [duplicate]

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 8 years ago.
I have a piece of PHP code I am using to grab the 20 next messages in a database.
<?php
$user_email_msg = "bob#gmail.com";
$msg_email = "sam#gmail.com";
$start_query_msg = 1;
$end_query_msg = $start_query_msg-20;
$user_link_msg = mysqli_connect("localhost", "root", "admin", $user_email_msg);
$query_msg = "SELECT * FROM " . $msg_email . " WHERE id BETWEEN " . (string)$end_query_msg . " AND " . (string)$start_query_msg;
$result_msg = mysqli_query($user_link_msg, $query_msg);
$row_msg = mysqli_fetch_all($result_msg, MYSQLI_NUM);
$next_20 = $row_msg[0];
print_r($next_20);
?>
When I run this code I get this error message:
"Warning: mysqli_fetch_all() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\jayden\messages.php on line 10"
I am so lost here, because I can't see any way that $result_msg can be returning a boolean value.
How can I fix the issue?
I may be wrong, but most likely your fourth parameter to mysqli_connect is not an email address but should rather be the database name
echo the variable $query_msg and run the query in phpmyadmin or any favorite application. The error is about mistake in sql query check that now.
print the query and check if the query is ok.
print_r($query_msg);
this problem occurs when your query is not right.

Invalid parameter in select from database? [duplicate]

This question already has an answer here:
MysQl error : Invalid parameter number
(1 answer)
Closed 8 years ago.
Its probley something small but i been looking at this for ages and still cant get it to work at all im getting two errors
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number in
the code i have is this:
public function getdata ($tran_id)
{
$sql = "SELECT tran_id, seller_user_name, user_name_buyer
FROM trade_transaction, feedback Where feedback.feedback_username = trade.user_name_of_buyer
AND user_name_of_buyer = :user_name_buyer ";
$sth = $this->db->prepare($sql);
$sth->execute(array(':tran_id' => $tran_id, ':user_name_buyer ' => $_SESSION['user_name']));
$user = $sth->fetch();
You're binding a :tran_id parameter during your call to execute, but you're not using that parameter in your query.
Change your execute line to this
$sth->execute(array(':user_name_buyer ' => $_SESSION['user_name']));
Remove :tran_id from your parameter list or add a condition for that parameter. I hope this help.
Your select statement does not have a where clause for tran_id, either remove the tran_id from your execute call
$sth->execute(array(':user_name_buyer ' => $_SESSION['user_name']));
or add a extra where clause to your sql statement
$sql = "SELECT tran_id, seller_user_name, user_name_buyer
FROM trade_transaction, feedback
WHERE feedback.feedback_username = trade.user_name_of_buyer
AND tran_id = :tran_id
AND user_name_of_buyer = :user_name_buyer ";

PHP $_GET['id'] and security [duplicate]

This question already has answers here:
How can I prevent SQL injection in PHP?
(27 answers)
Closed 9 years ago.
I have links on my webpage like this: http://test.com/index.php?function=news&id=88
So whenever I put a ' after 88, I get the following error: Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in ... line 588
So I read about mysql_real_escape_string(), but I'm getting the ID not posting and I have no clue how should I prevent getting this error.
function news()
{
$query = mysql_query("SELECT * FROM news WHERE id=".$_GET['id']."");
while($news = mysql_fetch_row($query))
{
...
}
}
The easy way is to cast the id to integer, if the id is an integer that is:
$id = (int)$_GET['id'];
But it's strongly recomended to use pdo or mysqli with prepared statements:
http://php.net/manual/en/book.pdo.php
http://php.net/manual/en/book.mysqli.php
You can do a redirect whenever mysql_fetch_row() don't return anything (i.e. because there is no id 89)
Something like:
if (!$row = mysql_fetch_row($result)) {
header(Your error page);
}
Warning: mysql_fetch_row() expects parameter 1 to be resource
This means the the $result = mysql_query(....); call you made before the mysql_fetch_row() failed and resulted FALSE instead of a Resource ( i.e. a handle to the query result );
Look at the query, post it if possible, that is where your problem is.
Your code assumes that the query was successful without checking. For debugging purposes, add an 'or die(mysql_error())' line to the end of the mysql_query() statement.
$query = mysql_query("SELECT * FROM news WHERE id=".$_GET['id']."") or die( mysql_query() );
For more robust error handling in production applications, check the value of $query and log an error if it is false.
if (false === $query ) {
// Log error and/or notify an administrator
}
else {
while($news = mysql_fetch_row($query)) ...
As pointed out in other answers, you should ensure that the value of the id parameter is an integer since your query assumes that it will be. You can do this by casting:
(int)$_GET['id']
or via more robust type checking
if ( !is_numeric( $_GET['id'] ) ) {
// Take appropriate action
}
else {
// Create and execute the query

Categories