Fatal error: Call to a member function fetch_assoc() on string - php

I tried to execute a query and iterate thru the result. The echo "<h1>" . $row["SummonerId"]. "</h1>"; works and it print it out at the page. But somehow this Error occurred after the printed result:
Fatal error: Call to a member function fetch_assoc() on string
I saw many errors similar to this. But those are on a non-object and not on string. What is that error?
Here is my code:
$servername = "localhost:3307";
$username = "root";
$password = "";
$dbname = "st-datacollector";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM summoner";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<h1>" . $row["SummonerId"]. "</h1>";
$summonerId = $row["SummonerId"];
$url = "https://euw.api.pvp.net/api/lol/euw/v1.3/game/by-summoner/" . $summonerId . "/recent?api_key=" . $api_key;
$result = file_get_contents($url);
$resultJSON = json_decode($result);
foreach($resultJSON_decoded->games as $game){
echo $game->gameMode." ".$game->gameType." ".$game->subType;
echo "<br>";
}
}
} else {
echo "0 results";
}
$conn->close();

$result variable is used to loop thru mysql query, you have to use different variable names inside your loop
$servername = "localhost:3307";
$username = "root";
$password = "";
$dbname = "st-datacollector";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM summoner";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<h1>" . $row["SummonerId"]. "</h1>";
$summonerId = $row["SummonerId"];
$url = "https://euw.api.pvp.net/api/lol/euw/v1.3/game/by-summoner/" . $summonerId . "/recent?api_key=" . $api_key;
$fcontents = file_get_contents($url);
$resultJSON = json_decode($fcontents);
foreach($resultJSON_decoded->games as $game){
echo $game->gameMode." ".$game->gameType." ".$game->subType;
echo "<br>";
}
}
} else {
echo "0 results";
}
$conn->close();
One more little thing I noticed.. $resultJSON and $resultJSON_decoded doesn't match inside the loop

Related

How can fetch a variable in sql to php according to post id? [duplicate]

I am getting an error "Fatal error: Call to undefined function fetch_assoc()". Can you please advise on how I should proceed?
The code is mentioned below
$servername = "localhost";
$username = "s";
$password = "j";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "select idnew_table, new_tablecol from new_schema.new_table;";
$result = $conn->query($sql);
$r = count($result);
if ($result['num_rows'] != 10) {
while($row = $result[fetch_assoc()]){
echo "id: " . $row["idnew_table"]. " - Name: " . $row["new_tablecol"];
}
} else {
echo "0 results";
}
$conn->close();
This line:
while($row = $result[fetch_assoc()]){
should be:
while($row = $result -> fetch_assoc()){

add alt for images in a while statement

Can anyone help please?
This is my code.I want to add an alt option for images but everything I try is throwing errors.
I have tried studying the php handbook and have tried copying code from other questions but so far no luck.
<?php
$servername = "localhost";
$username = "logosewe_5";
$password = "password";
$dbname = "logosewe_5";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM mk2";
$sql = "SELECT * FROM mk2 WHERE brand='2786'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<img src='$row['ming']'>';
}
} else {
echo "0 results";
}
$conn->close();
?>
You have a string concatenation problem :
echo '<img src="' . $row['ming'] . '">';
Will output : <img src="myimage.jpg">.
To add a alt attribute, you could do :
echo '<img src="' . $row['ming'] . '" alt="' . $row['name'] . '">';
Will output something like
<img src="myimage.jpg" alt="image name">
change your code like this:
<?php
$servername = "localhost";
$username = "logosewe_5";
$password = "password";
$dbname = "logosewe_5";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM mk2"
;
$sql = "SELECT * FROM mk2 WHERE brand='2786'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<img src="'.$row['ming'].'" alt="'.$altText.'">'; //change here
}
} else {
echo "0 results";
}
$conn->close();
?>
The problem was that you did not have an alt attribute in your echo statement and the second was your concatenation was a little off with misplaced quotes.
while($row = $result->fetch_assoc()) {
echo '<img src=' . $row['ming'] . 'alt="Your alt message">';
}

Displaying results of SQL query as a table?

I am trying to output the results of an SQL query as a table on a page on my website. I have found a few solutions online but I can't get any of them to work properly. Right now I copied and pasted a bit of code to just output the first two columns but I can't figure out how to get every column in a table. I am new to PHP and web development in general so any help would be appreciated.
My PHP:
<?php
SESSION_START() ;
$servername = "localhost";
$username = "MY USERNAME";
$password = "MY PASSSWORD";
$dbname = "MY DATABASE NAME";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//$_session['userid'] = $userlogged;
$sql = "SELECT * FROM `climbs` WHERE `userlogged` = '" . $_SESSION['userid'] . "'";
$result = mysqli_query($conn,$sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["climb-id"]. "</td><td>" . $row["climbname"]. " " . $row["cragname"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
mysqli_close($conn);
?>
check with var_dump :
some like that:
$result = mysqli_query($conn,$sql);
var_dump($result);
if ($result->num_rows > 0) {
maybe the query it's wrong.

Give argument php function to show database results in another file

I have this php code to connect database it works fine.
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "cv";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
mysqli_set_charset($conn,"utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM services";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["service_name"]. " " . $row["service_desc"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
My question is: how to create function getServices() and give argument to show this results in another php file using foreach or while, like this:
<?php foreach ($results as $key=>$result) : ?>
<?php echo $result['service_name']; ?>
<?php endforeach; ?>
Okay. So you have to do the following things:
db.php :
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "cv";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
mysqli_set_charset($conn,"utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
function yourFunctionName() {
$sql = "SELECT * FROM services";
$result = $conn->query($sql);
return $result;
}
$conn->close();
otherfile.php :
<?php
include 'db.php';
$data = yourFunctionName();
if ($data->num_rows > 0) {
// output data of each row
while($row = $data->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["service_name"]. " " . $row["service_desc"]. "<br>";
}
} else {
echo "0 results";
}
?>
Hope this works, haven't tested yet.

MySQL Data to JSON Array with inserting a string

This is my Code for now but it only works until the for loop in the mySQLResultsetToJSON().
<?php
$servername = "127.0.0.1";
$username = "root";
$password = "123456";
$table = "ticketing";
$link = new mysqli($servername, $username, $password, $table);
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}
echo "Connected successfully";
$result = $link->query("SELECT * from Ticket");
print_r($result);
$final_result = mySQLResultsetToJSON($result);
print_r($final_result);
$link->close();
function mySQLResultsetToJSON($resultSet)
{
for($i = 0; sizeof($resultSet); $i++)
{
$rows = array();
while($r = mysqli_fetch_assoc($resultSet[$i])) {
$rows[] = $r;
}
$jsonResult[$i] = json_encode(array('Results' => $rows));
}
print_r($jsonResult);
return $jsonResult;
}
?>
Thank you!
Thomas
echo "mysql data<br />";
$result = $link->query("SELECT * from users");
print_r($result->fetch_object());
echo "<br />";
echo "in json<br />";
$res = ['Results' => $result->fetch_object() ];
echo json_encode($res);
$link->close();
User like
$result = $link->query("SELECT * from Ticket");
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
print "<pre>";
print_r(json_encode(array('Results' =>$rows)));
$link->close();

Categories