expects parameter warning 1 to be resource - php

This below code is giving me mysql_fetch_array() expects parameter
<?php
$query = $forumdb->prepare("SELECT COUNT(*) as cnt FROM smf_personal_messages");
$query->execute();
$num_rows = mysql_fetch_array($query);
if ($query->rowCount() == 0) {
echo "<tr><td colspan='6'><small>No rows found</small></td></tr>";
}
echo ($num_rows['cnt']);
?>
Warning: mysql_fetch_array() expects parameter 1 to be resource,
object given in /home/gamin1/public_html/ucp/forumstats.php on line
127

Seems to be a reasonable assumption that you want to use PDO. As has already been mentioned, you can't mix pdo(_msyql) with the other mysql apis.
But there are some other issues with your script:
<?php
// not an error per se, but:
// no placeholders in the query + sent only once -> prepared statement superfluous
$result = $forumdb->query("SELECT COUNT(*) as cnt FROM smf_personal_messages");
// assuming PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION, so no error handling here....
// Count(*) with no explicit grouping always returns exactly one record, so
// ->rowCount() will do you no good
// instead fetch that one record and read the field holding the value of Count(*) as cnt
$row = $result->fetch();
if ( 0==$row['cnt'] ) {
echo "<tr><td colspan='6'><small>table cotains no records</small></td></tr>";
}
else {
echo '# of rows: ', row['cnt'];
}

Use $query->fetch() instead of mysql_fetch_array($query).
You are using PDO here: http://php.net/manual/fr/pdostatement.fetch.php

Related

Error Query PHP after migrate to mysql database

I have execute query using PHP which previously executed on mssql server database . Now with the same table and data. I using mysql database to execute my query. But error happen. Any suggestion for my query below in order to can execute using mysql database :
$year = mysql_query("SELECT * FROM education_year ORDER BY id DESC");
if (isset($_GET['year'])){
$educationyear= mysql_fetch_array(mysql_query("SELECT * FROM educationyear WHERE year='{$_GET['year']}'"));
}else {$educationyear = mysql_fetch_array($year);}
$kode['KODE'] = mysql_fetch_array(mysql_query("SELECT KODE FROM educationyear WHERE year='$educationyear'"));
$result = mysql_query("SELECT * FROM Province");
while($row = mysql_fetch_array($result))
{
$xd = mysql_fetch_array(mysql_query("SELECT COUNT (*) AS total FROM child WHERE id_province='{$row['province_code']}' AND education='A'
AND educationyear='{$educationyear['KODE']}'"));
}
Error message like below :
Notice: Array to string conversion in C:\xampp\htdocs\xy\demo.php on line 19
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\xy\demo.php on line 20 .
Its line when execute $xd query.
There are a few problems with your code
1st: When you use an array within double-quoted string, do not quote the array key. Change
"...WHERE year='{$_GET['year']}..."
"...WHERE id_province='{$row['province_code']}'..."
To:
"...WHERE year='{$_GET[year]}..."
"...WHERE id_province='{$row[province_code]}'..."
2nd: The design pattern below is not good:
mysql_fetch_array(mysql_query("SELECT...")
You're taking the result of mysql_query and feeding it directly to mysql_fetch_array. This works as long as the query succeeds and returns a resource. If the query fails, it will return FALSE and mysql_fetch_array will trigger the error you see:
mysql_fetch_array() expects parameter 1 to be resource, boolean given
Instead, make sure there is no error before proceeding
$result = mysql_query("SELECT...")
if($result===false){
//Query failed get error from mysql_error($link).
//$link is the result of mysql_connect
}
else{
//now it's safe to fetch results
$record = mysql_fetch_array($result);
}
3rd: do not use mysql_ functions. They have been abandoned for years and have been removed from the most recent version of PHP. Switch to MySQLi or PDO
4th: learn about prepared statements. You're using user supplied input directly in your query ($_GET['year']) and this makes you vulnerable to SQL injection.

PHP - basic select query returning only the first row of mysql database

I've been running the query oh phpMyAdmin and it shows all the rows, but my query in php only returns the first row.
$result = $mydb -> query("SELECT * FROM music_sheet WHERE category='".$_REQUEST["submitter"]."'");
print(count($result)); //always returns 1
for ($x = 0; $x < count($result); $x++) {
$row = mysqli_fetch_row($result);
}
For reference, here's why count() is returning 1. From the manual:
If the parameter is not an array or not an object with implemented Countable interface, 1 will be returned. There is one exception, if array_or_countable [the parameter] is NULL, 0 will be returned.
Since $result is a resource (object) that is returned from your query, not an array which you would get within your loop getting the actual data out of the resource, count($resource) will return 1.
Your solution is of course to use mysqli_num_rows(). To retrieve data from this resource, you need to loop over it as you are doing, but either use mysqli_num_rows() in place of count(), or other (more common) ways of looping through a result set, like so:
while($row = mysqli_fetch_row($result)) {
// do stuff
}
You have to use mysqli_num_rows($result) function to count rows which are returned by MySQLi query.
Try this
echo mysqli_num_rows($result);
while($row = mysqli_fetch_row($result)) {
// write your code...
}
Use this instead of the for loop
the first thing that the query method returns to you is a resource/object. This query method always return a mysqli_result object for successful queries using SELECT, SHOW, DESCRIBE or EXPLAIN queries . For other successful queries mysqli_query() will return TRUE. For that reason it always count it as "1", what you should try is:
$result = $mydb -> query("SELECT * FROM music_sheet WHERE category='".$_REQUEST["submitter"]."'");
$numRows = $result->num_rows;
print(count($numRows)); //it should show you the total amount of rows
//verify the amount of rows before of tryng to loop over it
if ($numRows) {
while($object = mysqli_fetch_object($result)){
//for each loop you will have an object with the attributes of the row
echo $object->song_name;
}
}
This should work,
Regards.

How can I get the count of the amount of rows for this result set in php?

I looked up how to do it and this is what I thought it said to do, however, I keep getting this error
Warning: mysql_num_rows() expects parameter 1 to be resource, object given
<?php
$txn_sql = " SELECT * FROM test WHERE item2 = 835587755 ";
$order_num = mysqli_query($Connection, $txn_sql);
$num_rows = mysql_num_rows($order_num);
echo $num_rows;
?>
The mysql_* and mysqli_* functions are from separate libraries, and the former is deprecated. You can't use mysql_num_rows on a mysqli_query result.
If you're using mysqli, you get an object back. Call the num_rows() method on that to get the result.
$num_rows = $order_num->num_rows();
Alternatively, if you (for some reason) want to use the procedural style, you can use mysqli_num_rows instead.
$num_rows = mysqli_num_rows($order_num);
It's very easy, you just have to replace mysql_num_rows with mysqli_num_rows. mysql_num_rows can't handle a mysqli result!
So try this:
<?php
$txn_sql = " SELECT * FROM test WHERE item2 = 835587755 ";
$order_num = mysqli_query($Connection, $txn_sql);
$num_rows = mysqli_num_rows($order_num);
echo $num_rows;
?>

Issue receiving a mysql_query

I'm trying to check if a specific link is already contained in a database,
but I keep getting an error stating "mysql_num_rows() expects parameter 1 to be resource"
I've tried changing a lot of things, but nothing seems to work. Can anybody help?
$result = mysqli_query($con, "SELECT * FROM `songs` WHERE `link` = '$link'");
if($result == False){
"echo f3";
return False;
}
$count =mysql_num_rows($result);
if($count > 0){
echo "f4", $count;
return False;
}
mysqli_* is not the same as mysql_*. You can't use resource from one in another.
Use mysqli_num_rows() to get number of rows from mysqli resource.

why does mysql_fetch_array() expect parameter 1 to be resource? why isn't parameter 1 resource?

Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\xampp\htdocs\test\index.php on line 19
<?php
$con = mysql_connect('localhost');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("schedule", $con);
$sql = "SELECT * FROM classes LIMIT 0,50\n";
mysql_query($sql);
IF (!$sql) {
ECHO 'broken';
};
while($row = mysql_fetch_array($sql, MYSQL_BOTH))
{
echo $row['language'] . " " . $row['level'];
echo "<br />";
}
mysql_close($con);
?>
why? the query works in phpmyadmin
Your parameter to mysql_fetch_array() function is your SQL statement string. This is what your warning say. You should first use
$res = mysql_query($sql);
and pass $res as parameter to mysql_fetch_array()
The input to mysql_fetch_Array is a resource, which is also the returned value from mysql_query. If you pass the value $sql to mysql_query(), it will not modify the parameter since it is passed by value. You have to assign the return value to another variable, which will be the desired resource.
$result = mysql_query($sql);
And then, pass the result parameter to mysql_fetch_array :
$row = mysql_fetch_array($result, MYSQL_BOTH)
Another important note: As you might see in all the related threads, read the red box in the php.net for these functions.
Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL
extension should be used. See also MySQL: choosing an API guide and
related FAQ for more information. Alternatives to this function
include:
mysqli_query() PDO::query()
Change: mysql_query($sql);
To: $sql = mysql_query($sql);
Cited from PHP.net about mysql_query():
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning
resultset, mysql_query() returns a resource on success, or FALSE on
error.
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc,
mysql_query() returns TRUE on success or FALSE on error.
In your case the resource is returned, but you've forgotten to assign it to anything.
Change mysql_query($sql) to $resource = mysql_query($sql).
Full documentation right here: http://php.net/manual/en/function.mysql-query.php

Categories