Remind first row after getting all data - PHP PDO [duplicate] - php

This question already has an answer here:
Reset cursor position in PDO
(1 answer)
Closed 4 years ago.
I am using php PDO for server connection. just like that
$stmt = $pdo->prepare('call naviSql( ?, ?, ? )');
$stmt->execute( array( $someone, $on_navi, $on_date ) );
while( $row = $stmt->fetch( ) ) {
echo '';
}
now after echoing out all the data, how i can remind first row values once again?

Use fetchAll instead and store all your results in a variable. The first row can then be accessed by its index, i.e.:
// Store all fetched rows in a variable
$results = $stmt->fetchAll();
// Iterate through all your results
foreach($results as $row) {
echo '';
}
// Re-access the first row
$firstRow = $results[0];
print_r($firstRow);

Related

mysqli array to mysql in subquery? [duplicate]

This question already has answers here:
MySQLi Bind Param with an array for IN [duplicate]
(2 answers)
Closed 4 years ago.
I have this select:
$stmt = $mysqli->prepare("select following from following where user =? and block=0");
$stmt->bind_param('i', $user);
$stmt->execute();
$stmt->bind_result($followers);
$stmt->fetch();
$stmt->close();
I'd like to use $followers in another mysql conection:
select... from ... where (p.user in (?))
in this ? I'd like to place the $followers variable in mysql in format. (1,5,7,2).
Any ideas how to convert the bind_result $followers into this mysql in format?
One way to accomplish this would be to create another string variable and append the result of $followers in a while loop.
I typically work in procedural style, and I am assuming that your statement returns multiple rows into $followers:
$sql_in_string = ''; // initialize string variable as blank
while (mysqli_stmt_fetch($stmt)){
$sql_in_string = $sql_in_string . $followers . "," ; // append value + comma
}
$sql_in_string = rtrim($sql_in_string,","); // remove the final comma
At this point, $sql_in_string should have your values as "1,2,3" format and then you can bind that parameter to your next query.

Return MYSQLi Prepared Statement Query and Bind Results an an Array [duplicate]

This question already has answers here:
How to fetch all in assoc array from a prepared statement?
(4 answers)
Closed 2 years ago.
I want to put the result of a mysqli prepared statement into an array and return the array for use in another page.
The code is presented below.
function getPermission($mysqli , $email) {
if ($stmt = $mysqli->prepare("SELECT m.email , g.permission
FROM members m
INNER JOIN members_groups q ON m.member_id = q.member_id
INNER JOIN groups g on q.group_id = g.group_id
WHERE email = ?"))
{
$stmt->bind_param('s', $email); // Bind "$email" to parameter.
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
// get variables from result.
$stmt->bind_result($user_email, $user_perms);
while ($stmt->fetch()) {
echo ' ID: '.$user_email; //prints the results.
}
}
}
I want to place the query results into an array and return the array.
Note: Am using a wamp server with php and mysql in windows 7. I tried to use the $stmt->getresults method, but it returns nothing.
You could try to store it in an array, one by one, in your loop:
$results = array();
while ($stmt->fetch()) {
$results[] = array(
"email" => $user_email,
"perms" => $user_perms
);
}
return $results;
If you need this array in another file, where you include this function, this will work, but if you need it in another request, PHP will have to run this code again to have the array filled.
If you need this array during the session, then consider putting the result in a session variable:
$_SESSION['myarray'] = $results;
Then in the function, first check if the above session variable is set, and if so, immediately return that value. Only when it is not yet set, you perform the rest of the function.

Looping through a PHP object [duplicate]

This question already has an answer here:
PDO looping through and printing fetchAll
(1 answer)
Closed 7 years ago.
I did a query in php and all the results are stored in an object. For example
$query = "select * from some_table";
$sth = $dbc->query($query);
$results = $sth->fetchAll();
Therefore the results of the query is stored in $results and I have a property called name. How can I now loop through this object $results to change all the values of a certain property name of the object.
Assuming name is a public property:
for($i = 0; $i < count($results); $i++){
$results[$i]->name = ...;
}

Reset row pointer of a mySQL result (PDO) [duplicate]

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
}

Converting mysql_result to mysqli when creating a session [duplicate]

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;

Categories