I'm having problems with the Array to string conversion error.
It's occurring here:
<div id="pagename">
<?php echo ['SELECT name FROM hjemmesider']; ?>
</div>
I'm trying to fetch a row from my db and display it, but i can't make it work... I have tried tons of things to try and sort it out, and of course researched it a great bit on the internet.
in your foreach or while use echo $row['your_field_name'];
You are not doin it right.. try like this:
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$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["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
It most probably means that you are trying to echo an Array. Use print_r function on your result and things will get clearer.
Related
I am updating all my code to Mysqli, before I did I had this code and it worked:
while($row = mysql_fetch_assoc($WildHorses)){
$InWild[] = $row['id'];
}
$RandomWild = array_rand($InWild);
$RandomHorse = $InWild[$RandomWild];
This is my SELECT statement:
$sql = "SELECT Horse.id, Horse.Name, Horse.Age, Horse.Image_name, Horse.Owner, Horse.Barn, Images.Image_path, Images.Image_name FROM Horse, Images WHERE Horse.Owner = '$colname_WildHorseList' AND Images.Image_name = Horse.Image_name";
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " Name: " . $row["Name"]. " ImageName: " . $row["Image_name"]. "<br>";
}
} else {
echo "0 results";
}
The SELECT statement ends up echoing all of the correct information, but I want to make an array of only the Id's so that I can pick one at random each time a button is clicked.
I have tried multiple different copies and pastes of code to try and get what I want, but nothing seems to come out right.
Can someone point me in the right direction or explain what I'm doing wrong?
In your while loop you can simply do this :-
$i=0;
$animals=array();
$animals[$i]=$row["id"]; //puts id in array
And then you can create a random number by "rand()" between the length of 0-$i
and can get the job done.
I'm trying to get rows from my database between two dates. I've used other questions here to get to this point, but I don't know how to move forward from here.
Here is the code, and underneath are the screenshots of the output and a photo running the query in SQL.
Thanks for the help in advance!
--
This first section of code outputs the ID, name, etc.
<?php
$sql = "SELECT * FROM songs ";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["ID"] . " - Name: " . $row["name"] . " " . $row["released"]. "<br>";
}
} else {
echo "0 results";
}
/* close connection */
$link->close();
?>
This section of code outputs "0 Results"
<?php
$from_date = '2015-01-01';
$to_date = '2017-04-04';
$sql = "SELECT * FROM songs WHERE released BETWEEN '" . $from_date . "' AND '" . $to_date . "' ";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["ID"] . " - Name: " . $row["name"] . " " . $row["released"]. "<br>";
}
} else {
echo "0 results";
}
/* close connection */
$link->close();
?>
This is what the page looks like with the above code.
I've successfully selected the rows in SQL.
EDIT: I made a mistake when writing the code into the question for the IF ELSE statement on the 2nd block of code. I just updated it to what I actually have on the site. I didn't change anything based on the solutions, and am still not getting the rows to print.
You done everything right, except using while/else ;
Your code (2-nd) section should look like this:
if ($result->num_rows > 0) {
while () {
//print your rows
}
} else {
//Say that there's 0 results
}
Your results are listed - as you can see from your screenshot. Problem: While along with else does not make sence.
your last example is missing a } so it says: while { } else { echo "0 results"}
But the echo "0 results" should be related to the num_rows-check, not to the while.
Ah man, I just needed to had to comment out this line from the first block:
$link->close();
I apologize to everyone who answered if it wasn't clear that the blocks of code followed one another. Anyways, for anyone else with the same problem, don't close the connection -_-
Here's what is output when I comment out the line from the first block
I'm really stuck on this guys, and I know where the fault is but I can't seem to fix it.
The while loop itself works it's the if that is causing all the trouble.
Whenever there are 3 groups in the database it only displays 2 the strange part it will only display the items in the while loop. But the first item is somehow tied to the if statement.
public function get_group($user_id){
$sql3="SELECT * FROM groups WHERE user =
$user_id";
$results = mysqli_query($this->db, $sql3);
$user_data = mysqli_fetch_array($results);
if ($results->num_rows > 0) {
// output data of each row
while($row = $results->fetch_assoc()){
echo "hello";
echo "group :" . $row["group"] . "<br>";
echo "groupdesc:" . $row["groupdesc"] . "<br>";
echo "<a class='btn btn-primary' href='project.php?groupid=" . $row["groupid"] . "'>></a>";
}
}
else {
echo "0 results";
}
}
It seems it can't output the first one the first one just keeps hidden.
You fetch your first row in this line:
$user_data = mysqli_fetch_array($results);
And you do nothing with it.
And please do not mix procedural and object-oriented usage of mysqli.
This is the part of the PHP code I am having the issue:
$query = "SELECT * FROM clients where idcard = '$idcard'";
$result = mysqli_query($dbc, $query)
or die("Error quering database.");
if(mysqli_fetch_array($result) == False) echo "Sorry, no clients found";
while($row = mysqli_fetch_array($result)) {
$list = $row['first_name'] . " " . $row['last_name'] . " " . $row['address'] . " " . $row['town'] . " " . $row['telephone'] . " " . $row['mobile'];
echo "<br />";
echo $list;
}
Even if I insert an existing idcard value I get no output when there is the if statement, an incorrect idcard displays "Sorry, no clients found" fine. However if I remove the if statement if I enter an existing idcard the data displays ok.
Can you let me know what is wrong with the code please ?
Thanks
Use mysqli_num_rows to count the results:
if(mysqli_num_rows($result) == 0) echo "Sorry, no clients found";
mysqli_fetch_array() fetches an item from the database.
This means your if() code fetches a first item from the database.
Then, when you call mysqli_fetch_array() again from the while() condition, the first item has already been fetched, and you are trying to fetch the second one ; which does not exist.
You must ensure that you use the result from mysqli_fetch_array() and not call it one time just for nothing ; or, as an alternative, you could use the mysqli_num_rows() function (quoting) :
Returns the number of rows in the result set.
$query = "SELECT * FROM clients where idcard = '$idcard'";
$result = mysqli_query($dbc, $query)
or die("Error quering database.");
if(mysqli_num_rows($result) == 0) {
echo "Sorry, no clients found";
}else{
while($row = mysqli_fetch_array($result)) {
$list = $row['first_name'] . " " . $row['last_name'] . " " . $row['address'] . " " . $row['town'] . " " . $row['telephone'] . " " . $row['mobile'];
echo $list . "<br />";
}
}
Try this.
EDITED: Added closing bracket.
Use mysqli_num_rows() to test if there is anything returned.
Imagine you put some money in your pocket.
Eventually an idea came to your mind to see if you are still have the money.
You are taking it out and count them. All right.
Still holding them in hand you decided to take them from pocket. Oops! The pocket is empty!
That's your problem.
To see if you got any rows from the database you can use mysqli_num_rows(). It will return the number of bills, without fetching them from the pocket.
The problem is, that you try to use mysqli_fetch_array to queck for the number of results. mysqli_fetch_array will fetch the first result, compare it to false and then discard it. The next mysqli_fetch_array will then fetch the second result, which is not existing.
If you want to check if any clients where found, you can use mysqli_num_rows like this:
$idcard = mysqli_escape_string($dbc, $idcard); // See below: Prevents SQL injection
$query = "SELECT * FROM clients where idcard = '$idcard'";
$result = mysqli_query($dbc, $query) or die("Error quering database.");
if(mysqli_num_rows($result) == 0) {
echo "Sorry, no clients found";
} else {
while($row = mysqli_fetch_array($result)) {
// Do whatever you want
}
}
If $idcard is a user supplied value, please look out for SQL injection attacks.
Thank you for reading my question.
I am trying to make a site where information from a database is displayed onto a webpage. The end result will look like this, but for a different game.
Here is a plain HTML page of what I want it to look like.
So far I know that my connection to the database works. When I run:
mysql_select_db("DATABASE", $con);
$result = mysql_query("SELECT * FROM DATABASE");
while($row = mysql_fetch_array($result)) {
echo $row['Title'] . " " . $row['Type'];
echo "<br />";
}
It returns the Title and Type.
What I want to do is run an If/Else statement that runs a different that block of code depending on the card type.
while($row = mysql_fetch_array($result)) {
if ($row['Title'] == 'Hero') {
echo "<div>";
}
}
I tried this based on the tutorials at w3schools.com but it doesn't work.
Do any of you have any ideas for what I should do?
EDIT:
Here is what I tried running:
while($row = mysql_fetch_assoc($result)) {
if ($row['Title'] == 'Hero') {
echo $row['Title'] . " Hero.<br>";
} else {
echo $row['Title'] . " Who cares.<br>";
}
}
Here is the output (Gimli should show up as a Hero):
For Gondor! Who cares.<br>
Bilbo Baggins Who cares.<br>
Ungoliant's Spwan Who cares.<br>
Gimli Who cares.
EDIT 2: Thank you Phil for spotting the error, I now get the result I wanted using Mikushi's method. Thank you all so much.
The fetching of your mysql result seems wrong, should be like this:
while($row = mysql_fetch_assoc($result)) {
if ($row['Title'] == 'Hero') {
echo ""; }
}
mysql_fetch_array fetch the result as an indexed array (1=> data, 2=> thing) , which explains why $row['Title'] doesn't work.
The difference:
http://ca2.php.net/mysql_fetch_array
http://ca2.php.net/mysql_fetch_assoc
Please, always refer to the documentation, it's very well done and a better source than w3cschools.
Maybe it's one of those all too obvious things but...
Shouldn't it be
if ($row['Type'] == 'Hero') // "Type", not "Title"