Warning: mysql_result() expects parameter 1 to be resource, string given in - php

Here is a code which give the error.
The code is below.
$search = ("SELECT `patData` FROM `reportData` WHERE id = 2")
or die (mysql_error());
echo mysql_result($search,1);
In this code $search Query is work well.

$search = mysql_query("SELECT `patData` FROM `reportData` WHERE id = 2");
if (!$search) {
die('Could not query:' . mysql_error());
}
echo mysql_result($search, 0);
The problem is that mysql_query() is may be returning a Boolean instead of a result resource. There are two reasons this can happen:
You performed query that returns success/fail instead of a result set.
Your query failed.
Notes :
Don't write code that uses the mysql_* functions. They are deprecated and will eventually be removed from PHP.
Use MySQLi or PDO instead.

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.

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;
?>

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

Php error help : mysql_fetch_assoc()

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given
my code php is :
function getResourceLevel($vid) {
$q = "SELECT * from " . TB_PREFIX . "fdata where vref = $vid";
$result = mysql_query($q, $this->connection);
return mysql_fetch_assoc($result);
}
Please Help me
It looks like mysql_query() has returned false.
This could by due to an error in your mysql syntax or due to a lack of permissions for accessing the database table you have requested.
In either case, you can try calling mysql_error() which will return a string with a best guess of what went wrong with your last mysql function.
Edit: As mentioned in some of the comments, the use of mysql_* functions is discouraged, so if you have opportunity you should update your code to use the mysqli or PDO MySQL extensions. Better yet, use something like Zend DB to move you one layer further away from the database API.
Try to echo your query and run in some mysql query browser and check if its giving required results.
function getResourceLevel($vid) {
$q = "SELECT * from " . TB_PREFIX . "fdata where vref = $vid";
echo $q;
$result = mysql_query($q, $this->connection);
return ($result) ? mysql_fetch_assoc($result) : false;
}
Try this
function getResourceLevel($vid) {
$q = "SELECT * from " . TB_PREFIX . "fdata where vref = $vid";
$result = mysql_query($q, $this->connection) || die(mysql_error());
return mysql_fetch_assoc($result);
}
Notice the || die(mysql_error()) statement.
This will stop the code and show the sql error that is being returned as false.
Hope this helps.

Basic PHP/MySQL Error with mysql_fetch_array()

The error I get:
...mysql_fetch_array() expects parameter 1 to be resource, boolean given...
awayid is in the address bar properly. I can print it out just fine, but for some reason the following code gives me the above error.
$result = mysql_query("select * from team where id=" . $_GET['awayid']);
$row = mysql_fetch_array($result);
EDIT Tried the mysql_error(). It seems I forgot to select a database... however, even why I use mysql_select_db('gamelydb'); I still get the mysql error No database selected
Your query is failing... Therefore $result is set to false.
$result = mysql_query("select * from team where id=" . $_GET['awayid']);
var_dump($result); // bool(false)
Call mysql_error() to get the error message for your query:
echo mysql_error();
Your query is failing and returning a boolean FALSE. Try this:
$result = mysql_query("select ...") or die(mysql_error());
^^^^^^^^^^^^^^^^^^^^^^---- add this
This will kill the script and show you the exact reason the query is failing.
mysql_query() returns false if the query is unsuccessful, i.e. an error occured. That is why you need to check $result for being false first.
Use mysql_error() to output the error.
You need to be sure there is results from your query :
while ($row = mysql_fetch_array($result)) {
// echo $row[] ... ;
}
First of all, your query is very open to SQL injection attacks. Do not directly insert anything from $_GET or $_POST (or really anywhere) into your query. At the minimum, use mysql_real_escape_string on the variable.
mysql_query is returning false becuase there is something wrong with the query. You can use mysql_error to see what the last reported error is.
if ($result = mysql_query("select * from team where id='" . $_GET['awayid']) . "'") {
$row = mysql_fetch_array($result);
}
else {
echo mysql_error();
}
Anyway...you know that writing a $_GET parameter right into the SQL query is very very bad? Try it with PHP Data Objects.
Did you try and search around first Tory, we answer these questions over and over again, next time please search around.
The reason why this error occurs is because your running a query with mysql_query that fails, because it fails it returns false, you then pass the value of false to mysql_fetch_array, it's like doing mysql_fetch_array(false)
You need to make sure that mysql_query is successful:
try something like this:
if(false !== ($result = mysql_query("select * from team where id=" . $_GET['awayid'])))
{
$row = mysql_fetch_array($result);
}else
{
die("Query has failed: " . mysql_error())
}

Categories