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

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.

Related

Is mysql return data to php variable when error [duplicate]

This question already has answers here:
How to check if a MySQL query using the legacy API was successful?
(6 answers)
Closed 6 years ago.
so i created php variable contain mysql query inside , but is it will return data to the php variable when the table / column / data is not available ?
example :
$a1='1';
$item1nanowr = mysql_query("SELECT item_name FROM item_info WHERE item_special_number = $a1 ");
now just imagine that item_name column not available , is it will return data ?
and if i put "or die" statement like this :
$item1nanowr = mysql_query("SELECT item_name FROM item_info WHERE item_special_number = $a1 ") or die("Coming Soon");
is it will return word Coming Soon ?
or die() will run only if something goes wrong with the query. Several things could go wrong. Some of them are:
The database table does not exist
One of the specified columns does not exist
There is a syntax error in the query
The database is offline or the connection has been lost
So it will run on critical errors. It won't run if your table or a specific column is empty.
Also it is very important to stop using mysql_ functions!. The mysql extension is deprecated from PHP 5.5 and completely removed from PHP 7.
Use mysqli or PDO instead. You should also use Prepared Statements instead of directly interpolating variables in your query. More on Prepared Statements here

COUNT values in a column and store count in variable? [duplicate]

This question already has answers here:
How can I store the result of an SQL COUNT statement in a PHP variable
(4 answers)
Closed 8 years ago.
I have tried a few things and googled but can't get something that works. I want to use an SQL query to count how many rows in a table has "Rock" as it's value inside the "Genre" column. And then I need to store the count number inside a variable so that I can echo it later in my page. Any help? I'm new to php...
A query like this
$lo_rec = mysql_query("SELECT COUNT(`genre`) as `total`
FROM `tablename`
WHERE `genre` = 'ROCK'");
$result = mysql_fetch_object($lo_rec);
Then when you grab the results, assign it
$li_count = $result->total;
echo $li_count;
And before anyone says so, I know to use mysqli or PDO :-)

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

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.

how to get data after sql inerst command in php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there a way to return the id of a row that was just created in MySQL with PHP?
hi, i have an inset command such as
insert (name) values($name);
where i have id column as outoincrement how can i get the id of the inserted record after inserting directly
If you are working with mysql_* function, you'll have to use mysql_insert_id() (quoting) :
Retrieves the ID generated for an
AUTO_INCREMENT column by the
previous query (usually INSERT).
With mysqli, you'll use mysqli::insert_id().
And, with PDO, you'll call PDO::lastInsertId().
mysql_query("Your query here");
$last_id = mysql_insert_id();

Is there a way to see a prepared query as it will be executed on the database? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PDO Prepared Statements
I'm using the mysqli extension in PHP and I'm wondering, is there possibly any way to see a prepared query as it will be executed on the server, e.g. The query is something like this
select * from table1 where id = ? and name = ?
but I want to see the query after the values are filled in, like this:
select * from table1 where id = 20 and name = "John"
Turn on mysql query logging and it will log all queries to a text file for you to review.
Duplicate of PDO Prepared Statements
Short answer: no. A prepared query will never be converted to the query you expect. It's executed directly by the database server. You can use mysql's query log or PDO's undocumented function debugDumpParams, but both are just approximations.
See it where?
If it's your code you have the query and you have the prepared parameters, log them separately or replace in the original query string.
If the binding will fail you will get an error, otherwise you should expect the same values to be "filled" in as you specified them.
Its the way most of the times I am debugging mysql quires:
$q = "select * from table1 where id = ".$id." and name = ".$name;
echo $q;
The output generates all variables assigned to the query.
Hope I understood you exactly, what you wanted.

Categories