Calling for where tinyint is 0 (used as boolean) in query - php

I am currently using tinyint to store boolean values in mysql, and am trying to query a database but it is failing. The error I am getting is Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given which if im not mistaken just means the query didn't work. Before I post the query let me say I am using practically deprecated php for the query, however it's not going live and I just need it to work real quick. I know this stuff is all being updated so feel free to share any relevant materials (I do need to get caught up as it is) however the solution I am looking for is for my old school query. The query is:
$sql = mysql_query("SELECT * FROM contact ORDER BY id ASC WHERE read='0'");
where read is the tinyint in question.
I have tried WHERE read=0
and WHERE read=false
None of these are working, I do appreciate any help in advance!

You need to structure the query correctly:
"SELECT * FROM contact WHERE read=0 ORDER BY id ASC"
WHERE comes before ORDER BY.
Additionally, "mysql_num_rows() expects parameter 1 to be resource" is happening because you're calling a method on a failed query - not a resource. You could get a proper error on your query itself with something like mysql_query("SELECT... your query") or die(mysql_error()) But officially we all suggest moving to PDO or mysqli
And using their respective error reporting utilities.

Related

MySql query to Azure SQL query failing

I am trying to move my web site to Azure, and so to rely on their SQL servers, while before I was using Apache/MySql.
I have some difficulties trying to convert the queries, as they do not work anymore.
The following works if i connect to the MySql db, but it fails if I change the connection to the Azure SQL.
string(178) "SELECT * FROM schemedb.users WHERE CreatedBy=20 ORDER BY dateCreation Desc" string(177) "SELECT IdUsr FROM schemedb.userssettings WHERE User=20 ORDER BY Id Desc"
All connections to the Azure SQL server are correct, as a simpler query before these gets executed fine. But then I got stuck here.
The php error console says
Fatal error: Call to a member function setFetchMode() on a non-object in ...
To understand where the problem was I used var_dump on the queries, to see where they were failing, turns out that if I change them to:
string(178) "SELECT * FROM schemedb.users ORDER BY dateCreation Desc" string(177) "SELECT IdUsr From schemedb.userssettings Order By Id Desc"
These queries are then valid, so I guess that the problem is the where clause then, but I have no clue of what's the problem.
-- EDIT
I've made some test, turns out that if I manually write the query, it works as expected.
What I mean is that the above mentioned query is build through several php custom methods like so:
." WHERE ".$this->buildWhere()." ORDER BY ". etc.
But if I type there WHERE CreatedBy = 20, then it works.
My best guess is that it is a format problem, since in SQL Azure the data type in the query must be of the same type of the one declared in the column.
However, I cant' understand what's wrong, since my custom method is writing the value vithout ', therefore as a number, not a string...

Assign the results of an SQL query to a PHP variable [duplicate]

This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 9 years ago.
Forgive me but I am very new to PHP and SQL.
I am simply trying to assign the results of an SQL query to a PHP variable ($num). The SQL query is counting how many times customer_id appears in a table. However when i use the code below i get the error :
Warning: mysql_result() expects parameter 1 to be resource, boolean given in ... line **
session_start(); is at the top of the page and {$_SESSION['userid']} is actually an integer value of the currently logged in user which corresponds to the customer_id in the bookings table. Database connection is the include/db_connection.php (which i know works). The code i am using currently is :
<?php
include 'include/db_connection.php'
$num = mysql_result(mysql_query("SELECT COUNT(*) FROM bookings WHERE customer_id={$_SESSION['userid']}"),0);
?>
The fact that a boolean is given suggests that the query failed, and the mysql_query returned FALSE. You could try to use mysql_query("foo") or die(mysql_error()); to find out what's wrong.
You need to tell php to fetch the row - mysql_query() simply returns a boolean (true/false) result. Store the query result in a variable and fetch the result with $row = mysql_fetch_assoc($myqueryresult) or $row = mysql_fetch_array($muqueryresult) and use the field result from $row['column'] where needed.
It might be easier to use the count rows function in php rather than COUNT in sql too.
Ideally you should use the mysqli api to query mysql instead as it is much more secure and modern.

PHPMYADMIN returns result, PHP Query doesnt

So, I created a query in PhpMyAdmin to pick one random online member that is part of a certain group. It works fine in PhpMyAdmin and does exactly what I want. However, when I run this query using PHP it does not return anything. I simply get 'NULL' when I use var_dump($result).
$sql= "SELECT
ow_base_user_online.userId,
ow_base_authorization_user_role.roleId
FROM
ow_base_user_online
INNER JOIN ow_base_authorization_user_role ON ow_base_authorization_user_role.userId = ow_base_user_online.userId
WHERE
ow_base_authorization_user_role.roleId = 14
ORDER BY
RAND()
LIMIT 1";
$result = OW::getDbo()->queryForList($sql);
Please, does anyone have any ideas?
Please does anyone have any ideas?
Yes, use error reporting from your mysql query and see what it returns. Check PHP errors too (can get auth warnings to show something daft like bad user/password etc).
Also, for testing, try removing the class you use and stick the DB connection code above that query and query all direct from a test script. If it works fine, then your query is ok, as is the connection and credentials.
From there you can work backwards checking the class you have.

MySQL ORDER BY statement returning boolean?

I have absolutely no idea why this is happening, but my simple MySQL statement using an ORDER BY ... DESC command gives a really weird error when I try to perform the query.
The error is
mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in E:/.../home.php on line 23
And my code is:
$data = mysql_query("SELECT * FROM `blogposts` LIMIT 0, 30 ORDER BY id DESC");
while($results = mysql_fetch_assoc($data))//error here
I can't find out why. Any help is appreciated.
Oh, by the way, I know that everyone who looks at this question is going to rip into me for still using mysql.* libraries and there will be a highly upvoted comment about the evils of it and the fact that I am vulnerable to SQL injection. To answer before it's asked, as it were, this is not going online, it's purely a home project running on localhost. So don't even bother lecturing me.
ORDER BY clause must come before the LIMIT clause
SELECT * FROM `blogposts` ORDER BY id DESC LIMIT 0, 30

Query won't match using LIKE with returned ajax data

I'm trying to match up profiles in my MySQl database with the names and skillsets that are dropped into my droppable div, See HERE. I come across two problems one being the error mysql_fetch_array() expects parameter 1 to be resource, boolean given which I believe means my query is returning false. That takes me to the other problem, my $data not matching anything. How can I match the names and skills dropped into the droppable div with the names and skills in my database?
if (isset($_POST['data'])){
$data = $_POST['data'];
$query = mysql_query("SELECT * FROM gradesheet WHERE 'firstname','lastname','grade' LIKE '{$data}'");
while($row=mysql_fetch_array($query)){
$firstname=$row['firstname'];
$lastname=$row['lastname'];
$gradet=$row['grade'];
$user_id=$row['user_id'];
echo $firstname;
I used %Like% thinking it would give me a better chance than MATCH AGAINST. I would appreciate any knowledge or ideas on matching my query better and having it return something. As well as any tips in general.
You need to do a LIKE for each of the columns in your WHERE statement, and choose either AND or OR to bring them together. The percent symbol % is a wildcard.
SELECT * FROM gradesheet
WHERE 'firstname' LIKE '%term%'
OR 'lastname' LIKE '%term%'
OR 'grade' LIKE '%term%'
You should also avoid using the mysql_* PHP extensions as they have been deprecated; use PDO or mysqli instead. Aside from using deprecated extensions, your code is also wide open for a SQL injection attack. You should always filter data from request variables before sticking it into a query.

Categories