I have this code which bring a single post from my database depend on id
global $connect;
$id = $_GET['id'];
$sql_query="SELECT * FROM `topics` WHERE id = {$id}";
$result = mysqli_query($connect,$sql_query);
while ($row = mysqli_fetch_assoc($result)) { ....}
How can i make this as function (My try) :
Function get_single_post($id) {
global $connect;
$id = (int)$id;
$sql_query="SELECT * FROM `topics` WHERE id = {$id}";
$result = mysqli_query($connect,$sql_query);
while ($row = mysqli_fetch_assoc($result)) {
$post[] = $row;
}
return $post;
}
to use this function i use this :
get_single_post($_GET['id']);
and when i call something i use :
$post['title']; for title as ex
Remember, a function returns a value, but that value must be assigned to a variable for you to be able to access it.
$post = get_single_post($_GET['id]);
Using the above should now allow you to access the post as you expect.
If your id is primary key than you don't need while loop as it will return only one result
modified function
Function get_single_post($id) {
global $connect;
$id = (int)$id;
$sql_query="SELECT * FROM `topics` WHERE id = {$id}";
$result = mysqli_query($connect,$sql_query);
return mysqli_fetch_assoc($result);
}
Related
i have party id but i want to get party_name from party table and made a function but its not working can anybody please correct it where i am wrong?
function get_partyname($party)
{
global $database;
$sql = 'SELECT party_name from party WHERE id= '.$party;
$result = $conn->query($sql);
$row = $result->fetch_assoc();
extract($row);
return $party_name;
}
$sql = 'SELECT * FROM '.$table_name ;
$result = $conn1->query($sql);
while($row = $result->fetch_assoc())
{
echo $row['id'];
$party_name = get_partyname($row['party']);
echo $party_name;
}
You need to add
global $conn;
because your database connection cannot be accessed outside the function.
You just have to replace global $database; with global $conn;.That's it.
i have this code to call posts from database
<?php
$q = "SELECT * FROM posts WHERE user_id = '$user_info[id]' ORDER BY id DESC";
$r = mysqli_query($dbc, $q);
while($post_info = mysqli_fetch_assoc($r)) { ?>
//html
it works fine but i like to create a function includes the query and the while loop and move it to functions.php and keep the html code in template.php but I don't know how with the while loop..
function data_post($dbc, $user_info['id']){
$q = "SELECT * FROM posts WHERE user_id = '$user_info[id]' ORDER BY id DESC";
$r = mysqli_query($dbc, $q);
while($post_info = mysqli_fetch_assoc($r))
return $post_info
}
I have tried this but there is no result
function ss()
{
...........
while($post_info = mysqli_fetch_assoc($r))
{
$new_array[]=$post_info;
}
return $new_array;
}
And return the $new_array outside of the while loop
And get the value like this
$mm= ss();
print_r($mm); here you get that $new_array values
Try this:
Function Body
function data_post($dbc, $user_id){
$q = "SELECT * FROM posts WHERE user_id = '$user_id' ORDER BY id DESC";
$r = mysqli_query($dbc, $q);
$arrayPost = array();
while($post_info = mysqli_fetch_assoc($r)){
$arrayPost[] = array('id' => $post_info['postId'], 'name' => $post_info['postName']);
// or whatever data you want to return of post, insert in array
}
return $arrayPost;
}
Function Calling
$userPostArray = data_post($dbc, $user_info['id']);
So I have my code
function GetApi($connection,$UserId){
global $Apicall;
$Apicall = array();
$Apiidquery = mysqli_query($connection, "SELECT ID FROM ` Characterapi` WHERE UserId = '$UserId'");
while($results = mysqli_fetch_assoc($Apiidquery)){
$Apicall[] = $results['ID'];
}
}
The output of this function if I call
$Apicall[0] = 3
$Apicall[1] = 11
and this is the information I want. But now I want to use a function like
function Keyquery($Apicall,$connection ){
global $keyidcall, $keyid ,$Vcode;
$Keyidquery = array();
$Keyidquery = mysqli_query($connection, "SELECT keyid, Vcode FROM `Characterapi` WHERE ID = '$Apicall'");
$results = mysqli_fetch_object($Keyidquery);
$keyid = $results->keyid;
$Vcode = $results->Vcode;
}
This code does run if i set $Apicall ="3"; The issue im having is that I want the first function to get All the IDs associated with $userId in my data base then for each Id run the second function to to get the two specific pieces of information from that query.
In response to the comment below, this is the solution which I would use. However you should be wary of using this method as it does not parameterize the values, and as such not sanitized.
<?php
function Keyquery($Apicall,$connection ){
global $keyidcall, $keyid ,$Vcode;
$string = "ID IN('";
$string.= implode("','", $Apicall);
$string.="')";
$Keyidquery = mysqli_query($connection, "SELECT keyid, Vcode FROM `Characterapi` WHERE ".$string.";");
$results = mysqli_fetch_object($Keyidquery);
$keyid = $results->keyid;
$Vcode = $results->Vcode;
}
?>
please be easy on me, i just started learning PDO and still finding my way how to convert my mysqli to PDO.
so i have a function to get the contents from my database
function getContent() {
$db = PDOconn();
$query = "SELECT * FROM posts ORDER BY id DESC LIMIT 0,3";
$sql = $db->prepare($sql);
$row = $sql->fetchAll(PDO::FETCH_ASSOC);
return $row;
}
normally when i return $row in mysqli, i would define fetch_assoc() in my while loop.
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$content = $row['content'];
}
Now, since (PDO::FETCH_ASSOC) is already declared in my function.
how would i properly create my while loop to print the values in PDO?
[edit]
updated code
i will be declaring my while loop outside of the function. so i need something to return from my function but i dont know what that is..
function getContent() {
$db = PDOconn();
$query = "SELECT * FROM posts ORDER BY id DESC LIMIT 0,3";
$sql = $db->prepare($query);
$row = $sql->execute();
return $row;
}
this is my while loop outside the function.
$sql = getContent();
while ($row = $sql->fetchAll(PDO::FETCH_ASSOC)) {
$id = $row['id'];
$content = $row['content'];
}
With fetchAll() you don't have to use while at all. As this function returns an array, you have to use foreach() instead:
function getContent() {
$db = PDOconn();
$query = "SELECT * FROM posts ORDER BY id DESC LIMIT 0,3";
$sql = $db->prepare($query);
$sql->execute();
return $sql->fetchAll();
}
$data = getContent();
foreach($data as $row) {
$id = $row['id'];
$content = $row['content'];
}
I have the following function:
function getUser($user_id){
$mysqli = dbConnect();
$gu = "select * from users where user_id = '$user_id'";
$ru = $mysqli->query($gu);
$user = $ru->fetch_array();
return $user;
}
Which is called eg:
$user_id = $_SESSION[user_id];
getUser($user_id);
Then I want to simply echo fields i want, e.g. name. But, when I try the following, it returns empty
echo "users name is $user['name']"; // returns: users name is
Is there a better way to do this?
UPDATE Also tried the following but still empty:
function getUser($user_id){
$mysqli = dbConnect();
$gu = "select * from users where user_id = '$user_id'";
$ru = $mysqli->query($gu);
$user = array();
while($row = $ru->fetch_array()) {
$user[] = $row;
}
return $user;
}
your line:
getUser($user_id);
should be:
$user=getUser($user_id);
This way you'll be setting $user to the array the getUser returns, then you can use it.
Remove the single quotes when printing an array, you echo may needed to be like:
echo "users name is $user[name]";