Can't select a number from MySQL PHP - php

I am trying to get a value from database and echo it. It seems I mixed something wrong, so at the moment just no value appears.
$postamount=$db->prepare("SELECT post_amount FROM users WHERE id = ? ");
$postamount->bind_param('s',$_SESSION['id']);
$postamount->execute();
$fsvsfnvdjn= mysqli_fetch_assoc($postamount);
$fvklnsfvnfv=implode($fsvsfnvdjn);
echo($fvklnsfvnfv);
Such warning appear in the log:
mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in /Applications/MAMP/htdocs/pageok.php on line 25
implode(): Argument must be an array in /Applications/MAMP/htdocs/pageok.php on line 26
Can you explain please, how to do this right?

Alter your script with the following
$postamount=$db->prepare("SELECT post_amount FROM users WHERE id = ? ");
$postamount->bind_param('s',$_SESSION['id']);
$postamount->execute();
$result = $postamount->get_result();
$arr = array();
while ($row = $result->fetch_assoc()) {
$arr[]=$row["post_amount"];
}
$str=implode($arr);
echo($str);

Related

How to get result array from mysql in PHP

How to get result array from mysql in PHP ?
here is my code
$company = $_POST["company"];
$query = $this->db->query("Select id from users WHERE company like '$company'") ;
$user_id = mysqli_fetch_array($query,MYSQLI_ASSOC);
print_r ('$user_id');
here error message
Severity: Warning
Message: mysqli_fetch_array() expects parameter 1 to be mysqli_result,
object given
can anyone fix my code ??
Use codeigniter query builder:-
$company = $_POST["company"];
$query = $this->db->query("Select id from users WHERE company like '$company'") ;
foreach ($query->result() as $row)
{
echo $row->available_fields;
}
Don't mix mysql with codeigniter query builder classes. Refer this link for better understanding https://www.codeigniter.com/userguide3/database/query_builder.html

mysqli_fetch_array parameter error

I have this problem where I am trying to get input from the user and then display information. The user is meant to insert an ID number, obtained by $_POST[PID], then I catch that in result and when I try to get the result and print their info. This is the code:
$result = mysqli_query($connection, $_POST[PID]);
while($row = mysqli_fetch_array($result))
{
echo some info
echo "<br>";
}
However such code yields this error:
mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
How can I fix such error such that I am able to obtain the input and use it in mysqli_fetch_array
The second argument for mysqli_query() must be a query.
For instance:
$var = "insert into tbl where field = '{$_POST['PID']}'";
$result = mysqli_query($connect, $var);
Read it here:
http://www.w3schools.com/php/func_mysqli_query.asp
Refer to my answer from your previous question(similar to this question)
Obtain resource from POST php
Please learn about these functions to properly use them (Link here)
Instead of $_POST[PID] in mysqli_query() there must be some query string like
"select * from tablename where id ='". $_POST['PID']."'
Also, you need to enclose the key name within single quotes like $_POST['PID']

mysql_fetch_row() expects parameter 1 to be resource, object given.? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
What is the cause of error in my code and how I can to fix?(i use of codeigniter)
$query_hotel_search = $this->db->query("SELECT * FROM hotel_submits WHERE name LIKE '%$hotel_search%' ORDER BY name asc");
$units = array();
while( $row = mysql_fetch_row( $query_hotel_search ) ) // Line 27
{
$units = unserialize( $row[0] );
}
echo json_encode(var_dump($units));
Error:
A PHP Error was encountered Severity: Warning Message:
mysql_fetch_row() expects parameter 1 to be resource, object given
Line Number: 27
Output: array(0) { } null
UPDATE:
Error:
A PHP Error was encountered Severity: Notice Message:
unserialize() [function.unserialize]: Error at offset 0 of 3 bytes
Line Number: 29
Output: bool(false) null
See my database: http://i.stack.imgur.com/IORSM.jpg
$query_hotel_search = mysql_query("SELECT * FROM hotel_submits WHERE name LIKE '%$hotel_search%' ORDER BY name asc");
if(mysql_num_rows($query_hotel_search)==0){
return '0';
}else{
$units = array();
while( $row = mysql_fetch_row( $query_hotel_search ) )
{
$units = unserialize( $row->units[0] ); // Line 29
}
echo json_encode(var_dump($units));
}
http://de2.php.net/manual/en/function.mysql-fetch-row.php
mysql_fetch_row() takes not the query itself but rather the result from the mysql_query command (which is a resource).
Look at the example at the PHP Documentation (linked at the top) and you will get the functionality of mysql_fetch_row.
Maybe the assignment of $query_hotel_search can clarify the situation.
You are using a codeigniter object as mysql resource. Instead you should use object iterator provided by codeigniter to loop through the results.
foreach($query_hotel_search->result() as $row)
{
// $row as object
}
foreach($query_hotel_search->result_array() as $row)
{
// $row as array
}
For the second part (UPDATE), mysql_fetch_row() return an enumerated array. so you should use:
$units = unserialize($row[12]);

PHP: Warning: sort() expects parameter 1 to be array, resource given

I wanted to arrange the array of table list with sort() function but i am getting same kind of warning.
<?php
require_once("lib/connection.php");
$result = mysql_query("SHOW TABLES FROM `st_db_1`");
sort($result);
foreach ($result as $result){
echo $result ;
}
?>
and the warning I am getting are:
Warning: sort() expects parameter 1 to be array, resource given in C:\wamp\www\Copy (4) of st_db_1\test_2.php on line 9
Warning: Invalid argument supplied for foreach() in C:\wamp\www\Copy (4) of st_db_1\test_2.php on line 10
The warning is pretty clear: mysql_query does not return an array with results from the query, but a resource. You need a function like mysql_fetch_array() to return the data you need (and on which you can perform a sort operation).
See the manual for the use of mysql_query() http://nl3.php.net/mysql_query
And maybe unrelated, but you can sort your results in MySQL right away by adding ORDER BY <fieldname> to your query.
The variable $result is only a resource of the type result. You need to fetch then the data from the result set with e.g. mysql_fetch_assoc().
$result = mysql_query("SHOW TABLES FROM `st_db_1`");
$array = array();
while ($row = mysql_fetch_assoc($result)) {
$array[] = $row["Tables_in_st_db_1"];
}
sort($array);
foreach ($array as $item) {
echo $item;
}
I'm not providing the most efficient code imaginable, but this should make it clear what's going on and solve your problem:
$result = mysql_query("SHOW TABLES FROM `st_db_1`");
$my_array_of_table_names = array();
while ( $row = mysql_fetch_array($result, MYSQL_NUM)) {
$my_array_of_table_names[] = $row[0];
}
sort($my_array_of_table_names);
foreach ($my_array_of_table_names as $table_name){
echo "$table_name\n";
}
Your problem is that you aren't actually getting the data from the query.
mysql_query() doesn't give you a recordset.
What it does is query the database and returns a database resource which you can then use to get the data.
What you need is after calling mysql_query(), you then need to also call mysql_fetch_array() or similar. (there are a range of functions available, but that's probably the best one to use in this case). Then sort() the data from that, not $result.
It clearly says: it expects an array and you pass something else.
If you had checked the type of $result you would have seen that it is not an array, intead a resource.

Using PHP/MySQL to Calculate Percentages

I'm trying to calculate the number of students who've completed an their homework for an online gradebook, and I can't figure the code out...
// SELECT THE TOTAL
$gettotal = "SELECT enroll FROM student_course WHERE classID = $classID";
$showtotal = #mysqli_query ($dbc, $gettotal); // Run the query.
//THIS IS LINE 108
$numtotal = mysql_num_rows($showtotal);
echo '$numtotal';
// SELECT THOSE PASSED
$getpassed = "SELECT entry FROM grades WHERE classID = $classID AND test_result >= 80";
$showpassed = #mysqli_query ($dbc, $getpassed); // Run the query.
$numpassed = mysql_num_rows($showpassed);
//THIS IS LINE 117
echo '$numpassed';
// PERFORM THE PERCENTAGE FUNCTION
function percent($numpassed, $numtotal) {
$count1 = $numpassed / $numtotal;
$count2 = $count1 * 100;
$count = number_format($count2, 0);
echo $count;
}
//THIS IS LINE 124
percent($numpassed, $numtotal);
I get the following error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource on line 108
$numtotal
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource on line 117
$numpassed
Warning: Division by zero in on line 124
0
Okay - while I thank everyone for their concern removing the # ... no one noticed that the problem was using mysqli_query and then mysql_num_rows. It needed to be changed to mysqli_num_rows.
Thanks though :)
Chances are the query has failed.
Also mysql_query will return FALSE on error so you can check for that.
$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
Btw: You might want to remove the # operator before mysql_query so you see if something goes wrong there.
Remove error suppression (#) in your code and use inspection methods print_r($var) and var_dump($var) to verify the returned database call values.

Categories