This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP Error: mysql_fetch_array() expects parameter 1 to be resource, boolean given
I am trying to build an array from a mysql object after querying.
The number or rows returned in the query is 68, this is why I thought a forloop would be good. However php does not like my code inside the forloop.
Here is the code:
$result = $db->query("SELECT * FROM `networktariff`");
$rows = mysql_num_rows($result);
for ($i= 0; $i<$rows; $i++)
{
$tariffs[$i] = mysql_fetch_assoc($result[$i]);
}
I am getting an error message saying :
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in /usr/local/www/apache22/data/quote/index.php on line 58
There is no such thing like "mysql object".
Your $db object belongs to some user-defined class and there is no point in asking anyone about this class' behavior.
Assuming that query() method just utilizing ordinary mysql_query() function call, it does return a resource, not object.
If you have a strange idea of using some class to run the query yet bare API functions for the rest,
$tariffs = array();
$result = $db->query("SELECT * FROM `networktariff`");
while ($row = mysql_fetch_assoc($result) {
$tariffs[] = $row;
}
I dunno though why your class doesn't have some helper function to get the whole data at once, something like
$tariffs = $db->query2arr("SELECT * FROM `networktariff`");
Yup, read the docs on mysql_fetch_assoc().
A better construction might be:
while($row = mysql_fetch_assoc($result)) {
// Do stuff with $row
}
To elaborate: $result does not contain any data. It's a "resource" handle that points to a resultset that MySQL has created. To access the data of the resultset, you use mysql_fetch_* and pass it the resource handle. Each time you call a fetch function, the internal pointer is incremented one row. So you'll get fresh data each time until MySQL reaches the end of the results, at which point you'll get FALSE. This is why the while() loop works.
print $result as $db->query is not standard php function, it might return you result set array.
try same code with replacing $db->query to mysql_query
Related
So I have this code:
foreach ($_SESSION['basket'] as $item) {
$ref = $_SESSION['basket'][$counter];
$result = pg_query ($conn, 'SELECT * from music WHERE ref='.$ref.' ORDER BY artist');
}
This will output the first row fine, however it outputs this Warning: pg_fetch_row() expects parameter 1 to be resource, boolean given if I try to retrieve more than one row. I don't understand how I'm giving a boolean to parameter 1, this is the code on line 46 where it is getting the error:
($row = pg_fetch_row($result))
Thanks in advance
You can use $row =pg_fetch_array($result) and then $row['field_name'] to take values out in the foreach loop.
The error may be because your connection variable $conn does not connect to your database.
Try all possibilities. Thank you.
If your query fails, pg_query returns FALSE. Also, you should use pg_query_params instead to prevent SQL injection.
Have a look at the examples in the PHP doc: http://php.net/manual/en/function.pg-query.php.
REMEMBER TO:
A: Escape input, or even better use prepared statements
B: Check the return value before iterating over the results. If an error occured it doesn't make sense to try and iterate.
In general the PHP docs are a great place, when strugling with the details of the PHP-api's, which are sometimes... Less intuitive than they could be. :-)
Is this a bad mysql query i used in php?
$tablenamep = $_POST["tablenamep"];
$res = mysqli_query($con, "SELECT * FROM `$tablenamep` WHERE number=9");
So when i try to fetch the result using:
while ($row = mysqli_fetch_assoc($res))
There is an sql injection error :
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given
I have read several questions and answers regarding this error, but my question is why is the query returning a boolean, when i have even added a value to $tablenamep variable. I added the value to the variable from my android app using this code :
nameValuePairs.add(new BasicNameValuePair("tablenamep", msg));
The codes are working and there aren't any errors, but my android app is crashing when i try to get the result of the php. How can i solve this! (NOTE : there is nothing wrong in my android app, i've thoroughly checked it)
Why is this a bad query? What can i do for the Query to not return a boolean, and return the actual value?
I guess your $res = mysqli_query($con, "SELECT * FROM $tablenamep WHERE number=9");
returns fails.
As what stated here in TECHNICAL DETAILS TABLE
For successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries it will
return a mysqli_result object. For other successful queries it will
return TRUE. FALSE on failure
mysqli_fetch_assoc() function needs mysqli_result but the query fails that why it returns boolean instead of object.
You will find that your query has 'failed'
Insert the bit of code below
if($result === FALSE) {
die(mysql_error());
}
just above this line
while ($row = mysqli_fetch_assoc($res))
and you will find it dies at the script there.
Odds are that your posted value has a problem with it - echo out your posted value and see if it contains any ' or " etc. etc.
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
I'm working on an assignment for school which involves using PHP to generate a table of SQL results. I'm having a problem converting the results of my query. This is my error message:
Catchable fatal error: Object of class mysqli_result could not be converted to string
Here is the relevant part of my code:
$q = "SELECT pages.pageid,pages.pagetitle,pagetype.pagetypename,stylesheets.stylename
FROM pages
INNER JOIN stylesheets
ON pages.styleid=stylesheets.styleid
INNER JOIN pagetype
ON pages.pagetypeid=pagetype.pagetypeid
ORDER by pages.".$sortBy." ".$sortDirection;
print("<p>Query: ".$q."</p>\n");
$r = mysqli_query($db_link,$q);
$n = mysqli_num_rows($r);
print("<p>$r</p>");
My only theory is that maybe since I'm using an inner join with multiple tables there's something additional I need to do? I ran the query the code displayed in the MySQL server and it worked fine and returned the right results.
The problem is that $r is an object, not a string. You want to print the value of $n, not $r. Change your print statement to the following to fix the error:
print("<p>$n</p>");
It's unclear from the question details whether you're asking how to print number of rows, or how to print the actual results of the query.
If you're trying to do the latter: mysqli_query() returns a mysqli_result object. The returned result resource should be passed to mysqli_fetch_array(), mysqli_fetch_assoc() or other functions for dealing with result tables, to access the returned data.
The basic structure would look like:
$r = mysqli_query($db_link,$q) or die(mysqli_error($db_link);
$n = mysqli_num_rows($r);
while ($row = mysqli_fetch_assoc($r)) {
// use $row['key']
}
Also, I suggest you use more descriptive variable names in your code -- it makes your code maintainable and easy to read.
You should also be more careful about constructing your query strings. If $sortBy is coming from user input, it is a vector for an SQL injection attack.
mysqli_query() returns a result set resource, which is not the same as a set of results. You need to retrieve the data with mysql_fetch_array() or similar.
$r = mysqli_query($db_link,$q) or die(mysqli_error($db_link);
$n = mysqli_num_rows($r);
while ($row = mysqli_fetch_array($r)) {
var_dump($row); // replace this with your own code to format the output
}
Part of your confusion is that you are using lots of 1 character strings that—in my honest opinion—simply cause confusion & don’t really benefit coding nowadays. Use real words for strings & make your life—and debugging process—easier. Also your query & results seem to have not been parsed. Here is my take on your code using an object oriented style of handling:
$query = "SELECT pages.pageid,pages.pagetitle,pagetype.pagetypename,stylesheets.stylename
FROM pages
INNER JOIN stylesheets
ON pages.styleid=stylesheets.styleid
INNER JOIN pagetype
ON pages.pagetypeid=pagetype.pagetypeid
ORDER by pages.".$sortBy." ".$sortDirection;
print("<p>Query: ".$query."</p>\n");
$result = $db_link->query($query);
$number_of_rows = $result->num_rows($result);
while($row = $result->fetch_assoc()){
print("<p>$row</p>");
}
I've searched the site for an answer but couldn't find any answer for my specific problem that worked.
I have two files, the first one is my index.php of-course, the second is my functions.php file, which obviously contains the functions, I made this function:
function sql_get($lang, $tabler, $rower){
if ($lang == "heb") {
$result = mysql_query("SELECT * FROM %s WHERE id = 0",
mysql_real_escape_string($tabler));
$row = mysql_fetch_array($result);
return $row['$rower'];
}
else if ($lang == "rus") {
$result = mysql_query("SELECT * FROM %s WHERE id = 0",
mysql_real_escape_string($tabler));
$row = mysql_fetch_array($result);
return $row['$rower'];
}
this code supposed to get an information about the language (from a get, it gets it, it's all fine with that), the sql table and the specific row from this table where the id is 0.
and return the information from the row inserted.
My warnings and errors when the language is "heb":
Warning: mysql_query() expects parameter 2 to be resource, string given in /home/elenbyin/public_html/elenby.co.il/vadim/functions.php on line 16
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/elenbyin/public_html/elenby.co.il/vadim/functions.php on line 17
and when the language is "rus":
Warning: mysql_query() expects parameter 2 to be resource, string given in /home/elenbyin/public_html/elenby.co.il/vadim/functions.php on line 23
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/elenbyin/public_html/elenby.co.il/vadim/functions.php on line 24
the function call in the index.php file looks like that:
I will be thankful for the people who will try to help and especially for those who will help me.
Thanks A lot!
Firstly, you haven't connected to the database inside your function. If you've opened your database somewhere then you can get away with this with mysql, but you shouldn't.
Secondly, you're passing your variable as a second parameter to mysql_query(), but mysql_query() expects the query to be complete as the first parameter, and the second parameter should be the connection resource tht connects to the database, which you haven't got.
From the structure of the query it looks like you intend to use sprintf() to create it.
This should create the query for you, assuming you have opend a databse connection:
$query = sprintf("SELECT * FROM %s WHERE id = 0",
mysql_real_escape_string($tabler));
$result = mysql_query($query) or die(mysql_error());
Note: mysql is deprecated. You shouldn't use it. Use mysqli or PDO instead. You'll need to be more rigorous about passing in your database connecions if you use mysqli.
As Amal suggested - better not use mysql_* functions cause not only they're deprecated but also vulnerable to sql-injection.
That said, in order to fix your code you should do:
$table = mysql_real_escape_string($tabler);
$sql = "SELECT * FROM $table WHERE id = 0";
$link=mysql_connect('host','user','pass');
$result = mysql_query($sql, $link);
...
as the second (and optional) parameter of mysql_query should be a resource - not a string!
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
I am connected with my database and there seems to be an error appearing on this line of code:
while($br = mysql_fetch_assoc($brand))
and on my query I put this:
$brand = mysql_query("Select * from genratb");
The error says
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\videoshop\index.php on line 166
The first command is actually my line 166.
It looks like your query failed. The mysql_query call probably returned false, instead of the result resource.
$brand = mysql_query("Select * from genratb");
if (!$brand)
{
//error, query failed
}
else
{
while($br = mysql_fetch_assoc($brand))
{
//use row
}
}
If they query fails, then mysql_query() will return false. In that case, you need to look at mysql_error() to find out why the query failed.
The PHP documentation states:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
Most likely your query is failing for some reason, and setting $brand to FALSE. Are you sure your database connection is working, and your table exists?
You can add after your query line:
if (FALSE===$brand) { die(mysql_error()); }
This should tell you what is going wrong.