MySQL & PHP Parameter 1 as Resource - php

Alright, PHP is throwing this error at me (in the log) when I run the code mentioned below:
Error
mysql_num_rows() expects parameter 1 to be resource, string given in (place) on line 10
Line 9-11
$queryFP = ("SELECT * FROM db");
$countFP = mysql_num_rows($queryFP);
$aID = rand(1, $countFP);
I think it has something to do with the $queryFP's syntax, but I'm not completely sure how to fix it since $queryFP's syntax is the simplest query I've ever seen.

You need to query the database first.
$queryFP = ("SELECT * FROM db");
Should be:
$queryFP = mysql_query("SELECT * FROM db");

You are missing the mysql_query function, it should be like this:
$queryFP = "SELECT * FROM table_name_here";
$queryFP = mysql_query($queryFP) or die(mysql_error());
$countFP = mysql_num_rows($queryFP);
$aID = rand(1, $countFP);

As it been said, you're missing mysql_query function.
Though whole approach is wrong. You shouldn't select whole load of ata if you need only number of rows.
So, it must be
$sql = "SELECT count(*) FROM db";
$res = mysql_query($sql) or trigger_error(mysql_error().$sql);
$row = mysql_fetch_row($res);
$countFP = $row[0];
$aID = rand(1, $countFP);
And I hope you won't use $aID for any database related action

Related

Problems Outputting PHP from MySQL

I am trying to import MySQL data into PHP through the Wordpress PHP Snippet plugin. For whatever reason I keep getting error 'mysql_fetch_array() expects parameter 1 to be resource, boolean'.
My code is as follows:
Connection
[insert_php]
$conn = mysql_connect("localhost", "albert", "notrealpassword") or die
(mysql_error());
PHP
mysql_select_db('mydatabase');
$query = "SELECT * FROM mytable";
$result = mysql_query($query);
while ($subjectone = mysql_fetch_array($result))
{echo $subjectone['dataintable'];}
[/insert_php]
It is because before the last result the answer of mysql_fetch_array is array but while waits for bolean.
Also probably you know it is better to use new MySQLi functions
mysql_select_db('mydatabase');
$query = "SELECT * FROM mytable";
$result = mysql_query($query);
while (is_array($subjectone = mysql_fetch_array($result)))
{echo $subjectone['dataintable'];}

How to set a variable in where clause of Select query in php

while executing the below query i'm not getting sucess value
$name=qwe;
$result = mysql_query("SELECT *FROM USER WHERE name = $name");
Instead of above query if i put the below, I can able to get the appropriate ans:
$result = mysql_query("SELECT *FROM USER WHERE name = 'qwe'");
Can anyone give a solution for my first query??
Close the variable in quote and concatenate the string into query
$name= "qwe";
$result = mysql_query("SELECT *FROM USER WHERE name = '".$name."'");
one more note
please use mysqli for security purposes. Thanks
Use the following:
$result = mysql_query("SELECT *FROM USER WHERE name = '".$name."'");
P.S. I have not tested the above code but it should work.
select * from table_name where col_name = '".$variable."'
Directly use :
$name = 'qwe';
$result = mysql_query("SELECT * FROM USER WHERE name = '$name'");
And on more advise, you should never use mysql_* functions now as they have been deprecated in newer versions of PHP and will produce the notices.

MYSQL num rows error

Hi I am trying to use a piece of code which I have used before to quickly test out an idea, however I Keep getting the following error.
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in
$result = mysql_query("SELECT * FROM masterip_details WHERE timedate(datetime) = CURDATE() and ip_address='$ip_address");
$num_rows = mysql_num_rows($result);
if( $num_rows > 0 ) {
That's probably because the SQL request failed. Try to append or die(mysql_error()) next to your mysql_query, like this :
$result = mysql_query("SELECT * FROM masterip_details WHERE timedate(datetime) = CURDATE() and ip_address='$ip_address") or die(mysql_error());
This should output the error so that you can fix it.
EDIT: And I can also give you a clue of what that error could be. At the end of your request, you're not closing the single quote after $ip_address
("SELECT * FROM masterip_details WHERE timedate(datetime) = CURDATE() AND ip_address='$ip_address'")
You missed an ' at the end of your SQL-Statement and you should write "AND" uppercased, since all other SQL-Keywords are written uppercased (not essential, but easier to read).
Maybe the missing ' will fix your Query.
There is a sintax error in your query, you are missing a quote right after $ip_address just change to
$result = mysql_query("SELECT * FROM masterip_details WHERE timedate(datetime) = CURDATE() and ip_address='$ip_address'");
You keep getting that error because your query is wrong:
mysql_query("SELECT * FROM masterip_details WHERE timedate(datetime) = CURDATE() and ip_address='$ip_address"
is missing a quote after $ip_address
you are missing a single quote here and ip_address='$ip_address'")
Take a look at this:
Why shouldn't I use mysql_* functions in PHP?
Try this. You are missing one single quote...
$result = mysql_query("SELECT * FROM masterip_details WHERE timedate(datetime) = CURDATE() and ip_address='$ip_address' ");

GET request - unable to do a SELECT statement from DB

I will be sending a request to the PHP code through a GET request. it will have several parameters passed. I will be using these parameters in the WHERE clause of my select statement.
GET REQUEST
http:// localhost/server/person.php?Id=1&age=12&hieght=100
PHP code
<?php
$connection = mysql_connect("localhost","user","pwd");
if (!$connection )
{
die('FAIL: ' . mysql_error());
}
mysql_select_db("db", $connection );
$result = mysql_query("SELECT * FROM Person where hieght='$_GET[hieght]");
$num_rows = mysql_num_rows($result);
?>
i think my PHP code is wrong. I am not sure if this is the way to grab variables from a GET request.
To get a variable from $_GET use, as an example to get height from $_GET array you can write
$height=$_GET['height'];
so you can write
$result = mysql_query("SELECT * FROM Person where hieght='".$hieght."'");
or you can write
$result = mysql_query("SELECT * FROM Person where hieght='".$_GET['height']."'");
Update:
Use mysql_real_escape_string to prevent sql injection, like
$height=$_GET['height'];
$result = mysql_query("SELECT * FROM Person where hieght='".mysql_real_escape_string($hieght)."'");
or
$result = mysql_query("SELECT * FROM Person where hieght='".mysql_real_escape_string($_GET['height'])."'");
$result = mysql_query("SELECT * FROM Person where hieght='".$_GET['hieght']."'");
A good debug technique when you are feeling your your is to put the SQL into a string
that you can log and see if it is what you expected.
Using mysql_error() after the query may have given you a hint too...
$sql = "SELECT * FROM Person where hieght='".$_GET['hieght']."'";
$result = mysql_query($sql);
#Not for production code, but handy while learning - error goes into web server log.
error_log("SQL=$sql, error=".mysql_error());
You should be parsing input like that for injections:
<?php
$myHeight = mysql_reaL_escape_string($_GET['height']);
$connection = mysql_connect("localhost","user","pwd");
if (!$connection )
{
die('FAIL: ' . mysql_error());
}
mysql_select_db("db", $connection );
$result = mysql_query("SELECT * FROM Person where height=$myHeight");
$num_rows = mysql_num_rows($result);
?>
Secondly, you should also be using the PHP PDO object rather than the old mysql_query etc.
Use this
$height = $_GET['hieght'];
$result = mysql_query("SELECT * FROM Person where hieght='$height'");
Instead of
$result = mysql_query("SELECT * FROM Person where hieght='$_GET[hieght]");
Note that with this code it is very likely to be victim of sql
injection
you may also wanna use this concat:
mysql_query("SELECT * FROM Person where hieght='".$_GET['hieght']."'");

problems with mysql_num_rows

for some reason i can not understand im having troubles with mysql_num_rows.
Heres the script:
$notquery = 'SELECT * FROM notification WHERE uid = 1 AND read = 0
AND tipo = post
OR tipo = subpost OR tipo = logros';
$notQuery = (mysql_query($notquery));
$num_rows = mysql_num_rows($notQuery);
error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
This is probably because you have an error in the query execution.
Try adding or die(mysql_error()) to debug what's going wrong...
update the code to :
$notQuery = mysql_query($notquery) or die(mysql_error());
The sql is full with syntax error, try this
SELECT * FROM notification
WHERE
uid = 1 AND
`read` = 0 AND
tipo in('post', 'subpost', 'logros');
You input a string into mysql_num_rows =)
You use $notquery and $notQuery mixed... That must be the silliest thing I've ever seen.
Try this:
$sql = 'SELECT .....';
$result = mysql_query($sql);
echo mysql_error()."\n";
$numRows = mysql_num_rows($result);
Obviously, this might produce the same error, since you don't check for errors :) $result might not be a MySQL resource (it might be FALSE).

Categories