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
Related
This question already has answers here:
MySQLI Prepared Statement: num_rows & fetch_assoc
(5 answers)
Closed 5 years ago.
I am trying to search a table for specific items using a prepared statement in PHP. I am getting no errors, but also getting no record. Here is my code:
$items = [];
$search = "john";
if ($stmt = $this->con->prepare("SELECT * FROM phptest WHERE search = ?")) { //'john'";
$stmt->bind_param("s",$search);
$stmt->execute();
while ($row = mysqli_fetch_array($stmt)) {
$item = [];
$item['id'] = $row['id'];
$item['first'] = $row['search'];
$item['last'] = $row['data'];
array_push($items, $item);
}
}
return $items;
Now, when I don't use a prepared statement, and just SELECT * FROM phptest I get all the results in the table (including the item where search = 'john'). Furthermore, if I use the query SELECT * FROM phptest WHERE search = 'john' I get the one record where search = 'john'
But as soon as I turn it into the prepared statement, I get zero errors but zero records. I do get a warning:
mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
Which made me think my bind_param or execute() was returning FALSE, but when I check, it does not appear to be returning false.
I started off my adventure working through the tutorial https://www.simplifiedcoding.net/android-mysql-tutorial-to-perform-basic-crud-operation/, which I thought I understood fully but ran into my error when trying to make my own PHP API.
I then went to the manual http://php.net/manual/fr/mysqli.prepare.php, but still cannot find my error.
Though it has been closed as "off-topic," I have reviewed PHP bind_param not working and found nothing applicable to my situation.
Likewise, I am not finding the error in PHP bind_param not defined nor php bind_param is not working.
You're very close. mysqli_fetch_array() expects to be passed a result object, not the statement object itself:
$stmt = $conn->prepare(...);
$stmt->bind_param(...);
$stmt->execute();
$result = $stmt->get_result();
while ($row = mysqli_fetch_array($result)) {
Or, in the fully OO manner:
while ($row = $result->fetch_array()) {
I have execute query using PHP which previously executed on mssql server database . Now with the same table and data. I using mysql database to execute my query. But error happen. Any suggestion for my query below in order to can execute using mysql database :
$year = mysql_query("SELECT * FROM education_year ORDER BY id DESC");
if (isset($_GET['year'])){
$educationyear= mysql_fetch_array(mysql_query("SELECT * FROM educationyear WHERE year='{$_GET['year']}'"));
}else {$educationyear = mysql_fetch_array($year);}
$kode['KODE'] = mysql_fetch_array(mysql_query("SELECT KODE FROM educationyear WHERE year='$educationyear'"));
$result = mysql_query("SELECT * FROM Province");
while($row = mysql_fetch_array($result))
{
$xd = mysql_fetch_array(mysql_query("SELECT COUNT (*) AS total FROM child WHERE id_province='{$row['province_code']}' AND education='A'
AND educationyear='{$educationyear['KODE']}'"));
}
Error message like below :
Notice: Array to string conversion in C:\xampp\htdocs\xy\demo.php on line 19
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\xy\demo.php on line 20 .
Its line when execute $xd query.
There are a few problems with your code
1st: When you use an array within double-quoted string, do not quote the array key. Change
"...WHERE year='{$_GET['year']}..."
"...WHERE id_province='{$row['province_code']}'..."
To:
"...WHERE year='{$_GET[year]}..."
"...WHERE id_province='{$row[province_code]}'..."
2nd: The design pattern below is not good:
mysql_fetch_array(mysql_query("SELECT...")
You're taking the result of mysql_query and feeding it directly to mysql_fetch_array. This works as long as the query succeeds and returns a resource. If the query fails, it will return FALSE and mysql_fetch_array will trigger the error you see:
mysql_fetch_array() expects parameter 1 to be resource, boolean given
Instead, make sure there is no error before proceeding
$result = mysql_query("SELECT...")
if($result===false){
//Query failed get error from mysql_error($link).
//$link is the result of mysql_connect
}
else{
//now it's safe to fetch results
$record = mysql_fetch_array($result);
}
3rd: do not use mysql_ functions. They have been abandoned for years and have been removed from the most recent version of PHP. Switch to MySQLi or PDO
4th: learn about prepared statements. You're using user supplied input directly in your query ($_GET['year']) and this makes you vulnerable to SQL injection.
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);
I'm not very good yet with sessions or arrays.
Here is what I have so far but I am getting an error
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource.
Here is my code:
$checkgroupadmin = $this->db->query("SELECT group_id FROM groupadmin
WHERE contact_id = $contactid");
while ($adminrow = mysql_fetch_array($checkgroupadmin, MYSQL_ASSOC)) {
$_SESSION[group_admin] = array('group'=>$adminrow['group_id']);
}
Any help would be appreciated. :)
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource.
You are getting your error because $checkgroupadmin is not an actual query resource that mysql_fetch_array() requires.
$checkgroupadmin = $this->db->query("SELECT group_id FROM groupadmin WHERE contact_id = $contactid");`
In other words the above object oriented call is not returning a mysql resource query.
Why are you mixing object oriented style with procedural style anyways. Secondly the above code $this>db->query should be happening inside a Class' method
mysql_fetch_array() returns an associative array of the current results, but in your code you would just be overwriting the same value in the $_SESSION for each result. You likely need to create your own array of resulting IDs and then assign that to a $_SESSION variable.
// query database
$checkgroupadmin = $this->db->query("SELECT group_id FROM groupadmin WHERE contact_id = $contactid");
// array to hold each group_id
$result_array = array();
// varify that $checkgroupadmin returned results
if ($checkgroupadmin){
// get results into associative array
while ($adminrow = mysql_fetch_array($checkgroupadmin, MYSQL_ASSOC)) {
// put the group id into your results array
$result_array[] = $adminrow['group_id'];
}
}
// set results array to the 'group_admin' key in $_SESSION
$_SESSION['group_admin'] = $result_array;
Please note that the mysql_* extensions have been deprecated and you should be using mysqli or PDO instead.
Hey there, why does this code not work?
$qry = mysql_query("SELECT performerid,pic0 FROM ".$table." ORDER BY RAND() LIMIT 6");
$start = new WP_Query('showposts=6&orderby=rand');
if ($start->have_posts()) : while( $start->have_posts() ) : $start->the_post();
$rows = mysql_fetch_assoc($qry);
if (!$rows)
{
mysql_data_seek($rows,0);
$rows = mysql_fetch_assoc($qry);
}
$perfs = $rows['performerid'];
$pics = $rows['pic0'];
I ahve the following error:
Warning: mysql_data_seek(): supplied argument is not a valid MySQL result resource in /home/content/d/d/a/ddxxxx
Your call to mysql_data_seek only happens if $rows is null. If that's true, then the call to mysql_data_seek will certainly fail, because one of it's required args is null. That's why you're getting the error message.
The problem is you're passing the wrong thing to mysql_data_seek(). It's expecting you to pass it $qry (your results object) and not the empty $rows variable you just tested.