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' ");
Related
I am trying to pass a variable to a very basic mysql query. but php doesnt return a true value. nothing.
i have checked everything
the problem is here.
the syntax of $a varible typing into mysql query
$result = mysql_query("SELECT id,floatingnumber FROM posts WHERE id='$a' LIMIT 1");
when i change $a to 22 it returns a value otherwise nothing.
exact query is here...
$a=$this->post_id;
$result = mysql_query('SELECT floatingnumber FROM posts WHERE id="'.$a.'" LIMIT 1')or die(mysql_error());
$row = mysql_fetch_row($result);
$sdfa=$a.'-'.$row[0];
$sdfa returns "86 - " without quotes 86 - space
so the problem is on the mysql fetch row please help
Have you tried echoing the query to see what the real value of $a is?
echo "SELECT id,floatingnumber FROM posts WHERE id='$a' LIMIT 1";
Have you tried checking for errors?
$result = mysql_query("SELECT id,floatingnumber FROM posts WHERE id='$a' LIMIT 1") or die(mysql_error());
Also, you shouldn't even be using mysql_* as it's deprecated.
This is how you'd do it in PDO:
$stmnt = $db->prepare("SELECT id,floatingnumber FROM posts WHERE id=:id LIMIT 1");
$stmnt->bindValue( ':id' , $a , PDO::PARAM_INT );
$stmnt->execute();
$result = $stmnt->fetchAll(PDO::FETCH_ASSOC);
typically when I'm writing in double quotes, simply putting in the variable works:
"... $1 ..."
but also, I originally learned it with brackets
"... {$1} ..."
you can try that. also, a handy way to write queries is store the query string in its own variable so you can easily print out the query and see what you wrote before submitting.
$query = "SELECT id,floatingnumber FROM posts WHERE id=$a LIMIT 1";
$result = mysql_query( $query );
This helps identify things like this.
try this
$result = mysql_query("SELECT id,floatingnumber FROM posts WHERE id='".$a."' LIMIT 1");
if your $a is a number then do like that
$result = mysql_query("SELECT id,floatingnumber FROM posts WHERE id= $a LIMIT 1");
EDIT :
your code is right
$row = mysql_fetch_row($result);
$sdfa=$a.'-'.$row[0];
the problem is in your sql or table because there is no floatingnumber where id is 86 .
Please help me regarding the specified problem:
The code section:
$result = mysql_query("SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate,
UNIX_TIMESTAMP(throughdate) AS throughdate FROM events ORDER BY eventdate where
id='$_GET[id];'");
// the above query is not working
if ( mysql_num_rows($result) == 0 ) {
print "<p>No events right now.</p>\n";
}
else {
$lasteventmonth = '';
while ($row = mysql_fetch_array($result)) {
$eventmonth="";
$eventmonth = date("F Y",$row['eventdate']);
if ($lasteventmonth != $eventmonth) {
print "<p style='font-size: 18px;'><b>$eventmonth</b></p>";
}
$lasteventmonth = $eventmonth;
showEvent($row);
}
}
?>
........................
........................//other codes
when the code evaluates as follows:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Users\Fagun\Desktop\UsbWebserver\Root\mapcal\mapcal.php on line 122
No events right now.++++++++
After your call to mysql_query, use this:
if (! $result) {
echo mysql_errno() . ": " . mysql_error(). "\n";
}
this will tell you exactly, why MySQL won't run your query.
is ID a String or int? Either way I guess you shouldn't include a trailing semicolon?
Try changing it as follows...
$result = mysql_query("SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate,
UNIX_TIMESTAMP(throughdate) AS throughdate FROM events ORDER BY eventdate where
id='$_GET[id]'");
I assume it's an issue with how you're using building the query and concatenating the id. Try this instead (notice how the ID is concatenated):
$query = "SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate,
UNIX_TIMESTAMP(throughdate) AS throughdate FROM events
ORDER BY eventdate where
id='".$_GET[id]."'";
$result = mysql_query($query) or die(mysql_error());
You don't have to break it into 2 pieces, but - this should be easier to read and understand. You can even echo out the query before running it to see what query is actually being created, and try it manually on your database.
The or die(mysql_error()) part will give you specifics on what the issue is (if it wasn't the ID issue).
Quote values properly :
$_GET[id] should be $_GET['id']
Try below:
$result = mysql_query("SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate,
UNIX_TIMESTAMP(throughdate) AS throughdate FROM events ORDER BY eventdate where
id='".$_GET['id']."');
Try this:
"SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate,
UNIX_TIMESTAMP(throughdate) AS throughdate FROM events ORDER BY eventdate where
id='".$_GET['id'].";'"
I'm assuming that id does not come from user input. If it does, this is vulnerable to a SQL injection attack.
try:
$result = mysql_query("SELECT *,
UNIX_TIMESTAMP(eventdate) AS eventdate,
UNIX_TIMESTAMP(throughdate) AS throughdate
FROM events
ORDER BY eventdate
where id= '" . intval($_GET['id']) . "'");
if($result)
{
//Do code
}
use intval() to make sure $_GET['id'] is an integer.
use the if statement to make sure the query has executed correctly.
Try this
$result = mysql_query("SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate,
UNIX_TIMESTAMP(throughdate) AS throughdate FROM events where
id='".$_GET['id']."' ORDER BY eventdate");
I'm having problems putting two where statements together
My current code:
include 'config.php';
$result22 = mysql_query("SELECT * FROM messages WHERE to_user='".$_SESSION['username']."' AND to_read_yet='yes' ");
$num_rows22 = mysql_num_rows($result22);
echo "$num_rows22 ";
For some reason this isn't working. I am not getting any results i have checked the db and there are results which should come out
You should read about sql syntax. After where you put any number conditions with bool operators. Using 2 times where is incorrect
$result = mysql_query("SELECT * FROM messages WHERE to_user='".$_SESSION['username']."' AND to_read_yet='"no"' ");
Leave the second WHERE expression out, it's just WHERE condition_1 AND condition_2 AND condition_3 AND ....
$result = mysql_query("SELECT * FROM messages WHERE to_user='".$_SESSION['username']."' AND to_read_yet='"no"' ");
SELECT *
FROM messages
WHERE to_user = '".$_SESSION['username']."'
AND to_read_yet= '"no"'
try with:
$result = mysql_query("SELECT * FROM messages WHERE to_user='".$_SESSION['username']."' AND to_read_yet='no' ");
$result = mysql_query( "SELECT * FROM messages WHERE to_user='" . $_SESSION['username'] . "' AND to_read_yet='no'" );
Drop the second WHERE
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).
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