Get just the name from mysql_query resource - php

$agent_query=mysql_query("
SELECT name FROM users WHERE id='$agent_id'
");
$get_agent_name=mysql_fetch_assoc($agent_query);
$this->session->agent=$get_agent_name['name'];
I know the mysql_fetch_assoc() expects parameter 1 to be resource, but is there a way I can get just the name without running any loop in Zend?

The php function mysql_result can be used to return a single field for a query by specifying the row and column you want returned. As there is only 1 row and column for your query (I assume), they will always be 0 and 0.
$name = mysql_result($agent_query, 0, 0);

Related

Why can I not store booleans in a php array?

Why can't I store a boolean in an array? I get an error when I attempt to run it. (On line line 3)
The columns being retrieved with the exception of stamp are booleans. Here's a snippet of my code.
$BoolQ = "SELECT stamp, active, latvian, russianSpeaker FROM tasktable WHERE taskID=usrid;";
$Boolr = mysqli_query($connection,$BoolQ);
$Boolrow = mysqli_fetch_array($Boolr);
Error:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
In the 3rd line of the code: $Boolrow = mysqli_fetch_array($BoolQ); , shouldn't you be using $Boolr as the parameter instead of $BoolQ? If that's a typo, which most probably it is, the result of mysqli_query is false, probably an issue with connection or the query.

mysqli_num_rows function returns 1 instead of 0

I'm fairly new to mysql and php development. I'm trying to understand why the mysqli_num_rows doesn't return what I expect - zero rows when there are definitely no records returned when I run the SQL statement from phpmyadmin.
I've searched around and I have failed to find a specific reason for why this isn't working.
Thank you in advance for any assistance.
An extract from my code is shown below:
if (isset($_GET['q']) && !empty($_GET['q'])){
$sql_wo = "SELECT MAX(wo_nbr) AS wo_nbr FROM workorders WHERE proj_id = '".$_GET['q']."'";
$result_wo = mysqli_query($connect,$sql_wo);
$rowCount_wo = mysqli_num_rows($result_wo);
echo "$rowCount_wo";
//returns 1 instead of 0???
When you use MAX(), COUNT(), etc you will always get one row returned even if the count or MAX is zero.
mysqli_num_rows() function returns the number of rows in a result set. And when you use COUNT() or MAX(), there would always be one row giving/showing you the result set.

I am getting a error in the log, but the scripts do what I want

I am trying to do the following.
SELECT from database and echo, If there are less than 3 results, I want to subtract the amount of results from 3 and use an alternative SELECT string, The code works and does what I expect of it except for the error in the logs.
PHP Warning: mysql_num_rows() expects parameter 1 to be resource,
null given in
This error seems to be because the result is empty, How can I check how many rows in a result without getting an error when the table has no results.
$sqls="SELECT * FROM xxx ORDER by xxx_id ASC LIMIT 0,3";
$objRs = mysql_query($sqls);
The database works, the SQL query works, the row below this is the one giving me the error when I have a NULL result. Is there another way to check the number of rows without getting the server pissed at me
$count = mysql_num_rows($objRS);
If (!empty($count)) {
$pnb = 3;
} else {
while($rows = mysql_fetch_array($objRs, MYSQL_ASSOC)) {
The code "works" by coincidence alone. You have a typo:
$objRs
is not the same thing as
$objRS
Variable names are case-sensitive. But the comparison you have set up doesn't actually check any results. This:
!empty($count)
is just checking if $count has any value. Which it does. Whether that value is a positive number or not, your code doesn't care. But it probably should...

php mysqli to call stored proc, get get results

I have a stored proc that does a geospatial query. The proc issues two sql statements but only the 2nd one does a query but unfortunately both statements produce a result set. I need the second result set which contains the results of the actual query.
The first statement sets a bounding box:
SET #bbox = 'POLYGON(($polygon))'; \n
SELECT * , AsText( location )
FROM users
WHERE Intersects( location, GeomFromText( #bbox ) ) [snipped for brevity]
If I run the above in phpMyAdmin, it works but I get the following message AFTER the SET command is issued and I want to throw this away:
# MySQL returned an empty result set (i.e. zero rows).
On the php side, I build the query string, calling the stored proc and on return the first thing I do is throw away the empty result set.
$query = "CALL usp_queryByPolygon('$polygon', $msg_id, $user_type)";
$result = mysqli_query($cxn, $query) or die("GEOCODE: queryPolygon - " .sql_error());
sql_free_result($result);
After throwing away the result set I now need the results of the query and this is what I have done:
$result = sql_next_result();
The problem is when I try to use this second result set as in:
if(mysqli_num_rows($result) > 0)
I get errors:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given
in /blah/blah/module.php on line 96
To complicate things, all of the above is in a loop and there could be dozens or 100's of polygons to search.
So the question is this: what is the proper way to get that 2nd result set?
You'd better be accurate of what functions you execute. sql_next_result() is no standard PHP function, nor is it in MySQLi which you seem to use. If it's some kind of database class, please just show the methods that class uses. Nobody here can but quess what sql_next_result() does.
Assuming you're talking about mysqli_next_result(), that indeed returns a boolean, you need to call mysqli_use_result() after that in order to retreive the next result set.
Found out the two statements: SET #bbox and SELECT can be executed sequentially so mysqli and the two results are just fodder that don't need to be dealt with.

PHP fetch MySQL array with an index

When fetching an array from MySQL the rows are typically returned with a key from 0 to the size of your recordset:
row[0][key][value]
Is it possible to have one of the fields from the select statement returned as the key in the array?
For example. Assuming my data set has StudentID, Name, City, etc.
How can I select into an array where I could refer to the StudentID as the index like this:
rows[StudentID][Name]
rows[StudentID][City]
etc.
Thanks!
PDOStatement::fetchAll
To return an associative array grouped by the values of a specified column, bitwise-OR PDO::FETCH_COLUMN with PDO::FETCH_GROUP.
// Other PDO stuff to get a statement - abstract below
$result = PDOStatement::fetchAll( PDO::FETCH_COLUMN | PDO::FETCH_GROUP, 0 );
See example 3 on this page
Depending on which library you are using:
mysql_fetch_assoc()
mysqli_fetch_assoc()
PDO fetches both by default.

Categories