This question already has answers here:
MySQLi equivalent of mysql_result()?
(12 answers)
Closed 10 months ago.
I am trying to convert a mysql script to mysqli and have hit a wall. I am trying to convert a mysql_result to mysqli however am unsure how to do this, below is my code
$_SESSION['num_user'] = mysql_result(mysqli_query($GLOBALS["___mysqli_ston"], "SELECT COUNT(*) FROM `members` WHERE mem_emailactivated = 1"), 0);
As weird as it is, there doesn't seem to be a way to fetch without prepare/execute.
$stmt = mysqli_prepare($GLOBALS['___mysqli_ston'], "SELECT COUNT(*) AS count ...");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $count);
mysqli_stmt_fetch($stmt);
$_SESSION['num_user'] = $count;
You are mixing things up here. You wouldn't use mysql_result to get the result from a query using mysqli. you would instead use mysqli to iterate through the returned array and then store this array in the $_SESSION.
$result = $mysqli->query("call getUsers()");
if($result){
// Cycle through results
while ($row = $result->fetch_object()){
$user_arr[] = $row;
}
$result->close();
}
$_SESSION['num_user'] = $user_arr;
Related
This question already has answers here:
php JSON_encode not working
(4 answers)
Closed 1 year ago.
What I am trying to achieve is to simply put all the rows of a mysqli result to a JSON file.
My code looks like this:
$sth = mysqli_query($mysqli, "SELECT * FROM table");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
print_r($rows);
$mysqli->close();
$fileobj = fopen("takeOutItems.json", 'w');
fwrite($fileobj,json_encode($rows));
fclose($fileobj);
printing the $rows arrays shows data correctly.
fwrite, however, does not change anything in takeOutItems.json.
What am I doing wrong?
The issue was that some data elements were not displayed correctly. Adding $mysqli->set_charset("utf8"); resolved the issue.
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
I searched the Internet for some Solutions but none of it works and I can't find out why. I'm new to PHP and MYSQL/MyAdmin so I really don't understand what I did wrong.
I've already tried several commands and "while $row = $result->fetch_array()" and stuff like that.
$Word = "SELECT Word FROM RandWord WHERE Number = '$Number'";
$Word = mysqli_query($data, $Word);
echo $Word;
$Amount = count($Word);
I want it to Output the "Count($Word);" for me, but it can't even echo because it can not be converted into a string. I want to see the word and use it.
You need to actually fetch the results first, before you can use it. You are also vulnerable to SQL injection attacks, so use a prepared statement instead.
In your code $Word is a result-object, which holds information - but you need to use a fetching method to retrieve the data first. count() in PHP is only usable on arrays (or on objects, but not that object which you get from mysqli_query()).
$query = "SELECT Word FROM RandWord WHERE Number = ?";
$stmt = $data->prepare($query);
$stmt->bind_param("s", $Number);
$stmt->execute();
$stmt->bind_result($word);
while ($stmt->fetch()) {
echo $word;
}
$stmt->close();
If you want it to be a count of each word, you have to run a query using the MySQL function COUNT() instead.
$query = "SELECT Word, COUNT(Word) as cnt FROM RandWord WHERE Number = ? GROUP BY Word";
$stmt = $data->prepare($query);
$stmt->bind_param("s", $Number);
$stmt->execute();
$stmt->bind_result($word, $count);
while ($stmt->fetch()) {
echo $word." has count ".$count;
}
$stmt->close();
This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 3 years ago.
I'm trying to echo the result of a mysqli_query however I keep getting the error 'Catchable fatal error: Object of class mysqli_result could not be converted to string' on line 'echo $result;'.
Is there any way I can convert it into a string so that it can be echoed? (P.S sorry if this is easy, I'm new to coding.)
My database is successfully connected and the SQL statements definitely work.
$sql= "SELECT ImageURL FROM `unnormalisedtable` WHERE Yeargroup = 9 ORDER BY RAND() LIMIT 1" ;
$result = mysqli_query($db, $sql);
echo $result;
The expected output is that the result of my SQLi query will be printed on screen, however the error is generated instead. Thanks for any help in advance.
Since you limit the selection to one entry use
$row = mysqli_fetch_array($result);
echo $row['ImageURL'];
If you select more than one entry loop over the result.
while($row = mysqli_fetch_array($result)) {
echo $row['ImageURL'];
}
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()) {
This question already has answers here:
Resetting array pointer in PDO results
(7 answers)
Closed 8 years ago.
I need to loop through the results of a query a few times. After the first loop, I cannot go through the results again as the pointer is at the bottom. How can I make it return to the top? I dont want to run the query again as that is just going to use up more resources and return the same results.
$stmt = $conn->prepare("SELECT * FROM customers");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
//This kind of loop will repeat elsewhere in the code
while($row = $stmt->fetch()){
//Do something here
}
while($row = $stmt->fetch()){
$rows[] = $row; //use $rows later
//Do something here with $row
}