i am experiencing some weird behaviour here.
i am using the following code to reference the facebook api.
$query = "SELECT msg, user_id, comment_time FROM comments WHERE aid = '$aid' ORDER BY comment_time DESC";
$result = mysql_query($query) or die("ERROR: $query.".mysql_error());
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_object($result)){
$uidval = $row->user_id;
$posterInfo = $facebook->api_client->users_getInfo($uidval, array('name', 'pic_square_with_logo', 'profile_url'));
$nameuser = $posterInfo[0]['name']; //this is line 50
$pic = $posterInfo[0]['pic_square_with_logo'];
$profile_url = $posterInfo[0]['profile_url'];
echo '<img src="'.$pic.'" />';
echo ''.$nameuser.'';
echo '<br>';
echo $row->comment_time;
echo '<br>';
echo $row->msg;
}
}
it gives me this error:
Fatal error: Cannot use string offset as an array in /home/amitver/public_html/roadies/comments.php on line 50
but surprisingly i am using the exact same code successfully at the top of my page. why this weird behaviour. this is the code at the top of page:
//connect to fB
$uid = $user_id;
$userInfo = $facebook->api_client->users_getInfo($user_id, array('name', 'pic_square'));
$nameuser = $userInfo[0]['name'];
$pic = $userInfo[0]['pic_square'];
I think that sometimes theusers_getInfo is returning an array, while other times it is returning a string. Probably it only returns a simple string if only one result is available.
Try this:
$nameuser = ($posterInfo[0]) ? $posterInfo[0]['name'] : $posterInfo['name'];
this will happen if $posterInfo is actually an empty string ('').
can you var_dump($posterInfo) in the loop and check what it's doing...
Related
I'm trying to enable a user to go to a post detail page. They can find these images after a search. But I always get this:
Warning: Illegal string offset 'postID' in C:\xampp\htdocs\eindwerk2\index.php on line 106
Warning: Illegal string offset 'postID' in C:\xampp\htdocs\eindwerk2\index.php on line 180
this is the php code. The seach function works btw.
$_SESSION['KEYWORD'] = array();
$error = '';
//$allResults2 = array();
if (isset($_POST['Find'])) {
try {
$pdoConnect = new PDO("mysql:host=localhost;dbname=imdterest", "root", "");
} catch (Exception $exc) {
echo $exc->getMessage();
exit();
}
$postTags = $_POST['naam'];
//$pdoQuery = "SELECT * FROM posts INNER JOIN user ON posts.postUser = user WHERE postTags = :postTags";
$pdoQuery = "SELECT posts.postID, posts.postUser, posts.postImageUrl, posts.postDescription, user.firstname, user.lastname, user.userID FROM posts INNER
JOIN user ON posts.postUser=user.email WHERE postTags = :postTags;";
$pdoResult = $pdoConnect->prepare($pdoQuery);
$pdoExec = $pdoResult->execute(array(":postTags" => $postTags));
if(!empty($_POST)){
while ($row = $pdoResult->fetch(PDO::FETCH_ASSOC)) {
$_SESSION['KEYWORD'][] = $row['postImageUrl']['postID'];
//$poster = $row['postID'];
//$allResults2[] = $row['postUser'];
//echo $postTags;
}
}
else {
echo 'no';
}
if(count($_SESSION['KEYWORD']) === 0) {
$error = "Sorry no results!";
}
This is the html
<div class="search">
<?php
foreach ($_SESSION['KEYWORD'] as $imageLink){
$postid = $imageLink['postID'];
echo '';
}
/*foreach ($allResults2 as $imageUser){
echo '$imageUser';
}*/
?>
<div class="searchError"><?php echo $error; ?></div>
</div>
Your error is from this line of code:
$_SESSION['KEYWORD'][] = $row['postImageUrl']['postID'];
This isn't legal syntax in PHP. You can reproduce it with:
<?php
$test = array(
test1 => "Hello",
test2 => "hello2"
);
echo $test["test1"]["test2"];
?>
When you try to execute this code, you get:
PHP Warning: Illegal string offset 'test2' in
Sadly, I'm not that sure what you try to achieve with this line of code. Anyway, your SQL result is a one-dimensional array. Your code acts like you work with a two dimensional array. You can only use $row["postImageUrl"] or $row["postID"] you you can't use both directly.
If you describe a bit better what you try to do with this line, I can help you. But actually I have no idea what your thoughts were.
I have to use a php function to manage some data from a db, but i get a Fatal Error on the line where I call mysqli_fetch_array().
$request = "SELECT Post.* FROM Post WHERE IdUseFk = '".$user_idPk."';";
$result = mysqli_query($CONN, $request)
or die ("<font color='red'>Error: ".mysqli_error($CONN)."</font>");
$num = mysqli_num_rows($result);
if ($num == 0) {echo "NOTHING TO DO HERE";}
else
{
for ($i=1; $i<=$num; $i++)
{
$row = mysqli_fetch_array($result, MYSQLI_ASSOC); //<<-- ERROR ON THIS LINE
/*...Some other code...*/
}
}
I get this: Fatal error: in C:\xampp\htdocs\Work\load_data.php on line 137
Any suggestion?
EDIT: PROBLEM SOLVED
I can't believe it. For some reason the 'space' chars I put before and after the '=' char in $row = mysqli_fetch_array()
weren't real spaces but another special char that is blank in most editors but php read as part of the function name. I was able to see it thanks to the editor in cpanel. Now it works
Try using a while loop, and you also had an error in your SQL syntax.
$request = "SELECT Post.* FROM Post WHERE IdUseFk = '$user_idPk'";
$result = mysqli_query($CONN, $request)
or die ("<font color='red'>Error: ".mysqli_error($CONN)."</font>");
$num = mysqli_num_rows($result);
if ($num == 0) {
echo "NOTHING TO DO HERE";
} else {
while($row = mysqli_fetch_array($result) {
/*...Some other code...*/
}
}
I want to access the data in the php array. I have tried array[0] and array['Name'] but i dont get an output. here is my code:
session_start();
$user = $_SESSION['username'];
$sql_1 = "SELECT UserID FROM users WHERE username=$user";
$result_1 = mysqli_query($sql_1);
$uID = mysqli_fetch_array($result_1);
if ($uID=NULL) {
echo 'null';
} else {
echo $uID[0];
}
Now I am not getting any output from the echo command. So what am i doing wrong here?
The Part if ($uID=NULL) is always true[UPDATED:false], because you're doing an assignment rather than a comparism (that would be if ( $uID == NULL ))
Why is this not working?
<?php
$select = "select * from messages where user='$u'";
$query = mysqli_query($connect,$select) or die(mysqli_error($connect));
$row = mysqli_num_rows($query);
$result = mysqli_fetch_assoc($query);
$title = mysqli_real_escape_string($connect,trim($result['title']));
$message = mysqli_real_escape_string($connect,trim($result['message']));
while(($result = mysqli_fetch_assoc($query))){
echo $title;
echo '<br/>';
echo '<br/>';
echo $message;
}
?>
where as this works -
<?php
echo $title;
?>
SORRY TO SAY, BUT NONE OF THE ANSWERS WORK. ANY OTHER IDEAS?
If your mysqli query is returning zero rows then you will never see anything printed in your while loop. If $title and $message are not set (because you would want reference them by $result['title'] & $result['message'] if that are the field names in the database) then you will only see two <br /> tags in your pages source code.
If the while loop conditional is not true then the contents of the while loop will never execute.
So if there is nothing to fetch from the query, then you won't see any output.
Does you code display anything, or skip the output entirely?
If it skips entirely, then your query has returned 0 rows.
If it outputs the <br /> s, then you need to check your variables. I could be wrong, not knowing te entire code, but generally in this case you would have something like
echo $result['title'] instead of echo $title
If $title and $message come from your mysql query then you have to access them through the $result array returned by mysqli_fetch_assoc.
echo $result['title'];
echo $result['message'];
Also if your using mysqli you'd be doing something like this:
$mysqli = new mysqli("localhost", "user", "password", "db");
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
print $row['title'];
}
$result->close();
}
Try this:
<?php
$result = mysql_query($query);
while($row = mysqli_fetch_assoc($result)){
echo $title.'<br/><br/>'.$message;
}
?>
Does this work;
<?php
$select = "select * from messages where user='$u'";
$query = mysqli_query($connect,$select) or die(mysqli_error($connect));
$row = mysqli_num_rows($query);
while(($result = mysqli_fetch_assoc($query))){
echo $result['title'];
echo '<br/>';
echo '<br/>';
echo $result['message'];
}
?>
Basically I've made sure that it's not picking the first result from the query & then relying on there being more results to loop through in order to print the same message repeatedly.
i am trying to get get the following working nothing is happen when i use the function i am trying to get it to display images
class ItemRes {
//items DB
var $img="";
}
function ShowItemImage($index,$res_a){
if(sizeof($res_a) > $index){
if($res_a[$index] != NULL) {
$cimg = $res_a[$index]->img;
return "<img src='$cimg' width='70' height='70' style='cursor:pointer'></img>";
}
}else{
return "<center class='whitetxt'><strong>Empty</strong></center>";
}
}
$res_array = array();
$idx=0;
$result21 = mysql_query("SELECT * FROM photos WHERE eid='$eid' ORDER BY id DESC") or die (mysql_error());
while ($row21 = mysql_fetch_array($result21)) {
$img_path = $row21['path'];
$obj = new ItemRes();
$obj->img = $img_path;
$res_array[$idx] = $obj;
$idx++;
}
ShowItemImage(0,$res_array)
ShowItemImage(1,$res_array)
Thhere are many issues with this code, so I'll just take them one at the time.
Assuming you initiate the database connection somewhere else the first issue is
the line:
$result21 = mysql_query("SELECT * FROM photos WHERE eid='$eid' ORDER BY id DESC") or die (mysql_error());
Prior to this line you do not set the variable $eid and thus this will only fetch items with eid = ''
Second:
You do not end the last two rows with a semicolon, thus this will produce a fatal error.
Third:
Probably the reason to why you get 'nothing is happen' (wich I interpet as a blank page)
In your function ShowItemImage you return strings but you do nothing with them.
You need to change this:
ShowItemImage(0,$res_array)
ShowItemImage(1,$res_array)
to:
echo ShowItemImage(0,$res_array);
echo ShowItemImage(1,$res_array);
and you will probably start noticing some things on the screen.