Here is the code that is giving me a problem:
$letter = $_POST['artistButton'];
$result = $mysqli->query("SELECT * FROM `Generic` WHERE `songName` LIKE '$letter%'");
$row = $result->fetch_array();
while ($row = $result->fetch_array()) {
$Artist = $row['Artist'];
$songName = $row['songName'];
$Duration = $row['Duration'];
$URL = $row['URL'];
$Genre = $row['Genre'];
echo "<input type='submit' value='$songName' name='artistButton'>";
echo "<br />";
}
The statement is correct I saved it to a variable and printed it to the screen, then ran it from the database and it printed the results I wanted so it definitively is not that. For some reason though it is not displaying. I am guessing it has something to do with something I messed up converting from mysql to mysqli. Thanks -Sam
Clearly some codes are cut, but try to remove that extra:
$row = $result->fetch_array();
And use:
while($row = $result->fetch_assoc()) {
Since you're interested on associative indices anyway.
Important note: Since you're using mysqli_*, use its parameterized queries instead. Don't directly use $_POST values on the query.
$letter = "{$_POST[artistButton]}%";
$stmt = $mysqli->prepare('SELECT * FROM `Generic` WHERE `songName` LIKE ?');
$stmt->bind_param('s', $letter);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {
$Artist = $row['Artist'];
$songName = $row['songName'];
$Duration = $row['Duration'];
$URL = $row['URL'];
$Genre = $row['Genre'];
echo "<input type='submit' value='$songName' name='artistButton'>";
echo "<br />";
}
Related
I am trying to return a value from my database based on a user input on my form.
When I run the code using a value it works but when I put in the variable it doesn't. I am sure it is something simple, but I just don't get it?
Here is the code that works:
$sql_beam = mysqli_query($link,"SELECT cost_ft FROM Beams WHERE number = '201'");
while($row = mysqli_fetch_array($sql_beam))
{
echo "<p>" . $row['cost_ft'] . "</p>";
echo "<br>";
}
When I change it to this it doesn't:
$beam_num = $_POST['Beam Number'];
$sql_beam = mysqli_query($link,"SELECT cost_ft FROM Beams WHERE number = '$beam_num'");
while($row = mysqli_fetch_array($sql_beam))
{
echo "<p>" . $row['cost_ft'] . "</p>";
echo "<br>";
}
You should never put any variable directly into a query. Google sql injection and how to prevent it.
Here is a simple example:
$sql = "SELECT cost_ft FROM Beams WHERE number = ?";
$stmt = $link->prepare($sql);
$stmt->bind_param("s", $beam_num);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc())
{
echo "<p>" . $row['cost_ft'] . "</p>";
echo "<br>";
}
Variables in a query work best when enclosed in curly brackets like this:
$beam_num = $_POST['Beam Number'];
$sql_beam = mysqli_query($link,"SELECT cost_ft FROM Beams WHERE number = '{$beam_num}'");
while($row = mysqli_fetch_array($sql_beam))
{
echo "<p>" . $row['cost_ft'] . "</p>";
echo "<br>";
}
That should solve the problem, if it doesnt then try this:
$sql_beam = mysqli_query($link,"SELECT cost_ft FROM Beams WHERE number = $beam_num");
I had that in one of my codes and it worked.
Change your second line to this:
$sql_beam = mysqli_query($link,"SELECT cost_ft FROM Beams WHERE number = '". $beam_num ."'");
I'm trying to echo a bit of html within a while loop. I'm getting my stuff with PDO and PDO_ASSOC.
This is what I have:
$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$book_id = $row['id'];
$book_title = $row['title'];
$book_image = $row['image'];
$book_amz = $row['amazon'];
$book_desc = $row['description'];
$book_rating = $row['rating'];
$book_date = $row['date'];
$book_author = $row['author'];
$book_categorie = $row['categorie'];
$text = "ID: ' . $book_id . '";
}
return $text;
But it gives me only one row of the table. I even tried fetchAll, but it gives me nothing.
So making the assumption that the only element ever seen is the last element it is because what your are returning is being overwritten each loop. There are a few options to resolve this. The simplest is:
$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
$text = "";
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$book_id = $row['id'];
$book_title = $row['title'];
$book_image = $row['image'];
$book_amz = $row['amazon'];
$book_desc = $row['description'];
$book_rating = $row['rating'];
$book_date = $row['date'];
$book_author = $row['author'];
$book_categorie = $row['categorie'];
//String concatenation of text will
//give you one big string at the end to return.
$text .= "ID: '{$book_id}'";
}
return $text;
However this will not work well with your real bootstrap html. You need to make sure that the columns add up right.
You will need something a bit more intuitive
Using the actual code it would look something like
$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
$bookEcho = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$bookEcho[] = '<div class="col-md-3">
<div class="thumbnail">
<span>' . $book_title . '</span>
<img src="' . $book_image . '">
<div class="book-options">
<span>Bewertung</span><br/>
' . $stars . '
Jetzt lesen
</div>
</div>
</div>';
}
return $bookEcho;
Now in your function what ever it is you can do something like (this is not the most elegant thing I have ever written but should get the job done):
$cols = 4;
$colCount = 1;
foreach ($bookEcho as $book){
if($colCount == 0){//create a row}
echo $book;
$coolCount++;
if($colCount == 0){end a row}
if($colCount == 4){ $colCount = 0;}
}
The problem is that you're overwriting your values in your while loop. The loop is being executed once for each entry in the database, but only the last one will be returned. Instead you want to use arrays:
$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
$books = [];
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$book['id'] = $row['id'];
$book['title'] = $row['title'];
$book['image'] = $row['image'];
$book['amz'] = $row['amazon'];
$book['desc'] = $row['description'];
$book['rating'] = $row['rating'];
$book['date'] = $row['date'];
$book['author'] = $row['author'];
$book['categorie'] = $row['categorie'];
$book['text'] = "ID: ' . {$book['id']} . '"; // << Not sure if this is what you actually want. If not, adjust accordingly.
// Append the above values to the $books array
$books[] = $book;
}
return $books;
Use a foreach:
$books = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($books as $book){
$book_id = $book['id'];
$book_title = $book['title'];
// ...
}
I have an SQL query which I have recently turned into a prepared statement for security purposes. I have a query which returns many rows, each consisting of many columns. My question is how to echo the results using a while loop. My example I have so far:
$stmt = $conn->prepare("SELECT * FROM Customers
WHERE travel_Date >= ?
AND travel_Date <= ?
".$searchOption."
LIMIT ?
OFFSET ?");
$todayDateFrom = $todayDate." 00:00:00";
$todayDateTo = $todayDate." 23:59:59";
$stmt->bind_param("ssii", $todayDateFrom, $todayDateTo, $limit, $offset);
$stmt->execute();
while ($stmt->fetch()) {
//echo first name
//echo surname
//echo address
//echo number
//echo type
//15 other things i need to print off
}
I'm not sure what the best way to do this is. I have thought about:
$stmt->bind_result($firstName, $surname, $address, //etc);
But I'm wondering if there's another alternative similar to unprepared statements:
while($row = mysqli_fetch_array($query)){
echo $row['firstName'];
echo $row['surname'];
echo $row['address'];
//etc
}
try this:
$result = $stmt->get_result();
while ($row = $result->fetch_array())
{
echo $row['firstName'];
echo $row['surname'];
echo $row['address'];
}
try this -
$result_arr = array();
while($row = $stmt->fetch()){
$record = array();
$record['firstname'] = $row['firstname']; // if your db column contains firstname column other wise you can also use $row[0];
$record['name'] = $row[1]; // end so on .......
$result_arr[] = $record;
}
hope this will work for u...
I am trying to return a value from my database based on a user input on my form.
When I run the code using a value it works but when I put in the variable it doesn't. I am sure it is something simple, but I just don't get it?
Here is the code that works:
$sql_beam = mysqli_query($link,"SELECT cost_ft FROM Beams WHERE number = '201'");
while($row = mysqli_fetch_array($sql_beam))
{
echo "<p>" . $row['cost_ft'] . "</p>";
echo "<br>";
}
When I change it to this it doesn't:
$beam_num = $_POST['Beam Number'];
$sql_beam = mysqli_query($link,"SELECT cost_ft FROM Beams WHERE number = '$beam_num'");
while($row = mysqli_fetch_array($sql_beam))
{
echo "<p>" . $row['cost_ft'] . "</p>";
echo "<br>";
}
You should never put any variable directly into a query. Google sql injection and how to prevent it.
Here is a simple example:
$sql = "SELECT cost_ft FROM Beams WHERE number = ?";
$stmt = $link->prepare($sql);
$stmt->bind_param("s", $beam_num);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc())
{
echo "<p>" . $row['cost_ft'] . "</p>";
echo "<br>";
}
Variables in a query work best when enclosed in curly brackets like this:
$beam_num = $_POST['Beam Number'];
$sql_beam = mysqli_query($link,"SELECT cost_ft FROM Beams WHERE number = '{$beam_num}'");
while($row = mysqli_fetch_array($sql_beam))
{
echo "<p>" . $row['cost_ft'] . "</p>";
echo "<br>";
}
That should solve the problem, if it doesnt then try this:
$sql_beam = mysqli_query($link,"SELECT cost_ft FROM Beams WHERE number = $beam_num");
I had that in one of my codes and it worked.
Change your second line to this:
$sql_beam = mysqli_query($link,"SELECT cost_ft FROM Beams WHERE number = '". $beam_num ."'");
So I have some numbers that take the name "id" in my MySQL database. I tried this code:
while($row = mysql_fetch_array($result))
{
$id=$row['id'];
$user = $row['usrname'];
$fname = $row['fname'];
$lname = $row['lname'];
echo "<strong>User:</strong> ".$user." ".$fname." ".$lname."<br/>";
echo min($id);
}
On the last line I tried to echo the min value for id, but it didn't work. Any suggestions?
ANSWER: Use "ASC LIMIT 1"
while($row = mysql_fetch_assoc($sql)){
$id=$row['id'];
$user = $row['usrname'];
$fname = $row['fname'];
$lname = $row['lname'];
echo "<strong>User:</strong> ".$user." ".$fname." ".$lname."<br/>";
echo $id;
}
$sql = mysql_query("SELECT * FROM users ORDER BY id ASC LIMIT 1");
while($row = mysql_fetch_assoc($sql))
{
$id=$row['id'];
$user = $row['usrname'];
$fname = $row['fname'];
$lname = $row['lname'];
echo "<strong>User:</strong> ".$user." ".$fname." ".$lname."<br/>";
echo $id;
}
Try this:
$sql = 'SELECT min(id) as min FROM users';
$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);
$min_id = $row['min'];
In case you cannot change the query order (sort by names?), you can determine the lowest $id by using a helper variable:
$min = isset($min) ? min($min, $id) : $id;
And then print $min; after your loop.
If not able to use MIN() in MySQL, try this...
$smallestId = -PHP_INT_MAX;
while($row = mysql_fetch_assoc($sql)) {
...
$smallestId = min($smallestId, $id);
}
If you never have any negative ids (you shouldn't), you could just change -PHP_INT_MAX to 0.