How to separate arrays into variables - php

I've tried to look this up, but I'm not quite getting an answer that pertains directly to what I'm doing. I'm trying to retrieve values from a database that were previously submitted. I want to use these values to locate filepaths on the server and use the fillepath information to display the files. To do this, I need to separate the array that i have here. How would I separate this array?
$code = $_POST['codeInput'];
$code = htmlspecialchars($code);
$submitCodes = "SELECT story,video FROM `storycodes` WHERE `code` = $codeInput";
$files = mysql_fetch_array($submitCodes);
mysql_close($con);
print_r(array_values($files));
I appreciate any help guys. Ideally I would like to get a variable like $story or $storyPath and $video or $videoPath I need the video as a variable so I can play it with a video player.
EDIT:
I changed from mysql to mysqli and I'm getting all of these errors now that I can't seem to fix. I have the result set and it says I don't.
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/content/98/10339998/html/scripts/stories.php on line 26
I think this warning should fix itself once the array gets sorted out:
Warning: extract() expects parameter 1 to be array, null given in /home/content/98/10339998/html/scripts/stories.php on line 28
Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in /home/content/98/10339998/html/scripts/stories.php on line 30
$con = mysqli_connect("storycodes.db.10339998.hostedresource.com",$username,$password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysqli_select_db($con, "storycodes");
$code = $_POST['codeInput'];
$code = htmlspecialchars($code);
$query = "SELECT story,video FROM `storycodes` WHERE `code` = $codeInput";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($result);
extract($row);
mysqli_free_result($result);
mysqli_close($con);
echo $story . $video;

use it like this
$files = mysql_fetch_array($submitCodes);
extract( $files );
// now you can use it like this
echo $story . $video; // as variables

$files = mysql_fetch_assoc( $submitCodes );
extract( $files );

Related

Error : Array to string conversion in PHP

I'm getting this error while trying to execute my function.
The columns of my database are 100% correct and i tested every request and it worked!
Error :
[26-May-2018 05:21:49 UTC] PHP Notice: Array to string conversion in C:\wamp64\www\gcm\database.php on line 82
[26-May-2018 05:21:49 UTC] PHP Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\wamp64\www\gcm\database.php on line 88
My function:
function getHistoriqueNotification($user,$mdp){
$com = new DbConnect();
$message=array();
$sql="select UTLR_UID from adm_utilisateurs where UTLR_LOGIN='$user' and UTLR_MDP='$mdp'";
$result=mysqli_query($com->getDb(),$sql);
$getID = mysqli_fetch_assoc($result);
$userID = $getID['UTLR_UID'];
$sqli = "SELECT alr_alertes.ALRT_DES_LN1,alr_alertes.ALRT_PHOTO,alr_historiques.AHIS_DES_LN1,alr_historiques.AHIS_DATEHEURE from alr_alertes,alr_historiques WHERE alr_alertes.ALRT_UID=alr_historiques.ALRT_UID AND alr_historiques.UTLR_UID=$userID";
$resulti = mysqli_query($com->getDb(),$sqli);
while($row=mysqli_fetch_assoc($resulti)){
$message = array('photo' =>$row['ALRT_PHOTO'] , 'titre' => $row['ALRT_DES_LN1'] , 'dateHeure' =>$row['AHIS_DATEHEURE'] , 'detail' => $row['AHIS_DES_LN1']);
}
return $message;
}
When using mysqli you need to free up the results per each query. Since you made a query with the $sql and then a second query using the $sqli without freeing up the results in between the two queries it caused you the problem.
Please read on these mysqli functions:
http://php.net/manual/en/mysqli-result.free.php
Notice I closed your first connection and then made a second one. I think you can do this by keeping the same connection but calling mysqli_free_result() prior to your next query. I am not all that familiar with mysqli but I think that's right.
function getHistoriqueNotification($user, $mdp){
$com = new DbConnect();
$message=array();
$sql = "select UTLR_UID from adm_utilisateurs where UTLR_LOGIN='$user' and UTLR_MDP='$mdp'";
$result = mysqli_query($com->getDb(),$sql);
$getID = mysqli_fetch_assoc($result);
$userID = $getID['UTLR_UID'];
mysqli_close($com);
unset($com);
$com1 = new DbConnect(); //This is really just a test.
$sqli = "SELECT alr_alertes.ALRT_DES_LN1,alr_alertes.ALRT_PHOTO,alr_historiques.AHIS_DES_LN1,alr_historiques.AHIS_DATEHEURE from alr_alertes,alr_historiques WHERE alr_alertes.ALRT_UID=alr_historiques.ALRT_UID AND alr_historiques.UTLR_UID=$userID";
if ($resulti = mysqli_query($com1->getDb(), $sqli)){
while($row = mysqli_fetch_assoc($resulti)){
//Added the [] after message.
$message[] = array(
'photo' =>$row['ALRT_PHOTO'],
'titre' =>$row['ALRT_DES_LN1'],
'dateHeure' =>$row['AHIS_DATEHEURE'],
'detail' =>$row['AHIS_DES_LN1']
);
}
}else{
echo("Error description: " . mysqli_error($com1->getDb()));
}
print_r($message); //<---This will show you if you have a result.
return $message;
}

Warning: mysqli_fetch_array() expects parameter 2 to be long, object given in

the warning message above is shown when the site is searched. It is a simple search feature which displays all the records, matching the search word with a field in the table. The code is as below.
if(isset($_POST['submit'])){
$clean_search_word = mysqli_real_escape_string($con,$_POST['search_word']);
$sql = "SELECT * FROM webdir_user where user_category like '%$clean_search_word%'";
$record = mysqli_query($con,$sql);
if(!$record){
die('Error in SQL:'.mysql_error());
}
else{
while($result = mysqli_fetch_array($record,$con)){
}
}
}
I have found answers to kind of same issues as mine, but in most of the cases the warning message was caused by something else so I couldn't find any help with figuring out what caused the problem in my case. Any help or advise as to how to resolve. Thank you.
Remove the $con from your mysqli_fetch_array and it should work. There is only 1 string allowed in this 'function'. The other one ($con) is optional and has to be an integer. Like MYSQLI_ASSOC
if(isset($_POST['submit'])){
$clean_search_word = mysqli_real_escape_string($con,$_POST['search_word']);
$sql = "SELECT * FROM webdir_user where user_category like '%$clean_search_word%'";
$record = mysqli_query($con,$sql);
if(!$record){
die('Error in SQL:'.mysql_error());
}
else{
while($result = mysqli_fetch_array($record)){
}
}
}
mysqli_fetch_array
takes one parameter (the $result)
another optional one which is INTEGER (int $resulttype = MYSQLI_BOTH )

Can't select a number from MySQL 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);

PHP count rows of specific entries in database

New to PHP and overwhelmed by all the different solutions to similar problems. I don't know if I have a coding problem, a multiple query problem, or both/more.
In one php file I am opening a connection, running a query, and then on success counting the number of times that entry appears in the database... or at least attempting to.
// $team1, $team2 and $page come in through _POST up here...
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// build long query at this point...
$result = mysqli_query($connection, $query);
//I was successful getting it into the database, now I want to count how many times each entry appears.
if ($result) {
$team1result = mysqli_query($connection,"SELECT * FROM {$page} WHERE 'vote' = {$team1}") ;
$team1row = mysqli_fetch_row($team1result);
$team1count = $team1row[0];
$team2result = mysqli_query($connection,"SELECT * FROM {$page} WHERE 'vote' = {$team2}") ;
$team2row = mysqli_fetch_row($team2result);
$team2count = $team2row[0];
echo $team1count . " and " . $team2count;
}
I'm able to insert into the database just fine but then my console.log lights up with...
Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given in...
Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given in ...
Thanks for all the help tonight.
SOLUTION (Thanks to wishchaser):
if ($result) {
$team1rows = mysqli_num_rows(mysqli_query($connection,"SELECT * FROM $page WHERE vote = '$team1'"));
$team2rows = mysqli_num_rows(mysqli_query($connection,"SELECT * FROM $page WHERE vote = '$team2'"));
echo $team1 . " : " . $team1rows . " | ". $team2 . " : ". $team2rows;
}
There were no resulting rows for queries $team1result and $team2result. That is why you are getting this error.
use a if statement to check this
if($team1result)
$team1row = mysqli_fetch_row($team1result);
if($team2result)
$team2row = mysqli_fetch_row($team1result);
You will not get the errors.
And for counting the number of rows that a query result, use the folowing
$rows=mysqli_num_rows(mysqli_query($query));
and a good practice of finding the mistake in your query statement would be to echo it.
in this case
echo "SELECT * FROM $page WHERE vote = '$team1'";
echo "SELECT * FROM $page WHERE vote = '$team2'";
check if the echoed query has no mistakes(like an undefined variable).
You can easily count this using num_rows no need to access its index and all, simply use this
echo $team1row = mysqli_num_rows($team1result);
Reference Link

Warning: sqlite_query() expects parameter 1 to be resource, string given

I've looked all over the place and cant find a solution that helps my case. I hope someone can help?
I have this and receive
Warning: sqlite_query() expects parameter 1 to be resource, string given
It is relating to $dbresult line - so a problem with the query.
function Up(){
$dbquery = "DELETE FROM toolList WHERE toolId='".$data['toolId']."'";
$dbresult = sqlite_query($dbhandle, $dbquery);
}
foreach($result as $data){
print '< a href="'.Up().'">DELETE!< /a>';
}
Where is $dbhandle defined? I'm guessing it's a global variable, in which case you have to explicitly mention so within the Up function:
function Up(){
global $dbhandle, $data, $dbresult;
$dbquery = "DELETE FROM toolList WHERE toolId='".$data['toolId']."'";
$dbresult = sqlite_query($dbhandle, $dbquery);
}
You are either missing a call to sqlite_open(), or your $dbhandle variable is not available in your function.

Categories